use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method disassociateDiskStates.
/*
* Disassociate local disk states that no longer exist in Azure
*
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the blob discovered as part of get blob call.
*
* A disassociate on a disk state is invoked only if it meets two criteria: - Timestamp older
* than current enumeration cycle. - blob is not present on Azure.
*/
private void disassociateDiskStates(StorageEnumContext context, StorageEnumStages next) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_COMPUTE_HOST_LINK, context.parentCompute.documentSelfLink).addRangeClause(DiskState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(context.enumerationStartTimeInMicros));
Query.Builder typeFilterQuery = Query.Builder.create(Occurance.MUST_OCCUR);
Query blobFilter = Query.Builder.create(Occurance.SHOULD_OCCUR).addFieldClause(AZURE_STORAGE_TYPE, AZURE_STORAGE_BLOBS).build();
QueryTask.Query diskFilter = QueryTask.Query.Builder.create(QueryTask.Query.Occurance.SHOULD_OCCUR).addFieldClause(AZURE_STORAGE_TYPE, AZURE_STORAGE_DISKS).build();
typeFilterQuery.addClause(blobFilter);
typeFilterQuery.addClause(diskFilter);
qBuilder.addClause(typeFilterQuery.build());
QueryByPages<DiskState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), DiskState.class, context.parentCompute.tenantLinks, context.request.endpointLink).setMaxPageSize(getQueryResultLimit());
queryLocalStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
List<DeferredResult<Operation>> ops = new ArrayList<>();
queryLocalStates.queryDocuments(ds -> {
if (context.blobIds.contains(ds.id)) {
return;
}
ops.add(disassociateIfNotAttachedToCompute(context, ds));
}).thenCompose(r -> DeferredResult.allOf(ops)).whenComplete((r, e) -> {
logFine(() -> "Finished disassociation of disk states for Azure");
context.subStage = next;
handleSubStage(context);
});
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureInstanceService method updateNicStates.
/**
* Update {@code computeState.nicState[i].address} with Azure NICs' private IP.
*/
private DeferredResult<AzureInstanceContext> updateNicStates(AzureInstanceContext ctx) {
if (ctx.nics == null || ctx.nics.isEmpty()) {
// Do nothing.
return DeferredResult.completed(ctx);
}
List<DeferredResult<Void>> updateNICsDR = new ArrayList<>(ctx.nics.size());
for (AzureNicContext nicCtx : ctx.nics) {
if (nicCtx.nic == null) {
continue;
}
final NetworkInterfaceState nicStateToUpdate = new NetworkInterfaceState();
nicStateToUpdate.id = nicCtx.nic.id();
nicStateToUpdate.documentSelfLink = nicCtx.nicStateWithDesc.documentSelfLink;
if (nicCtx.nic.ipConfigurations() != null && !nicCtx.nic.ipConfigurations().isEmpty()) {
nicStateToUpdate.address = nicCtx.nic.ipConfigurations().get(0).privateIPAddress();
}
Operation updateNicOp = Operation.createPatch(ctx.service, nicStateToUpdate.documentSelfLink).setBody(nicStateToUpdate);
DeferredResult<Void> updateNicDR = ctx.service.sendWithDeferredResult(updateNicOp).thenAccept(ignored -> logFine(() -> String.format("Updating NIC state [%s] with Private IP [%s]: SUCCESS", nicCtx.nic.name(), nicStateToUpdate.address)));
updateNICsDR.add(updateNicDR);
}
return DeferredResult.allOf(updateNICsDR).thenApply(ignored -> ctx);
}
use of com.vmware.xenon.common.Operation 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.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureInstanceService method enableMonitoring.
private void enableMonitoring(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
Operation readFile = Operation.createGet(null).setCompletion((o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
AzureDiagnosticSettings azureDiagnosticSettings = o.getBody(AzureDiagnosticSettings.class);
String vmName = ctx.vmName;
String azureInstanceId = ctx.vmId;
String storageAccountName = ctx.storageAccountName;
// Replace the resourceId and storageAccount keys with correct values
azureDiagnosticSettings.getProperties().getPublicConfiguration().getDiagnosticMonitorConfiguration().getMetrics().setResourceId(azureInstanceId);
azureDiagnosticSettings.getProperties().getPublicConfiguration().setStorageAccount(storageAccountName);
ApplicationTokenCredentials credentials = ctx.azureSdkClients.credentials;
URI uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(UriUtils.buildUri(AzureUtils.getAzureBaseUri()), azureInstanceId, AzureConstants.DIAGNOSTIC_SETTING_ENDPOINT, AzureConstants.DIAGNOSTIC_SETTING_AGENT), AzureConstants.QUERY_PARAM_API_VERSION, AzureConstants.DIAGNOSTIC_SETTING_API_VERSION);
Operation operation = Operation.createPut(uri);
operation.setBody(azureDiagnosticSettings);
operation.addRequestHeader(Operation.ACCEPT_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
operation.addRequestHeader(Operation.CONTENT_TYPE_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
try {
operation.addRequestHeader(Operation.AUTHORIZATION_HEADER, AzureConstants.AUTH_HEADER_BEARER_PREFIX + credentials.getToken(AzureUtils.getAzureBaseUri()));
} catch (Exception ex) {
handleError(ctx, ex);
return;
}
logFine(() -> String.format("Enabling monitoring on the VM [%s]", vmName));
operation.setCompletion((op, er) -> {
if (er != null) {
handleError(ctx, er);
return;
}
logFine(() -> String.format("Successfully enabled monitoring on the VM [%s]", vmName));
handleAllocation(ctx, nextStage);
});
sendRequest(operation);
});
String fileUri = getClass().getResource(AzureConstants.DIAGNOSTIC_SETTINGS_JSON_FILE_NAME).getFile();
File jsonPayloadFile = new File(fileUri);
try {
FileUtils.readFileAndComplete(readFile, jsonPayloadFile);
} catch (Exception e) {
handleError(ctx, e);
}
}
use of com.vmware.xenon.common.Operation 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);
}
Aggregations