Search in sources :

Example 1 with SubscriptionInner

use of com.microsoft.azure.management.resources.implementation.SubscriptionInner in project photon-model by vmware.

the class AzureEndpointAdapterService method validateCredentials.

private DeferredResult<SubscriptionInner> validateCredentials(AuthCredentialsServiceState credentials) {
    String msg = "Getting Azure Subscription [" + credentials.userLink + "] for endpoint validation";
    AzureDeferredResultServiceCallback<SubscriptionInner> handler = new AzureDeferredResultServiceCallback<SubscriptionInner>(this, msg) {

        @Override
        protected DeferredResult<SubscriptionInner> consumeSuccess(SubscriptionInner subscription) {
            logFine(() -> String.format("Got subscription %s with id %s", subscription.displayName(), subscription.id()));
            if (!SubscriptionState.ENABLED.equals(subscription.state())) {
                logFine(() -> String.format("Subscription with id %s is not in active" + " state but in %s", subscription.id(), subscription.state()));
                return DeferredResult.failed(new IllegalStateException("Subscription is not active"));
            }
            return DeferredResult.completed(subscription);
        }
    };
    AzureSdkClients azureSdkClients = new AzureSdkClients(credentials);
    SubscriptionClientImpl subscriptionClient = azureSdkClients.getSubscriptionClientImpl();
    subscriptionClient.subscriptions().getAsync(credentials.userLink, handler);
    return handler.toDeferredResult().whenComplete((res, exc) -> azureSdkClients.close());
}
Also used : AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) SubscriptionClientImpl(com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl) SubscriptionInner(com.microsoft.azure.management.resources.implementation.SubscriptionInner)

Example 2 with SubscriptionInner

use of com.microsoft.azure.management.resources.implementation.SubscriptionInner 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)

Aggregations

SubscriptionClientImpl (com.microsoft.azure.management.resources.implementation.SubscriptionClientImpl)2 SubscriptionInner (com.microsoft.azure.management.resources.implementation.SubscriptionInner)2 AzureDeferredResultServiceCallback (com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback)1 AzureSdkClients (com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients)1 ServiceErrorResponse (com.vmware.xenon.common.ServiceErrorResponse)1