Search in sources :

Example 6 with StorageAccount

use of com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccount in project photon-model by vmware.

the class AzureStorageEnumerationAdapterService method getStorageAccounts.

/**
 * Retrieve storage accounts from Azure.
 */
private void getStorageAccounts(StorageEnumContext context, StorageEnumStages next) {
    String uriStr = context.enumNextPageLink;
    URI uri;
    if (uriStr == null) {
        uriStr = AdapterUriUtil.expandUriPathTemplate(LIST_STORAGE_ACCOUNTS, context.endpointAuth.userLink);
        uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(uriStr), QUERY_PARAM_API_VERSION, STORAGE_ACCOUNT_REST_API_VERSION);
    } else {
        uri = UriUtils.buildUri(uriStr);
    }
    context.storageAccountsToUpdateCreate.clear();
    context.storageAccountBlobUriMap.clear();
    context.storageAccountMap.clear();
    Operation operation = Operation.createGet(uri);
    operation.addRequestHeader(Operation.ACCEPT_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
    operation.addRequestHeader(Operation.CONTENT_TYPE_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
    try {
        operation.addRequestHeader(Operation.AUTHORIZATION_HEADER, AUTH_HEADER_BEARER_PREFIX + context.azureSdkClients.credentials.getToken(AzureUtils.getAzureBaseUri()));
    } catch (Exception ex) {
        this.handleError(context, ex);
        return;
    }
    operation.setCompletion((op, er) -> {
        if (er != null) {
            op.complete();
            handleError(context, er);
            return;
        }
        StorageAccountResultList results = op.getBody(StorageAccountResultList.class);
        op.complete();
        List<StorageAccount> storageAccounts = results.value;
        context.apiStorageAccountsCount += results.value.size();
        // If there are no storage accounts in Azure move to storage account cleanup.
        if (storageAccounts == null || storageAccounts.size() == 0) {
            context.subStage = StorageEnumStages.DISASSOCIATE_STORAGE_DESCRIPTIONS;
            handleSubStage(context);
            return;
        }
        context.enumNextPageLink = results.nextLink;
        logFine(() -> String.format("Retrieved %d storage accounts from Azure", storageAccounts.size()));
        for (StorageAccount storageAccount : storageAccounts) {
            String id = canonizeId(storageAccount.id);
            storageAccount.id = id;
            context.storageAccountsToUpdateCreate.put(id, storageAccount);
            context.storageAccountIds.add(id);
            context.storageAccountMap.put(id, storageAccount);
        }
        logFine(() -> String.format("Processing %d storage accounts", context.storageAccountIds.size()));
        context.subStage = next;
        handleSubStage(context);
    });
    sendRequest(operation);
    context.apiListStorageAccounts++;
}
Also used : CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) StorageAccount(com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccount) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) StorageAccountResultList(com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccountResultList) StorageException(com.microsoft.azure.storage.StorageException) UnknownHostException(java.net.UnknownHostException) URISyntaxException(java.net.URISyntaxException)

Example 7 with StorageAccount

use of com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccount in project photon-model by vmware.

the class AzureComputeHostStorageStatsGatherer method getStorageAccounts.

private void getStorageAccounts(AzureStorageStatsDataHolder statsData, StorageMetricsStages next) {
    String uriStr = AdapterUriUtil.expandUriPathTemplate(LIST_STORAGE_ACCOUNTS, statsData.parentAuth.userLink);
    URI uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(uriStr), QUERY_PARAM_API_VERSION, STORAGE_ACCOUNT_REST_API_VERSION);
    Operation operation = Operation.createGet(uri);
    operation.addRequestHeader(Operation.ACCEPT_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
    operation.addRequestHeader(Operation.CONTENT_TYPE_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
    try {
        operation.addRequestHeader(Operation.AUTHORIZATION_HEADER, AUTH_HEADER_BEARER_PREFIX + statsData.azureClients.credentials.getToken(AZURE_CORE_MANAGEMENT_URI));
    } catch (Exception ex) {
        this.handleError(statsData, ex);
        return;
    }
    operation.setCompletion((op, er) -> {
        if (er != null) {
            handleError(statsData, er);
            return;
        }
        StorageAccountResultList results = op.getBody(StorageAccountResultList.class);
        List<StorageAccount> storageAccounts = results.value;
        // If there are no storage accounts in Azure, the are no bytes being used/billed
        if (storageAccounts == null || storageAccounts.size() == 0) {
            statsData.stage = StorageMetricsStages.FINISHED;
            handleStorageMetricDiscovery(statsData);
            return;
        }
        logFine(() -> String.format("Retrieved %d storage accounts from Azure", storageAccounts.size()));
        for (StorageAccount storageAccount : storageAccounts) {
            statsData.storageAccounts.put(storageAccount.id, storageAccount);
        }
        logFine(() -> String.format("Processing %d storage accounts", statsData.storageAccounts.size()));
        statsData.stage = next;
        handleStorageMetricDiscovery(statsData);
    });
    sendRequest(operation);
}
Also used : CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) StorageAccount(com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccount) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) AZURE_CORE_MANAGEMENT_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_CORE_MANAGEMENT_URI) StorageAccountResultList(com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccountResultList) StorageException(com.microsoft.azure.storage.StorageException)

Aggregations

CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)7 StorageAccount (com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccount)7 StorageException (com.microsoft.azure.storage.StorageException)6 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)5 Azure (com.microsoft.azure.management.Azure)4 StorageAccountInner (com.microsoft.azure.management.storage.implementation.StorageAccountInner)4 StorageAccountListKeysResultInner (com.microsoft.azure.management.storage.implementation.StorageAccountListKeysResultInner)4 StorageAccountsInner (com.microsoft.azure.management.storage.implementation.StorageAccountsInner)4 ResultContinuation (com.microsoft.azure.storage.ResultContinuation)4 ResultSegment (com.microsoft.azure.storage.ResultSegment)4 StorageCredentials (com.microsoft.azure.storage.StorageCredentials)4 StorageErrorCode (com.microsoft.azure.storage.StorageErrorCode)4 BlobListingDetails (com.microsoft.azure.storage.blob.BlobListingDetails)4 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)4 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)4 ContainerListingDetails (com.microsoft.azure.storage.blob.ContainerListingDetails)4 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)4 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)4 COMPUTE_HOST_LINK_PROP_NAME (com.vmware.photon.controller.model.ComputeProperties.COMPUTE_HOST_LINK_PROP_NAME)4 UriPaths (com.vmware.photon.controller.model.UriPaths)4