use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AWSS3StorageEnumerationAdapterService method enumerateS3Buckets.
/**
* Call the listBuckets() method to enumerate S3 buckets.
* AWS SDK does not have an async method for listing buckets, so we use the synchronous method
* in a fixed thread pool for S3 enumeration service.
* If listBuckets() call fails due to unsupported region, we mark the S3 client invalid,
* stop the enumeration flow and patch back to parent.
*/
private void enumerateS3Buckets(S3StorageEnumerationContext aws) {
logInfo(() -> String.format("Running creation enumeration in refresh mode for %s", aws.request.original.resourceReference));
OperationContext operationContext = OperationContext.getOperationContext();
this.executorService.submit(() -> {
try {
List<Bucket> bucketList = aws.amazonS3Client.listBuckets();
for (Bucket bucket : bucketList) {
aws.remoteBucketsByBucketName.put(bucket.getName(), bucket);
}
OperationContext.restoreOperationContext(operationContext);
if (aws.remoteBucketsByBucketName.isEmpty()) {
aws.subStage = S3StorageEnumerationSubStage.DELETE_DISKS;
}
handleReceivedEnumerationData(aws);
} catch (Exception e) {
if (e instanceof AmazonS3Exception && ((AmazonS3Exception) e).getStatusCode() == Operation.STATUS_CODE_FORBIDDEN) {
markClientInvalid(aws);
} else {
logSevere("Exception enumerating S3 buckets for [ex=%s]", e.getMessage());
aws.error = e;
aws.stage = S3StorageEnumerationStages.ERROR;
handleEnumerationRequest(aws);
}
}
});
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AzureInstanceService method deleteVHDBlobsInAzure.
private DeferredResult<Void> deleteVHDBlobsInAzure(AzureInstanceContext ctx) {
if (isManagedDisk().test(ctx.bootDiskState)) {
return DeferredResult.completed(null);
}
String defaultSAName = ctx.bootDiskState.storageDescription.name;
String defaultSAKey = ctx.storageAccountKeysForDisks.get(ctx.bootDiskState.storageDescription.authCredentialsLink);
List<DiskService.DiskStateExpanded> diskStatesToDelete = ctx.dataDiskStates.stream().filter(disk -> disk.persistent == false).collect(Collectors.toList());
if (ctx.bootDiskState.persistent == false) {
diskStatesToDelete.add(ctx.bootDiskState);
}
OperationContext opCtx = OperationContext.getOperationContext();
// Create DR
DeferredResult<Void> dr = new DeferredResult<>();
CompletableFuture.runAsync(() -> {
diskStatesToDelete.forEach(disk -> {
try {
CloudStorageAccount storageAccountOfVHDBlob;
if (disk.storageDescription == null) {
storageAccountOfVHDBlob = CloudStorageAccount.parse(String.format(STORAGE_CONNECTION_STRING, defaultSAName, defaultSAKey));
} else {
String key = ctx.storageAccountKeysForDisks.get(disk.storageDescription.authCredentialsLink);
storageAccountOfVHDBlob = CloudStorageAccount.parse(String.format(STORAGE_CONNECTION_STRING, disk.storageDescription.name, key));
}
CloudBlobClient client = storageAccountOfVHDBlob.createCloudBlobClient();
CloudBlobContainer container = client.getContainerReference("vhds");
String vhdBlobName = disk.id.substring(disk.id.lastIndexOf("/") + 1);
CloudPageBlob blob = container.getPageBlobReference(vhdBlobName);
blob.deleteIfExists();
} catch (URISyntaxException | InvalidKeyException | StorageException e) {
logSevere("Unable to delete VHD file for disk [%s] because of %s ", disk.name, e.toString());
}
});
}, getHost().allocateExecutor(this)).whenComplete((obj, throwable) -> {
OperationContext.restoreOperationContext(opCtx);
dr.complete(obj);
});
return dr;
}
Aggregations