use of com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri in project photon-model by vmware.
the class GCPEnumerationAdapterService method checkLinkAndFinishDeleting.
/**
* The helper function which checks if the deletion is finished.
* If finished, go to Finished sub stage.
* @param ctx The Enumeration Context.
* @param deletionNextPageLink The next deletion page link.
*/
private void checkLinkAndFinishDeleting(EnumerationContext ctx, String deletionNextPageLink) {
if (deletionNextPageLink != null) {
URI nextPageInventoryLinkUri = PhotonModelUriUtils.createInventoryUri(this.getHost(), deletionNextPageLink);
logFine(() -> String.format("Querying page [%s] for resources to be deleted", nextPageInventoryLinkUri));
Operation.createGet(nextPageInventoryLinkUri).setCompletion((o, e) -> deleteQueryCompletionHandler(ctx, o.getBody(QueryTask.class), e)).sendWith(this);
return;
}
logFine(() -> "No compute states match for deletion");
ctx.subStage = EnumerationSubStages.FINISHED;
handleSubStage(ctx);
}
use of com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri in project photon-model by vmware.
the class VSphereAdapterSnapshotService method processNextStepsForDeleteOperation.
private void processNextStepsForDeleteOperation(SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
// Update the isCurrent
if (snapshot.isCurrent && snapshot.parentLink != null) {
logInfo("Updating the parent of the snapshot %s to current", snapshot.name);
SnapshotState parentSnapshot = new SnapshotState();
parentSnapshot.isCurrent = Boolean.TRUE;
context.snapshotOperations.add(Operation.createPatch(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshot.parentLink)).setBody(parentSnapshot).setReferer(getUri()));
}
// Check if the deleted snapshot is the last available snapshot
DeferredResult<Boolean> result = isLastSnapshotForCompute(context);
Operation[] patchComputeOp = new Operation[1];
result.whenComplete((b, e) -> {
if (e != null) {
logSevere(e);
deferredResult.fail(e);
return;
}
if (b) {
ComputeStateWithDescription compute = context.computeDescription;
compute.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, Boolean.FALSE.toString());
// patch compute adding property that it _hasSnapshots
logInfo("Updating the state of compute resource: %s", compute.name);
patchComputeOp[0] = Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.computeLink)).setBody(compute).setReferer(getUri());
context.snapshotOperations.add(patchComputeOp[0]);
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
return;
}
deferredResult.complete(context);
};
context.snapshotOperations.add(Operation.createDelete(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setReferer(getUri()));
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
});
}
use of com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri in project photon-model by vmware.
the class VSphereVirtualMachineEnumerationHelper method updateVm.
static void updateVm(VSphereIncrementalEnumerationService service, ComputeState oldDocument, EnumerationProgress enumerationProgress, VmOverlay vm, boolean fullUpdate) {
ComputeState state;
if (fullUpdate) {
state = makeVmFromResults(enumerationProgress, vm);
} else {
state = makeVmFromChanges(vm);
}
state.documentSelfLink = oldDocument.documentSelfLink;
state.resourcePoolLink = null;
state.lifecycleState = LifecycleState.READY;
state.networkInterfaceLinks = oldDocument.networkInterfaceLinks;
state.diskLinks = oldDocument.diskLinks;
if (oldDocument.tenantLinks == null) {
state.tenantLinks = enumerationProgress.getTenantLinks();
}
service.logFine(() -> String.format("Syncing VM %s", state.documentSelfLink));
if (CollectionUtils.isNotEmpty(state.diskLinks)) {
// Now check how many disks are added / deleted / needs to be updated.
List<Operation> ops = state.diskLinks.stream().map(link -> {
URI diskStateUri = UriUtils.buildUri(service.getHost(), link);
return Operation.createGet(createInventoryUri(service.getHost(), DiskService.DiskStateExpanded.buildUri(diskStateUri)));
}).collect(Collectors.toList());
OperationJoin.create(ops).setCompletion((operations, failures) -> {
if (failures != null) {
service.logFine(() -> String.format("Error in sync disks of VM %s", state.documentSelfLink));
patchOnComputeState(service, state, oldDocument, enumerationProgress, vm);
} else {
List<DiskService.DiskStateExpanded> currentDisks = operations.values().stream().map(op -> op.getBody(DiskService.DiskStateExpanded.class)).collect(Collectors.toList());
List<Operation> diskUpdateOps = new ArrayList<>(currentDisks.size());
// Select all disks to delete
List<String> disksToDelete = new ArrayList<>(state.diskLinks);
// Process the update of disks and then patch the compute
for (VirtualDevice device : vm.getDisks()) {
DiskService.DiskStateExpanded matchedDs = findMatchingDiskState(device, currentDisks);
// remove the active disk links
if (null != matchedDs) {
disksToDelete.remove(matchedDs.documentSelfLink);
}
Operation vdOp = processVirtualDevice(service, matchedDs, device, enumerationProgress, state.diskLinks, VimUtils.convertMoRefToString(vm.getId()), oldDocument);
if (vdOp != null) {
diskUpdateOps.add(vdOp);
}
}
// Delete disk states whose disks are deleted in vsphere.
for (String diskToDelete : disksToDelete) {
state.diskLinks.remove(diskToDelete);
diskUpdateOps.add(Operation.createDelete(PhotonModelUriUtils.createInventoryUri(service.getHost(), diskToDelete)));
}
OperationJoin.create(diskUpdateOps).setCompletion((operationMap, exception) -> {
patchOnComputeState(service, state, oldDocument, enumerationProgress, vm);
}).sendWith(service);
}
}).sendWith(service);
} else {
patchOnComputeState(service, state, oldDocument, enumerationProgress, vm);
}
}
Aggregations