Search in sources :

Example 1 with ServiceErrorResponse

use of com.vmware.xenon.common.ServiceErrorResponse in project photon-model by vmware.

the class VSphereEndpointAdapterService method doValidate.

private void doValidate(AuthCredentialsServiceState credentials, BiConsumer<ServiceErrorResponse, Throwable> callback, URI adapterManagementUri, String id) {
    BasicConnection connection = createConnection(adapterManagementUri, credentials);
    try {
        // login and session creation
        connection.connect();
        if (id != null && !id.isEmpty()) {
            // if a datacenter is configured also validate moref is OK
            new GetMoRef(connection).entityProp(VimUtils.convertStringToMoRef(id), VimNames.PROPERTY_NAME);
        }
        callback.accept(null, null);
    } catch (RuntimeFaultFaultMsg | InvalidPropertyFaultMsg | IllegalArgumentException e) {
        ServiceErrorResponse r = Utils.toServiceErrorResponse(e);
        r.statusCode = STATUS_CODE_BAD_REQUEST;
        r.message = String.format("Error looking for datacenter for id '%s'", id);
        callback.accept(r, e);
    } catch (ConnectionException e) {
        String msg = String.format("Cannot establish connection to %s", adapterManagementUri);
        logWarning(msg);
        callback.accept(null, e);
    } finally {
        closeQuietly(connection);
    }
}
Also used : GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) ConnectionException(com.vmware.photon.controller.model.adapters.vsphere.util.connection.ConnectionException)

Example 2 with ServiceErrorResponse

use of com.vmware.xenon.common.ServiceErrorResponse in project photon-model by vmware.

the class AzureInstanceService method validateAzureCredentials.

/**
 * Validates azure credential by making an API call.
 */
private void validateAzureCredentials(final AzureInstanceContext ctx) {
    if (ctx.computeRequest.isMockRequest) {
        ctx.operation.complete();
        return;
    }
    SubscriptionClientImpl subscriptionClient = new SubscriptionClientImpl(ctx.azureSdkClients.credentials);
    subscriptionClient.subscriptions().getAsync(ctx.endpointAuth.userLink, new ServiceCallback<SubscriptionInner>() {

        @Override
        public void failure(Throwable e) {
            // Azure doesn't send us any meaningful status code to work with
            ServiceErrorResponse rsp = new ServiceErrorResponse();
            rsp.message = "Invalid Azure credentials";
            rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
            ctx.operation.fail(e, rsp);
        }

        @Override
        public void success(SubscriptionInner result) {
            logFine(() -> String.format("Got subscription %s with id %s", result.displayName(), result.id()));
            ctx.operation.complete();
        }
    });
}
Also used : SubscriptionClientImpl(com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl) SubscriptionInner(com.microsoft.azure.management.resources.implementation.SubscriptionInner) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse)

Example 3 with ServiceErrorResponse

use of com.vmware.xenon.common.ServiceErrorResponse in project photon-model by vmware.

the class AzureEndpointAdapterService method validate.

private BiConsumer<AuthCredentialsServiceState, BiConsumer<ServiceErrorResponse, Throwable>> validate(EndpointConfigRequest body) {
    return (credentials, callback) -> {
        try {
            Boolean shouldProvision = Boolean.parseBoolean(body.endpointProperties.get(AZURE_PROVISIONING_PERMISSION));
            validateEndpointUniqueness(credentials, body.checkForEndpointUniqueness, body.tenantLinks).thenCompose(aVoid -> validateCredentials(credentials)).thenCompose(subscription -> getPermissions(credentials)).thenCompose(permList -> verifyPermissions(permList, shouldProvision)).whenComplete((aVoid, e) -> {
                if (e == null) {
                    callback.accept(null, null);
                    return;
                }
                if (e instanceof CompletionException) {
                    e = e.getCause();
                }
                final LocalizableValidationException localizableExc;
                if (e instanceof LocalizableValidationException) {
                    localizableExc = (LocalizableValidationException) e;
                } else {
                    // Azure doesn't send us any meaningful status code to work with
                    localizableExc = new LocalizableValidationException(e, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE);
                }
                ServiceErrorResponse rsp = Utils.toServiceErrorResponse(localizableExc);
                rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
                callback.accept(rsp, localizableExc);
            });
        } catch (Throwable e) {
            logSevere(e);
            ServiceErrorResponse rsp = new ServiceErrorResponse();
            rsp.message = "Invalid Azure credentials";
            rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
            callback.accept(rsp, e);
        }
    };
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) QueryTask(com.vmware.xenon.services.common.QueryTask) PROVIDER_PERMISSIONS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_PERMISSIONS_URI) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubscriptionInner(com.microsoft.azure.management.resources.implementation.SubscriptionInner) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE) URI(java.net.URI) Permission(com.vmware.photon.controller.model.adapters.azure.model.permission.Permission) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) AUTHORIZATION_NAMESPACE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTHORIZATION_NAMESPACE) PROVIDER_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_REST_API_VERSION) CompletionException(java.util.concurrent.CompletionException) PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) STATUS_CODE_UNAUTHORIZED(com.vmware.xenon.common.Operation.STATUS_CODE_UNAUTHORIZED) Optional(java.util.Optional) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) PermissionList(com.vmware.photon.controller.model.adapters.azure.model.permission.PermissionList) PRIVATE_KEY_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEY_KEY) SubscriptionState(com.microsoft.azure.management.resources.SubscriptionState) USER_LINK_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.USER_LINK_KEY) HashMap(java.util.HashMap) PRIVATE_KEYID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEYID_KEY) SubscriptionClientImpl(com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl) ArrayList(java.util.ArrayList) ZONE_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ZONE_KEY) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) BiConsumer(java.util.function.BiConsumer) Retriever(com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils.Retriever) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) EndpointAdapterUtils(com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils) AZURE_TENANT_ID(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_TENANT_ID) SUPPORT_DATASTORES(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_DATASTORES) AZURE_PROVISIONING_PERMISSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_PROVISIONING_PERMISSION) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) REGION_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.REGION_KEY) SUPPORT_PUBLIC_IMAGES(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_PUBLIC_IMAGES) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) CompletionException(java.util.concurrent.CompletionException) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse)

Aggregations

ServiceErrorResponse (com.vmware.xenon.common.ServiceErrorResponse)3 SubscriptionClientImpl (com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl)2 SubscriptionInner (com.microsoft.azure.management.resources.implementation.SubscriptionInner)2 SubscriptionState (com.microsoft.azure.management.resources.SubscriptionState)1 EndpointConfigRequest (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest)1 PRIVATE_KEYID_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEYID_KEY)1 PRIVATE_KEY_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEY_KEY)1 REGION_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.REGION_KEY)1 RequestType (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType)1 SUPPORT_DATASTORES (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_DATASTORES)1 SUPPORT_PUBLIC_IMAGES (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.SUPPORT_PUBLIC_IMAGES)1 USER_LINK_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.USER_LINK_KEY)1 ZONE_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ZONE_KEY)1 AzureUriPaths (com.vmware.photon.controller.model.adapters.azure.AzureUriPaths)1 AUTHORIZATION_NAMESPACE (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTHORIZATION_NAMESPACE)1 AUTH_HEADER_BEARER_PREFIX (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX)1 AZURE_PROVISIONING_PERMISSION (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_PROVISIONING_PERMISSION)1 AZURE_TENANT_ID (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_TENANT_ID)1 PROVIDER_PERMISSIONS_URI (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_PERMISSIONS_URI)1 PROVIDER_REST_API_VERSION (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.PROVIDER_REST_API_VERSION)1