use of com.microsoft.azure.management.storage.implementation.StorageAccountListKeysResultInner 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));
}
use of com.microsoft.azure.management.storage.implementation.StorageAccountListKeysResultInner in project photon-model by vmware.
the class AzureInstanceService method getStorageKeys.
/**
* Gets the storage keys from azure and patches the credential state.
*/
private void getStorageKeys(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
if (ctx.reuseExistingStorageAccount() || ctx.useManagedDisks()) {
// no need to get keys as no new storage description was created
handleAllocation(ctx, nextStage);
return;
}
StorageManagementClientImpl client = getStorageManagementClientImpl(ctx);
client.storageAccounts().listKeysAsync(ctx.storageAccountRGName, ctx.storageAccountName, new AzureAsyncCallback<StorageAccountListKeysResultInner>() {
@Override
public void onError(Throwable e) {
handleError(ctx, e);
}
@Override
public void onSuccess(StorageAccountListKeysResultInner result) {
logFine(() -> String.format("Retrieved the storage account keys for storage" + " account [%s]", ctx.storageAccountName));
AuthCredentialsServiceState storageAuth = new AuthCredentialsServiceState();
storageAuth.customProperties = new HashMap<>();
for (StorageAccountKey key : result.keys()) {
storageAuth.customProperties.put(getStorageAccountKeyName(storageAuth.customProperties), key.value());
}
storageAuth.tenantLinks = ctx.parent.tenantLinks;
Operation patchStorageDescriptionWithKeys = Operation.createPost(createInventoryUri(getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(storageAuth).setCompletion((o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
AuthCredentialsServiceState resultAuth = o.getBody(AuthCredentialsServiceState.class);
ctx.storageDescription.authCredentialsLink = resultAuth.documentSelfLink;
Operation patch = Operation.createPatch(UriUtils.buildUri(getHost(), ctx.storageDescription.documentSelfLink)).setBody(ctx.storageDescription).setCompletion(((completedOp, failure) -> {
if (failure != null) {
handleError(ctx, failure);
return;
}
logFine(() -> "Patched storage description.");
handleAllocation(ctx, nextStage);
}));
sendRequest(patch);
});
sendRequest(patchStorageDescriptionWithKeys);
}
});
}
use of com.microsoft.azure.management.storage.implementation.StorageAccountListKeysResultInner in project photon-model by vmware.
the class AzureComputeHostStorageStatsGatherer method getBlobUsedBytesAsync.
private void getBlobUsedBytesAsync(AzureStorageStatsDataHolder statsData, StorageMetricsStages next) {
Runnable getBlobsAsync = () -> {
String metricName = PhotonModelConstants.STORAGE_USED_BYTES;
List<ServiceStats.ServiceStat> statDatapoints = new ArrayList<>();
AtomicInteger accountsCount = new AtomicInteger(statsData.storageAccounts.size());
final List<Throwable> exs = new ArrayList<>();
for (Map.Entry<String, StorageAccount> account : statsData.storageAccounts.entrySet()) {
String resourceGroupName = getResourceGroupName(account.getValue().id);
statsData.azureClients.getAzureClient().storageAccounts().inner().listKeysAsync(resourceGroupName, account.getValue().name, new AzureAsyncCallback<StorageAccountListKeysResultInner>() {
@Override
public void onError(Throwable e) {
handleError(statsData, e);
}
@Override
public void onSuccess(StorageAccountListKeysResultInner result) {
logFine(() -> String.format("Retrieved the storage account keys for" + " storage account [%s].", account.getValue().name));
String storageConnectionString = String.format(STORAGE_CONNECTION_STRING, account.getValue().name, result.keys().get(0).value());
try {
CloudStorageAccount storageAccount = getAzureStorageClient(storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
ResultContinuation nextContainerResults = null;
do {
ResultSegment<CloudBlobContainer> contSegment = blobClient.listContainersSegmented(null, ContainerListingDetails.NONE, QUERY_RESULT_LIMIT, nextContainerResults, null, null);
nextContainerResults = contSegment.getContinuationToken();
for (CloudBlobContainer container : contSegment.getResults()) {
ResultContinuation nextBlobResults = null;
do {
ResultSegment<ListBlobItem> blobsSegment = container.listBlobsSegmented(null, false, EnumSet.noneOf(BlobListingDetails.class), QUERY_RESULT_LIMIT, nextBlobResults, null, null);
nextBlobResults = blobsSegment.getContinuationToken();
for (ListBlobItem blobItem : blobsSegment.getResults()) {
if (blobItem instanceof CloudPageBlob) {
CloudPageBlob pageBlob = (CloudPageBlob) blobItem;
// TODO https://jira-hzn.eng.vmware.com/browse/VSYM-3445
try {
CloudBlob blobSnapshot = pageBlob.createSnapshot();
statsData.snapshots.add(blobSnapshot);
CloudPageBlob pageBlobSnapshot = (CloudPageBlob) blobSnapshot;
ArrayList<PageRange> pages = pageBlobSnapshot.downloadPageRanges();
// https://jira-hzn.eng.vmware.com/browse/VSYM-3355
for (PageRange pageRange : pages) {
statsData.utilizedBytes += pageRange.getEndOffset() - pageRange.getStartOffset();
}
} catch (StorageException e) {
logWarning(() -> String.format("Error getting blob size: [%s]", e.getMessage()));
}
}
}
} while (nextBlobResults != null);
}
} while (nextContainerResults != null);
} catch (Exception e) {
logWarning(() -> String.format("Exception while getting blob used bytes: %s", Utils.toString(e)));
exs.add(e);
} finally {
// in the Azure account
if (statsData.snapshots.size() > 0) {
synchronized (statsData.snapshots) {
Iterator<CloudBlob> snapshotIterator = statsData.snapshots.iterator();
while (snapshotIterator.hasNext()) {
try {
CloudBlob snapshot = snapshotIterator.next();
snapshot.deleteIfExists();
snapshotIterator.remove();
} catch (StorageException e) {
// Best effort to delete all the snapshots
logWarning(() -> String.format("Exception while deleting snapshot: %s", Utils.toString(e)));
}
}
}
}
}
// if all storage accounts were processed, create ServiceStat and finish
if (accountsCount.decrementAndGet() == 0) {
if (!exs.isEmpty()) {
handleError(statsData, exs.iterator().next());
return;
}
if (statsData.utilizedBytes != 0) {
ServiceStats.ServiceStat stat = new ServiceStats.ServiceStat();
stat.latestValue = statsData.utilizedBytes;
stat.sourceTimeMicrosUtc = TimeUnit.MILLISECONDS.toMicros(Utils.getNowMicrosUtc());
stat.unit = PhotonModelConstants.getUnitForMetric(metricName);
statDatapoints.add(stat);
}
statsData.statsResponse.statValues.put(metricName, statDatapoints);
if (statsData.statsResponse.statValues.size() == 1) {
statsData.statsResponse.computeLink = statsData.computeHostDesc.documentSelfLink;
}
statsData.stage = next;
handleStorageMetricDiscovery(statsData);
}
}
});
}
};
PhotonModelUtils.runInExecutor(this.executorService, getBlobsAsync, throwable -> handleError(statsData, throwable));
}
Aggregations