use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method patchVMInstanceDetails.
private DeferredResult<ComputeState> patchVMInstanceDetails(EnumerationContext ctx, VirtualMachinesInner vmOps, ComputeState computeState) {
String resourceGroupName = getResourceGroupName(computeState.id);
String vmName = computeState.name;
AzureDeferredResultServiceCallback<VirtualMachineInner> handler = new Default<>(this, "Load virtual machine instance view:" + vmName);
PhotonModelUtils.runInExecutor(this.executorService, () -> {
vmOps.getByResourceGroupAsync(resourceGroupName, vmName, InstanceViewTypes.INSTANCE_VIEW, handler);
}, handler::failure);
return handler.toDeferredResult().thenApply(vm -> {
for (InstanceViewStatus status : vm.instanceView().statuses()) {
if (computeState.creationTimeMicros == null && status.time() != null) {
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(status.time().getMillis());
}
if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_RUNNING)) {
computeState.powerState = PowerState.ON;
} else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_STOPPED)) {
computeState.powerState = PowerState.OFF;
} else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_DEALLOCATED)) {
computeState.powerState = PowerState.SUSPEND;
}
}
if (computeState.customProperties == null) {
computeState.customProperties = new HashMap<>();
}
computeState.customProperties.put(RESOURCE_GROUP_NAME, resourceGroupName);
computeState.endpointLinks.add(ctx.request.endpointLink);
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
return computeState;
});
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureSecurityGroupService method deleteSecurityGroup.
private DeferredResult<AzureSecurityGroupContext> deleteSecurityGroup(AzureSecurityGroupContext context) {
String rgName = null;
if (context.securityGroupState.customProperties != null) {
rgName = context.securityGroupState.customProperties.get(RESOURCE_GROUP_NAME);
}
rgName = rgName != null ? rgName : String.format("%s-rg", context.securityGroupState.name);
ResourceGroupsInner azureClient = context.azureSdkClients.getResourceManagementClientImpl().resourceGroups();
String msg = "Deleting resource group [" + rgName + "] for [" + context.securityGroupState.name + "] network security group";
AzureDeferredResultServiceCallback<Void> callback = new Default<>(this, msg);
azureClient.deleteAsync(rgName, callback);
return callback.toDeferredResult().thenApply(__ -> context);
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default 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.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureInstanceService method updateDiskStates.
/**
* Update {@code computeState.diskState[i].id} with Azure Disks' VHD URI.
*/
private DeferredResult<AzureInstanceContext> updateDiskStates(AzureInstanceContext ctx) {
if (ctx.provisionedVm == null) {
// Do nothing.
return DeferredResult.completed(ctx);
}
List<DeferredResult<Operation>> diskStateDRs = new ArrayList<>();
// Update boot DiskState with Azure osDisk VHD URI in case of unmanaged disks and
// disks id in case of managed disks
{
final OSDisk azureOsDisk = ctx.provisionedVm.storageProfile().osDisk();
final DiskState diskStateToUpdate = new DiskState();
diskStateToUpdate.documentSelfLink = ctx.bootDiskState.documentSelfLink;
diskStateToUpdate.persistent = ctx.bootDiskState.persistent;
diskStateToUpdate.regionId = ctx.provisionedVm.location();
diskStateToUpdate.endpointLink = ctx.endpoint.documentSelfLink;
AdapterUtils.addToEndpointLinks(diskStateToUpdate, ctx.endpoint.documentSelfLink);
// The actual value being updated
if (ctx.useManagedDisks()) {
diskStateToUpdate.id = azureOsDisk.managedDisk().id();
} else {
diskStateToUpdate.id = AzureUtils.canonizeId(azureOsDisk.vhd().uri());
}
diskStateToUpdate.status = DiskService.DiskStatus.ATTACHED;
Operation updateDiskState = Operation.createPatch(ctx.service, diskStateToUpdate.documentSelfLink).setBody(diskStateToUpdate);
DeferredResult<Operation> updateDR = ctx.service.sendWithDeferredResult(updateDiskState).whenComplete((op, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Updating boot DiskState [%s] with VHD URI [%s]: FAILED with %s", ctx.bootDiskState.name, diskStateToUpdate.id, Utils.toString(exc)));
} else {
logFine(() -> String.format("Updating boot DiskState [%s] with VHD URI [%s]: SUCCESS", ctx.bootDiskState.name, diskStateToUpdate.id));
}
});
diskStateDRs.add(updateDR);
}
for (DataDisk azureDataDisk : ctx.provisionedVm.storageProfile().dataDisks()) {
// Find corresponding DiskState by name
Optional<DiskState> dataDiskOpt = ctx.dataDiskStates.stream().map(DiskState.class::cast).filter(dS -> azureDataDisk.name().equals(dS.name)).findFirst();
Optional<DiskState> externalDiskOpt = ctx.externalDataDisks.stream().map(DiskState.class::cast).filter(dS -> azureDataDisk.name().equals(dS.name)).findFirst();
// update disk state
if (dataDiskOpt.isPresent()) {
diskStateDRs.add(createDiskToUpdate(ctx, dataDiskOpt, azureDataDisk));
} else if (externalDiskOpt.isPresent()) {
diskStateDRs.add(createDiskToUpdate(ctx, externalDiskOpt, azureDataDisk));
} else {
// additional disks were created by the custom image. Will always be of
// managed disk type. Create new disk state.
DiskState diskStateToCreate = new DiskState();
diskStateToCreate.id = azureDataDisk.managedDisk().id();
diskStateToCreate.name = azureDataDisk.name();
diskStateToCreate.regionId = ctx.provisionedVm.location();
diskStateToCreate.customProperties = new HashMap<>();
diskStateToCreate.customProperties.put(DISK_CONTROLLER_NUMBER, String.valueOf(azureDataDisk.lun()));
if (azureDataDisk.managedDisk().storageAccountType() != null) {
diskStateToCreate.customProperties.put(AZURE_MANAGED_DISK_TYPE, azureDataDisk.managedDisk().storageAccountType().toString());
} else {
// set to Standard_LRS default
diskStateToCreate.customProperties.put(AZURE_MANAGED_DISK_TYPE, StorageAccountTypes.STANDARD_LRS.toString());
}
diskStateToCreate.customProperties.put(AZURE_DATA_DISK_CACHING, azureDataDisk.caching().toString());
if (azureDataDisk.diskSizeGB() != null) {
diskStateToCreate.capacityMBytes = azureDataDisk.diskSizeGB() * 1024;
}
diskStateToCreate.status = DiskService.DiskStatus.ATTACHED;
diskStateToCreate.persistent = ctx.bootDiskState.persistent;
diskStateToCreate.endpointLink = ctx.endpoint.documentSelfLink;
AdapterUtils.addToEndpointLinks(diskStateToCreate, ctx.endpoint.documentSelfLink);
Operation createDiskState = Operation.createPost(ctx.service, DiskService.FACTORY_LINK).setBody(diskStateToCreate);
DeferredResult<Operation> createDR = ctx.service.sendWithDeferredResult(createDiskState).whenComplete((op, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Creating data DiskState [%s] with disk id [%s]: FAILED " + "with %s", azureDataDisk.name(), azureDataDisk.managedDisk().id(), Utils.toString(exc)));
} else {
logFine(() -> String.format("Creating data DiskState [%s] with disk id [%s]: SUCCESS", azureDataDisk.name(), azureDataDisk.managedDisk().id()));
// update compute state with data disks present on custom image
ComputeState cs = new ComputeState();
List<String> disksLinks = new ArrayList<>();
DiskState diskState = op.getBody(DiskState.class);
disksLinks.add(diskState.documentSelfLink);
cs.diskLinks = disksLinks;
Operation.CompletionHandler completionHandler = (ox, ex) -> {
if (ex != null) {
handleError(ctx, ex);
return;
}
};
sendRequest(Operation.createPatch(ctx.computeRequest.resourceReference).setBody(cs).setCompletion(completionHandler));
}
});
diskStateDRs.add(createDR);
}
}
return DeferredResult.allOf(diskStateDRs).thenApply(ignored -> ctx);
}
use of com.vmware.photon.controller.model.adapters.azure.utils.AzureDeferredResultServiceCallback.Default in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createInternalTypeTags.
private void createInternalTypeTags(StorageEnumContext context, StorageEnumStages next) {
TagState blobTypeTag = newTagState(TAG_KEY_TYPE, AzureResourceType.azure_blob.name(), false, context.parentCompute.tenantLinks);
TagState vhdTypeTag = newTagState(TAG_KEY_TYPE, AzureResourceType.azure_vhd.name(), false, context.parentCompute.tenantLinks);
Collection<Operation> opCollection = new ArrayList<>();
Operation blobTypeTagOp = Operation.createPost(getHost(), TagService.FACTORY_LINK).setBody(blobTypeTag);
opCollection.add(blobTypeTagOp);
Operation vhdTypeTagOp = Operation.createPost(getHost(), TagService.FACTORY_LINK).setBody(vhdTypeTag);
opCollection.add(vhdTypeTagOp);
OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
} else {
for (Operation taskOp : ops.values()) {
TagState state = taskOp.getBody(TagState.class);
if (EnumUtils.isValidEnum(AzureResourceType.class, state.value)) {
switch(AzureResourceType.valueOf(state.value)) {
case azure_vhd:
context.internalVhdTypeTag = vhdTypeTag.documentSelfLink;
break;
case azure_blob:
context.internalBlobTypeTag = blobTypeTag.documentSelfLink;
break;
default:
break;
}
}
}
}
context.subStage = next;
handleSubStage(context);
}).sendWith(this);
}
Aggregations