use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureInstanceService method createAvailabilitySet.
private void createAvailabilitySet(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
String availabilitySetName = AzureUtils.getAvailabilitySetName(ctx);
String msg = "Creating Azure Availability Set [" + availabilitySetName + "] for [" + ctx.vmName + "] VM";
AvailabilitySetSkuTypes skuType = ctx.useManagedDisks() ? AvailabilitySetSkuTypes.MANAGED : AvailabilitySetSkuTypes.UNMANAGED;
AvailabilitySet.DefinitionStages.WithCreate availabilitySetDefinition = ctx.azureSdkClients.getComputeManager().availabilitySets().define(availabilitySetName).withRegion(ctx.child.description.regionId).withExistingResourceGroup(ctx.resourceGroup.name()).withSku(skuType);
AzureDeferredResultServiceCallbackWithRetry<AvailabilitySet> callback = new AzureDeferredResultServiceCallbackWithRetry<AvailabilitySet>(this, msg) {
@Override
protected DeferredResult<AvailabilitySet> consumeSuccess(AvailabilitySet availabilitySet) {
logInfo("Successfully created AvailabilitySet: " + availabilitySet.name());
ctx.availabilitySet = availabilitySet;
return DeferredResult.completed(availabilitySet);
}
@Override
protected Runnable retryServiceCall(ServiceCallback<AvailabilitySet> retryCallback) {
return () -> availabilitySetDefinition.createAsync(retryCallback);
}
};
availabilitySetDefinition.createAsync(callback);
callback.toDeferredResult().thenApply(ignore -> ctx).whenComplete(thenAllocation(ctx, nextStage, NETWORK_NAMESPACE));
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureInstanceService method attachPersistentDisksToVM.
private DeferredResult<AzureInstanceContext> attachPersistentDisksToVM(AzureInstanceContext ctx) {
// Attach all the persistent disks to the VM
DeferredResult<AzureInstanceContext> dr = new DeferredResult<>();
VirtualMachine.Update updateVM = ctx.virtualMachine.update();
for (Disk disk : ctx.persistentDisks) {
updateVM = updateVM.withExistingDataDisk(disk);
}
rx.Observable observable = updateVM.applyAsync();
observable.doOnNext(resource -> {
if (resource instanceof VirtualMachine) {
// update both the inner VM and the VirtualMachine objects in context
ctx.virtualMachine = (VirtualMachine) resource;
ctx.provisionedVm = ((VirtualMachine) resource).inner();
}
}).doOnCompleted(AzureUtils.injectOperationContext(() -> {
// Done with attaching disks. Completing DR.
getHost().log(Level.INFO, "Attached persistent " + ctx.persistentDisks.size() + " disks" + " to vm " + ctx.provisionedVm.name());
dr.complete(ctx);
})).doOnError(AzureUtils.injectOperationContext(dr::fail)).publish().connect();
return dr;
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureInstanceService method createPublicIPs.
private void createPublicIPs(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
if (ctx.nics.isEmpty()) {
handleAllocation(ctx, nextStage);
return;
}
AzureNicContext nicCtx = ctx.getPrimaryNic();
// For now if not specified default to TRUE!
if (nicCtx.nicStateWithDesc.description.assignPublicIpAddress == null) {
nicCtx.nicStateWithDesc.description.assignPublicIpAddress = Boolean.TRUE;
}
if (nicCtx.nicStateWithDesc.description.assignPublicIpAddress == Boolean.FALSE) {
// Do nothing in this method -> proceed to next stage.
handleAllocation(ctx, nextStage);
return;
}
PublicIPAddressesInner azureClient = getNetworkManagementClientImpl(ctx).publicIPAddresses();
final PublicIPAddressInner publicIPAddress = newAzurePublicIPAddress(ctx, nicCtx);
final String publicIPName = ctx.vmName + "-pip";
final String publicIPRGName = ctx.resourceGroup.name();
String msg = "Creating Azure Public IP [" + publicIPName + "] for [" + ctx.vmName + "] VM";
AzureProvisioningCallback<PublicIPAddressInner> handler = new AzureProvisioningCallback<PublicIPAddressInner>(this, msg) {
@Override
protected DeferredResult<PublicIPAddressInner> consumeProvisioningSuccess(PublicIPAddressInner publicIP) {
nicCtx.publicIP = publicIP;
return DeferredResult.completed(publicIP);
}
@Override
protected String getProvisioningState(PublicIPAddressInner publicIP) {
return publicIP.provisioningState();
}
@Override
protected Runnable checkProvisioningStateCall(ServiceCallback<PublicIPAddressInner> checkProvisioningStateCallback) {
return () -> azureClient.getByResourceGroupAsync(publicIPRGName, publicIPName, null, /* expand */
checkProvisioningStateCallback);
}
};
azureClient.createOrUpdateAsync(publicIPRGName, publicIPName, publicIPAddress, handler);
handler.toDeferredResult().thenApply(ignore -> ctx).whenComplete(thenAllocation(ctx, nextStage, NETWORK_NAMESPACE));
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureInstanceService method fetchDiskStatesToDelete.
private DeferredResult<AzureInstanceContext> fetchDiskStatesToDelete(AzureInstanceContext ctx) {
// fetch ComputeState and Get disk states
DeferredResult<AzureInstanceContext> dr = new DeferredResult<>();
Operation op = Operation.createGet(ctx.computeRequest.resourceReference);
op.setCompletion((o, e) -> {
if (e != null) {
dr.fail(new Throwable("Unable to fetch compute state for deleted VM. " + e.getMessage()));
return;
}
ComputeState cs = o.getBody(ComputeState.class);
List<Operation> fetchAllVMDisks = new ArrayList<>();
for (String diskLink : cs.diskLinks) {
fetchAllVMDisks.add(Operation.createGet(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(getHost(), diskLink))).setReferer(getUri()));
}
OperationJoin.create(fetchAllVMDisks).setCompletion((ops, exc) -> {
if (exc == null || exc.size() == 0) {
// assign boot disk and data disk to context
ctx.dataDiskStates = new ArrayList<>();
for (Operation diskGet : ops.values()) {
DiskService.DiskStateExpanded disk = diskGet.getBody(DiskService.DiskStateExpanded.class);
if (disk.bootOrder != null && disk.bootOrder == 1) {
ctx.bootDiskState = disk;
} else {
ctx.dataDiskStates.add(disk);
}
}
dr.complete(ctx);
} else {
String stacktrace = Utils.toString(exc);
dr.fail(new Throwable(String.format("Unable to fetch disks to delete for %s. FAILED with: %s", cs.name, stacktrace)));
return;
}
}).sendWith(getHost());
});
sendRequest(op);
return dr;
}
use of com.vmware.xenon.common.DeferredResult 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