Search in sources :

Example 1 with AzureDeferredResultServiceCallback

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

the class AzureInstanceContext method getNetworks.

/**
 * For every NIC lookup associated Azure Subnets as specified by
 * {@code AzureNicContext.networkState.name} and {@code AzureNicContext.subnetState.name}. If
 * any of the subnets is not found leave the {@link AzureNicContext#subnet} as null and proceed
 * without an exception.
 */
private DeferredResult<AzureInstanceContext> getNetworks(AzureInstanceContext context) {
    if (context.nics.isEmpty()) {
        return DeferredResult.completed(context);
    }
    SubnetsInner azureClient = service().getNetworkManagementClientImpl(context).subnets();
    List<DeferredResult<SubnetInner>> getSubnetDRs = context.nics.stream().filter(nicCtx -> nicCtx.networkRGState != null).map(nicCtx -> {
        String msg = "Getting Azure Subnet [" + nicCtx.networkRGState.name + "/" + nicCtx.networkState.name + "/" + nicCtx.subnetState.name + "] for [" + nicCtx.nicStateWithDesc.name + "] NIC for [" + context.vmName + "] VM";
        AzureDeferredResultServiceCallback<SubnetInner> handler = new AzureDeferredResultServiceCallback<SubnetInner>(service(), msg) {

            @Override
            protected DeferredResult<SubnetInner> consumeSuccess(SubnetInner subnet) {
                nicCtx.subnet = subnet;
                return DeferredResult.completed(subnet);
            }
        };
        azureClient.getAsync(nicCtx.networkRGState.name, nicCtx.networkState.name, nicCtx.subnetState.name, null, /* expand */
        handler);
        return handler.toDeferredResult();
    }).collect(Collectors.toList());
    return DeferredResult.allOf(getSubnetDRs).handle((all, exc) -> {
        if (exc != null) {
            String msg = String.format("Error getting Subnets from Azure for [%s] VM.", context.child.name);
            throw new IllegalStateException(msg, exc);
        }
        return context;
    });
}
Also used : VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ImageReferenceInner(com.microsoft.azure.management.compute.implementation.ImageReferenceInner) ResourceGroupInner(com.microsoft.azure.management.resources.implementation.ResourceGroupInner) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) Level(java.util.logging.Level) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) PublicIPAddressInner(com.microsoft.azure.management.network.implementation.PublicIPAddressInner) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) AvailabilitySet(com.microsoft.azure.management.compute.AvailabilitySet) SubnetInner(com.microsoft.azure.management.network.implementation.SubnetInner) DiskConfiguration(com.vmware.photon.controller.model.resources.ImageService.ImageState.DiskConfiguration) AvailabilitySetInner(com.microsoft.azure.management.compute.implementation.AvailabilitySetInner) Collectors(java.util.stream.Collectors) Disk(com.microsoft.azure.management.compute.Disk) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) BaseComputeInstanceContext(com.vmware.photon.controller.model.adapters.util.instance.BaseComputeInstanceContext) AzureSecurityGroupUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureSecurityGroupUtils) SubnetsInner(com.microsoft.azure.management.network.implementation.SubnetsInner) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) NetworkSecurityGroupsInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupsInner) DeferredResult(com.vmware.xenon.common.DeferredResult) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) NetworkSecurityGroupInner(com.microsoft.azure.management.network.implementation.NetworkSecurityGroupInner) StorageAccountInner(com.microsoft.azure.management.storage.implementation.StorageAccountInner) NetworkInterfaceInner(com.microsoft.azure.management.network.implementation.NetworkInterfaceInner) DiskService(com.vmware.photon.controller.model.resources.DiskService) SubnetsInner(com.microsoft.azure.management.network.implementation.SubnetsInner) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) SubnetInner(com.microsoft.azure.management.network.implementation.SubnetInner) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 2 with AzureDeferredResultServiceCallback

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

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

the class AzureStorageEnumerationAdapterService method createStorageDescription.

private DeferredResult<StorageDescription> createStorageDescription(StorageEnumContext context, StorageAccount storageAccount, StorageAccountsInner stOps) {
    String resourceGroupName = getResourceGroupName(storageAccount.id);
    AzureDeferredResultServiceCallback<StorageAccountListKeysResultInner> handler = new Default<>(this, "Load account keys for storage: " + storageAccount.name);
    PhotonModelUtils.runInExecutor(this.executorService, () -> {
        stOps.listKeysAsync(resourceGroupName, storageAccount.name, handler);
    }, handler::failure);
    return handler.toDeferredResult().thenCompose(keys -> AzureUtils.storeKeys(getHost(), keys, context.request.endpointLink, context.parentCompute.tenantLinks)).thenApply(auth -> {
        String connectionString = String.format(STORAGE_CONNECTION_STRING, storageAccount.name, auth.customProperties.get(AZURE_STORAGE_ACCOUNT_KEY1));
        context.storageConnectionStrings.put(storageAccount.id, connectionString);
        return auth;
    }).thenApply(auth -> {
        StorageDescription storageDesc = AzureUtils.constructStorageDescription(context.parentCompute, context.request, storageAccount, auth.documentSelfLink);
        return storageDesc;
    }).thenCompose(sd -> sendWithDeferredResult(Operation.createPost(context.request.buildUri(StorageDescriptionService.FACTORY_LINK)).setBody(sd).setCompletion((o, e) -> {
        if (e != null) {
            logWarning("Unable to store storage description for storage account:[%s], reason: %s", storageAccount.name, Utils.toJsonHtml(e));
        } else {
            StorageDescription storageDescription = o.getBody(StorageDescription.class);
            context.storageDescriptionsForPatching.put(storageDescription.id, storageDescription);
        }
    }), StorageDescription.class));
}
Also used : StorageAccountListKeysResultInner(com.microsoft.azure.management.storage.implementation.StorageAccountListKeysResultInner) STORAGE_ACCOUNT_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.STORAGE_ACCOUNT_REST_API_VERSION) Arrays(java.util.Arrays) QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) AZURE_STORAGE_BLOBS(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_BLOBS) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) StringUtils(org.apache.commons.lang3.StringUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) Azure(com.microsoft.azure.management.Azure) Utils(com.vmware.xenon.common.Utils) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) COMPUTE_HOST_LINK_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.COMPUTE_HOST_LINK_PROP_NAME) EnumSet(java.util.EnumSet) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) AZURE_STORAGE_ACCOUNT_KEY1(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_ACCOUNT_KEY1) StorageAccountListKeysResultInner(com.microsoft.azure.management.storage.implementation.StorageAccountListKeysResultInner) StatelessService(com.vmware.xenon.common.StatelessService) Set(java.util.Set) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) AZURE_STORAGE_CONTAINER_LEASE_STATUS(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_CONTAINER_LEASE_STATUS) TagService(com.vmware.photon.controller.model.resources.TagService) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) AZURE_STORAGE_CONTAINER_LEASE_STATE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_CONTAINER_LEASE_STATE) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceGroupStateType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.ResourceGroupStateType) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) StorageException(com.microsoft.azure.storage.StorageException) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) 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) UriPaths(com.vmware.photon.controller.model.UriPaths) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) StorageErrorCode(com.microsoft.azure.storage.StorageErrorCode) EnumUtils(org.apache.commons.lang3.EnumUtils) AZURE_STORAGE_CONTAINER_LEASE_LAST_MODIFIED(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_CONTAINER_LEASE_LAST_MODIFIED) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) LIST_STORAGE_ACCOUNTS(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_STORAGE_ACCOUNTS) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) ResultSegment(com.microsoft.azure.storage.ResultSegment) EMPTY_STR(com.vmware.photon.controller.model.constants.PhotonModelConstants.EMPTY_STR) ContainerListingDetails(com.microsoft.azure.storage.blob.ContainerListingDetails) UnknownHostException(java.net.UnknownHostException) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ResultContinuation(com.microsoft.azure.storage.ResultContinuation) URISyntaxException(java.net.URISyntaxException) QueryTask(com.vmware.xenon.services.common.QueryTask) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) DEFAULT_DISK_TYPE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_DISK_TYPE) AzureSdkClients(com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) AzureConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.getQueryResultLimit) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AZURE_STORAGE_CONTAINERS(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_CONTAINERS) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) UUID(java.util.UUID) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) Collectors(java.util.stream.Collectors) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) AzureUtils.getResourceGroupName(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getResourceGroupName) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) DiskService(com.vmware.photon.controller.model.resources.DiskService) Default(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) Level(java.util.logging.Level) HashSet(java.util.HashSet) AZURE_STORAGE_TYPE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_TYPE) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) STORAGE_CONNECTION_STRING(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.STORAGE_CONNECTION_STRING) ExecutorService(java.util.concurrent.ExecutorService) StorageAccountResultList(com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccountResultList) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) AZURE_STORAGE_DISKS(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_STORAGE_DISKS) StorageAccountsInner(com.microsoft.azure.management.storage.implementation.StorageAccountsInner) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) StorageCredentials(com.microsoft.azure.storage.StorageCredentials) AzureUtils.canonizeId(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.canonizeId) BlobListingDetails(com.microsoft.azure.storage.blob.BlobListingDetails) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) StorageAccountInner(com.microsoft.azure.management.storage.implementation.StorageAccountInner) StorageAccount(com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccount) OperationJoin(com.vmware.xenon.common.OperationJoin) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) Default(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription)

Example 4 with AzureDeferredResultServiceCallback

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

the class AzureInstanceService method deleteResourceGroup.

private DeferredResult<AzureInstanceContext> deleteResourceGroup(AzureInstanceContext ctx) {
    String msg = "Deleting resource group [" + ctx.resourceGroupName + "]";
    if (ctx.availabilitySetInners == null || ctx.availabilitySetInners.size() == 0 || ctx.availabilitySetInners.get(0).virtualMachines() == null || ctx.availabilitySetInners.get(0).virtualMachines().size() == 0) {
        // there are no other machine in this cluster,  delete the resource group
        ResourceGroupsInner resourceGoups = getResourceManagementClientImpl(ctx).resourceGroups();
        AzureDeferredResultServiceCallback<Void> deleteResourceGroupCallback = new AzureDeferredResultServiceCallback<Void>(this, msg) {

            @Override
            protected DeferredResult<Void> consumeSuccess(Void result) {
                logInfo("Successfully deleted resource group: " + ctx.resourceGroupName);
                return DeferredResult.completed(result);
            }
        };
        resourceGoups.deleteAsync(ctx.resourceGroupName, deleteResourceGroupCallback);
        return deleteResourceGroupCallback.toDeferredResult().thenApply(result -> ctx);
    }
    return DeferredResult.completed(ctx);
}
Also used : AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) ResourceGroupsInner(com.microsoft.azure.management.resources.implementation.ResourceGroupsInner)

Example 5 with AzureDeferredResultServiceCallback

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

the class AzureInstanceService method getPublicIPAddress.

private DeferredResult<AzureInstanceContext> getPublicIPAddress(AzureInstanceContext ctx) {
    if (ctx.getPrimaryNic().publicIP == null) {
        // No public IP address created -> do nothing.
        return DeferredResult.completed(ctx);
    }
    NetworkManagementClientImpl client = getNetworkManagementClientImpl(ctx);
    String msg = "Get public IP address for resource group [" + ctx.resourceGroup.name() + "] and name [" + ctx.getPrimaryNic().publicIP.name() + "].";
    AzureDeferredResultServiceCallback<PublicIPAddressInner> callback = new AzureDeferredResultServiceCallback<PublicIPAddressInner>(ctx.service, msg) {

        @Override
        protected DeferredResult<PublicIPAddressInner> consumeSuccess(PublicIPAddressInner result) {
            ctx.getPrimaryNic().publicIP = result;
            return DeferredResult.completed(result);
        }
    };
    client.publicIPAddresses().getByResourceGroupAsync(ctx.resourceGroup.name(), ctx.getPrimaryNic().publicIP.name(), null, /* expand */
    callback);
    return callback.toDeferredResult().thenApply(ignored -> ctx);
}
Also used : NetworkManagementClientImpl(com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl) AzureDeferredResultServiceCallback(com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback) PublicIPAddressInner(com.microsoft.azure.management.network.implementation.PublicIPAddressInner)

Aggregations

AzureDeferredResultServiceCallback (com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback)8 PublicIPAddressInner (com.microsoft.azure.management.network.implementation.PublicIPAddressInner)3 AzureSdkClients (com.vmware.photon.controller.model.adapters.azure.utils.AzureSdkClients)3 NetworkInterfaceInner (com.microsoft.azure.management.network.implementation.NetworkInterfaceInner)2 NetworkManagementClientImpl (com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl)2 StorageAccountInner (com.microsoft.azure.management.storage.implementation.StorageAccountInner)2 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)2 AzureUtils (com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils)2 DiskService (com.vmware.photon.controller.model.resources.DiskService)2 ResourceGroupState (com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState)2 StorageDescription (com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription)2 DeferredResult (com.vmware.xenon.common.DeferredResult)2 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)2 List (java.util.List)2 Map (java.util.Map)2 Level (java.util.logging.Level)2 Collectors (java.util.stream.Collectors)2 Azure (com.microsoft.azure.management.Azure)1 AvailabilitySet (com.microsoft.azure.management.compute.AvailabilitySet)1 Disk (com.microsoft.azure.management.compute.Disk)1