Search in sources :

Example 1 with StorageAccountResultList

use of com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccountResultList 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 2 with StorageAccountResultList

use of com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccountResultList 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)2 StorageException (com.microsoft.azure.storage.StorageException)2 StorageAccount (com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccount)2 StorageAccountResultList (com.vmware.photon.controller.model.adapters.azure.model.storage.StorageAccountResultList)2 Operation (com.vmware.xenon.common.Operation)2 URI (java.net.URI)2 AZURE_CORE_MANAGEMENT_URI (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_CORE_MANAGEMENT_URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1