use of com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.STORAGE_CONNECTION_STRING 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.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.STORAGE_CONNECTION_STRING 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;
}
use of com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.STORAGE_CONNECTION_STRING in project photon-model by vmware.
the class TestAzureDeleteTask method testProvisionWithPersistentUnmanagedDisk.
/**
* Creates a Azure unmanaged instance with persist options for disks. Checks if VHD blob cleanup happens according to the persist flag.
*/
@Test
public void testProvisionWithPersistentUnmanagedDisk() throws Throwable {
BaseComputeInstanceContext.ImageSource imageSource = createImageSource(getHost(), this.endpointState, IMAGE_REFERENCE);
// Create a Azure VM compute resource with 2 additional disks.
int numberOfAdditionalDisks = 3;
AzureTestUtil.VMResourceSpec vmResourceSpec = new AzureTestUtil.VMResourceSpec(getHost(), this.computeHost, this.endpointState, azureVMName).withImageSource(imageSource).withNicSpecs(DEFAULT_NIC_SPEC).withNumberOfAdditionalDisks(numberOfAdditionalDisks).withPersistentDisks(AzureTestUtil.VMResourceSpec.PersistentDisks.SOME).withExistingStorageAccount(storageAccountName, resourceGroupName).withManagedDisk(false);
// create Azure VM compute resource.
this.vmState = createVMResourceFromSpec(vmResourceSpec);
kickOffProvisionTask();
List<DiskService.DiskState> diskStates = this.vmState.diskLinks.stream().map(diskLink -> getHost().getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(getHost(), diskLink))).collect(Collectors.toList());
int computeStatesToRemain = 1;
deleteVMs(getHost(), this.vmState.documentSelfLink, this.isMock, computeStatesToRemain);
// assertions
if (!this.isMock) {
Azure az = getAzureSdkClients().getAzureClient();
String key = az.storageAccounts().getByResourceGroup(resourceGroupName, storageAccountName).getKeys().get(0).value();
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.parse(String.format(STORAGE_CONNECTION_STRING, storageAccountName, key));
CloudBlobClient client = cloudStorageAccount.createCloudBlobClient();
CloudBlobContainer container = client.getContainerReference("vhds");
for (DiskService.DiskState diskState : diskStates) {
String vhdBlobName = diskState.id.substring(diskState.id.lastIndexOf("/") + 1);
CloudPageBlob blob = container.getPageBlobReference(vhdBlobName);
assertEquals("Disk VHD blob should persist", diskState.persistent, blob.exists());
}
}
}
Aggregations