Search in sources :

Example 1 with AzureSdkClients

use of com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients 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 AzureSdkClients

use of com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients in project photon-model by vmware.

the class AzureStorageEnumerationAdapterService method handleEnumerationImpl.

/**
 * Creates the storage description states in the local document store based on the storage
 * accounts received from the remote endpoint.
 *
 * @param context The local service context that has all the information needed to create the
 *                additional description states in the local system.
 */
private void handleEnumerationImpl(StorageEnumContext context) {
    switch(context.stage) {
        case CLIENT:
            if (context.azureSdkClients == null) {
                context.azureSdkClients = new AzureSdkClients(context.endpointAuth);
            }
            context.stage = EnumerationStages.ENUMERATE;
            handleEnumeration(context);
            break;
        case ENUMERATE:
            String enumKey = getEnumKey(context);
            switch(context.request.enumerationAction) {
                case START:
                    logInfo(() -> String.format("Launching Azure storage enumeration for %s", enumKey));
                    context.enumerationStartTimeInMicros = Utils.getNowMicrosUtc();
                    context.request.enumerationAction = EnumerationAction.REFRESH;
                    handleEnumeration(context);
                    break;
                case REFRESH:
                    context.subStage = StorageEnumStages.GET_STORAGE_ACCOUNTS;
                    handleSubStage(context);
                    break;
                case STOP:
                    logInfo(() -> String.format("Azure storage enumeration will be stopped for %s", enumKey));
                    context.stage = EnumerationStages.FINISHED;
                    handleEnumeration(context);
                    break;
                default:
                    logSevere(() -> String.format("Unknown Azure storage enumeration action %s", context.request.enumerationAction));
                    context.stage = EnumerationStages.ERROR;
                    handleEnumeration(context);
                    break;
            }
            break;
        case FINISHED:
            logInfo(() -> String.format("Azure storage enumeration finished for %s", getEnumKey(context)));
            logStatsInformation(context);
            context.operation.complete();
            context.close();
            break;
        case ERROR:
            logWarning(() -> String.format("Azure storage enumeration error for %s", getEnumKey(context)));
            context.operation.fail(context.error);
            context.close();
            break;
        default:
            String msg = String.format("Unknown Azure storage enumeration stage %s ", context.stage.toString());
            logSevere(() -> msg);
            context.error = new IllegalStateException(msg);
            context.operation.fail(context.error);
            context.close();
    }
}
Also used : AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients)

Example 3 with AzureSdkClients

use of com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients in project photon-model by vmware.

the class AzureInstanceService method handleAllocation.

/**
 * State machine to handle different stages of VM creation/deletion.
 *
 * @see #handleError(AzureInstanceContext, Throwable)
 * @see #handleAllocation(AzureInstanceContext, AzureInstanceStage)
 */
private void handleAllocation(AzureInstanceContext ctx) {
    logInfo("Azure instance management at stage %s for [%s]", ctx.stage, ctx.vmName);
    try {
        switch(ctx.stage) {
            case CLIENT:
                if (ctx.azureSdkClients == null) {
                    ctx.azureSdkClients = new AzureSdkClients(ctx.endpointAuth);
                }
                switch(ctx.computeRequest.requestType) {
                    case CREATE:
                        logInfo("Provisioning [%s] for compute %s", ctx.vmName, ctx.resourceReference.getPath());
                        handleAllocation(ctx, AzureInstanceStage.CHILDAUTH);
                        break;
                    case VALIDATE_CREDENTIALS:
                        validateAzureCredentials(ctx);
                        break;
                    case DELETE:
                        handleAllocation(ctx, AzureInstanceStage.DELETE);
                        break;
                    default:
                        throw new IllegalStateException("Unknown compute request type: " + ctx.computeRequest.requestType);
                }
                break;
            case CHILDAUTH:
                getChildAuth(ctx, AzureInstanceStage.VMDISKS);
                break;
            case VMDISKS:
                getVMDisks(ctx, AzureInstanceStage.INIT_RES_GROUP);
                break;
            case INIT_RES_GROUP:
                createResourceGroup(ctx, AzureInstanceStage.INIT_AVAILABILITY_SET);
                break;
            case INIT_AVAILABILITY_SET:
                createAvailabilitySet(ctx, AzureInstanceStage.GET_IMAGE);
                break;
            case GET_IMAGE:
                getImageSource(ctx).whenComplete(thenAllocation(ctx, AzureInstanceStage.INIT_STORAGE));
                break;
            case INIT_STORAGE:
                createStorageAccount(ctx, AzureInstanceStage.POPULATE_NIC_CONTEXT);
                break;
            case POPULATE_NIC_CONTEXT:
                ctx.populateContext().whenComplete(thenAllocation(ctx, AzureInstanceStage.CREATE_NETWORKS));
                break;
            case CREATE_NETWORKS:
                // Create Azure networks, PIPs and NSGs referred by NIC states
                createNetworkIfNotExist(ctx, AzureInstanceStage.CREATE_PUBLIC_IPS);
                break;
            case CREATE_PUBLIC_IPS:
                createPublicIPs(ctx, AzureInstanceStage.CREATE_SECURITY_GROUPS);
                break;
            case CREATE_SECURITY_GROUPS:
                createSecurityGroupsIfNotExist(ctx, AzureInstanceStage.CREATE_NICS);
                break;
            case CREATE_NICS:
                createNICs(ctx, AzureInstanceStage.GENERATE_VM_ID);
                break;
            case GENERATE_VM_ID:
                generateVmId(ctx, AzureInstanceStage.CREATE);
                break;
            case CREATE:
                // Finally provision the VM
                createVM(ctx, AzureInstanceStage.CREATE_PERSISTENT_DISKS);
                break;
            case ENABLE_MONITORING:
                // TODO VSYM-620: Enable monitoring on Azure VMs
                enableMonitoring(ctx, AzureInstanceStage.GET_STORAGE_KEYS);
                break;
            case CREATE_PERSISTENT_DISKS:
                createPersistentDisks(ctx, AzureInstanceStage.UPDATE_COMPUTE_STATE_DETAILS);
                break;
            case UPDATE_COMPUTE_STATE_DETAILS:
                updateComputeStateDetails(ctx, AzureInstanceStage.GET_STORAGE_KEYS);
                break;
            case GET_STORAGE_KEYS:
                getStorageKeys(ctx, AzureInstanceStage.FINISHED);
                break;
            case DELETE:
                deleteVM(ctx, AzureInstanceStage.FINISHED);
                break;
            case FINISHED:
                // This is the ultimate exit point with success of the state machine
                finishWithSuccess(ctx);
                break;
            case ERROR:
                // This is the ultimate exit point with error of the state machine
                errorHandler(ctx);
                break;
            default:
                throw new IllegalStateException("Unknown stage: " + ctx.stage);
        }
    } catch (Throwable e) {
        // NOTE: Do not use handleError(err) cause that might result in endless recursion.
        ctx.error = e;
        errorHandler(ctx);
    }
}
Also used : AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients)

Example 4 with AzureSdkClients

use of com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method handleEnumerationImpl.

private void handleEnumerationImpl(EnumerationContext ctx) {
    switch(ctx.stage) {
        case CLIENT:
            if (ctx.azureSdkClients == null) {
                ctx.azureSdkClients = new AzureSdkClients(ctx.endpointAuth);
            }
            ctx.stage = EnumerationStages.ENUMERATE;
            handleEnumeration(ctx);
            break;
        case ENUMERATE:
            switch(ctx.request.enumerationAction) {
                case START:
                    logInfo(() -> String.format("Launching Azure compute enumeration for %s", ctx.request.getEnumKey()));
                    ctx.enumerationStartTimeInMicros = Utils.getNowMicrosUtc();
                    ctx.request.enumerationAction = EnumerationAction.REFRESH;
                    handleEnumeration(ctx);
                    break;
                case REFRESH:
                    ctx.subStage = ComputeEnumerationSubStages.COLLECT_REGIONS;
                    handleSubStage(ctx);
                    break;
                case STOP:
                    logInfo(() -> String.format("Azure compute enumeration will be stopped for %s", ctx.request.getEnumKey()));
                    ctx.stage = EnumerationStages.FINISHED;
                    handleEnumeration(ctx);
                    break;
                default:
                    logSevere(() -> String.format("Unknown Azure enumeration action %s", ctx.request.enumerationAction));
                    ctx.stage = EnumerationStages.ERROR;
                    handleEnumeration(ctx);
                    break;
            }
            break;
        case FINISHED:
            logInfo(() -> String.format("Azure compute enumeration finished for %s", ctx.request.getEnumKey()));
            ctx.operation.complete();
            ctx.close();
            break;
        case ERROR:
            logWarning(() -> String.format("Azure compute enumeration error for %s, Failed due to %s", ctx.request.getEnumKey(), Utils.toString(ctx.error)));
            ctx.operation.fail(ctx.error);
            ctx.close();
            break;
        default:
            String msg = String.format("Unknown Azure compute enumeration stage %s ", ctx.stage.toString());
            handleError(ctx, new IllegalStateException(msg));
    }
}
Also used : AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients)

Example 5 with AzureSdkClients

use of com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients in project photon-model by vmware.

the class AzureComputeHostStorageStatsGatherer method handleStorageMetricDiscovery.

/**
 * Calculates the storage metrics based on the storage accounts received from the remote
 * endpoint.
 * @param dataHolder The local service context that has all the information needed to
 * states in the local system.
 */
private void handleStorageMetricDiscovery(AzureStorageStatsDataHolder dataHolder) {
    switch(dataHolder.stage) {
        case GET_COMPUTE_HOST:
            getComputeHost(dataHolder, StorageMetricsStages.GET_PARENT_AUTH);
            break;
        case GET_PARENT_AUTH:
            getParentAuth(dataHolder, StorageMetricsStages.GET_CLIENT);
            break;
        case GET_CLIENT:
            try {
                if (dataHolder.azureClients == null) {
                    dataHolder.azureClients = new AzureSdkClients(dataHolder.parentAuth);
                }
            } catch (Throwable e) {
                logSevere(e);
                dataHolder.error = e;
                dataHolder.stage = StorageMetricsStages.ERROR;
                handleStorageMetricDiscovery(dataHolder);
                return;
            }
            dataHolder.stage = StorageMetricsStages.GET_STORAGE_ACCOUNTS;
            handleStorageMetricDiscovery(dataHolder);
            break;
        case GET_STORAGE_ACCOUNTS:
            getStorageAccounts(dataHolder, StorageMetricsStages.CALCULATE_METRICS);
            break;
        case CALCULATE_METRICS:
            getBlobUsedBytesAsync(dataHolder, StorageMetricsStages.FINISHED);
            break;
        case FINISHED:
            dataHolder.azureStorageStatsOperation.setBody(dataHolder.statsResponse);
            dataHolder.azureStorageStatsOperation.complete();
            dataHolder.close();
            logInfo(() -> String.format("Storage utilization stats collection complete for compute" + " host %s", dataHolder.computeHostDesc.id));
            break;
        case ERROR:
            dataHolder.azureStorageStatsOperation.fail(dataHolder.error);
            dataHolder.close();
            logWarning(() -> String.format("Storage utilization stats collection failed for compute" + " host %s", dataHolder.computeHostDesc.id));
            break;
        default:
    }
}
Also used : AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients)

Aggregations

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