use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereAdapterSnapshotService method populateAndGetSnapshotState.
private SnapshotState populateAndGetSnapshotState(ResourceOperationRequest request) {
SnapshotState snapshotState = new SnapshotState();
snapshotState.id = UUID.randomUUID().toString();
snapshotState.computeLink = request.resourceLink();
snapshotState.documentSelfLink = UriUtils.buildUriPath(SnapshotService.FACTORY_LINK, getHost().nextUUID());
final String snapshotRequestName = request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_NAME);
final String snapshotRequestDesc = request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_DESCRIPTION);
if (snapshotRequestName == null || snapshotRequestName.isEmpty()) {
snapshotState.name = "snapshot-" + Utils.getNowMicrosUtc();
} else {
snapshotState.name = snapshotRequestName;
}
if (snapshotRequestDesc == null || snapshotRequestDesc.isEmpty()) {
snapshotState.description = "description - " + snapshotState.name;
} else {
snapshotState.description = snapshotRequestDesc;
}
return snapshotState;
}
use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState 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.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereAdapterSnapshotService method isLastSnapshotForCompute.
private DeferredResult<Boolean> isLastSnapshotForCompute(SnapshotContext context) {
DeferredResult<Boolean> dr = new DeferredResult<>();
// find if for the compute has only one snapshot (the one which is to be deleted)
QueryTask qTask = getQueryWithFilters(context.snapshotState.computeLink, SnapshotState.FIELD_NAME_COMPUTE_LINK);
QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((o, e) -> {
if (e != null) {
logInfo(String.format("Failure getting snapshot state: %s", Utils.toString(e)));
dr.fail(e);
return;
}
QueryResultsProcessor rp = QueryResultsProcessor.create(o);
List<SnapshotState> snapshotsFinal;
if (!rp.hasResults()) {
dr.complete(Boolean.FALSE);
} else {
snapshotsFinal = rp.streamDocuments(SnapshotState.class).collect(Collectors.toList());
dr.complete(snapshotsFinal.size() == 1);
}
});
return dr;
}
use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereAdapterSnapshotService method revertSnapshot.
private void revertSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
SnapshotState existingSnapshotState = context.existingSnapshotState;
// Physical snapshot processing
ManagedObjectReference snapshotMoref = CustomProperties.of(snapshot).getMoRef(CustomProperties.MOREF);
if (snapshotMoref == null) {
deferredResult.fail(new IllegalStateException(String.format("Cannot find the snapshot %s to revert to", snapshotMoref)));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Reverting to snapshot with name %s", context.snapshotState.name);
task = connection.getVimPort().revertToSnapshotTask(snapshotMoref, null, false);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
logSevere("Reverting to the snapshot %s failed", context.snapshotState.name);
deferredResult.fail(e);
return;
}
// cause concurrency issue
if (snapshot.isCurrent) {
deferredResult.complete(context);
} else {
snapshot.isCurrent = true;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setBody(snapshot).setReferer(getUri()));
if (existingSnapshotState != null) {
existingSnapshotState.isCurrent = false;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), existingSnapshotState.documentSelfLink)).setBody(existingSnapshotState).setReferer(getUri()));
}
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);
};
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
}
}
use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereVMSnapshotEnumerationHelper method processSnapshot.
static void processSnapshot(VSphereIncrementalEnumerationService service, VirtualMachineSnapshotTree current, String parentLink, EnumerationProgress enumerationProgress, VmOverlay vm, String vmSelfLink) {
enumerationProgress.getSnapshotTracker().register();
QueryTask task = queryForSnapshot(enumerationProgress, current.getId().toString(), vmSelfLink);
VsphereEnumerationHelper.withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
VsphereEnumerationHelper.submitWorkToVSpherePool(service, () -> {
SnapshotState snapshotState = constructSnapshot(service, current, parentLink, vmSelfLink, enumerationProgress, vm);
if (result.documentLinks.isEmpty()) {
VSphereVirtualMachineEnumerationHelper.createSnapshot(service, snapshotState).thenCompose(createdSnapshotState -> trackAndProcessChildSnapshots(service, current, enumerationProgress, vm, vmSelfLink, createdSnapshotState)).whenComplete((ss, e) -> {
if (e != null) {
service.log(Level.SEVERE, "Creation of snapshot with name {%s} failed.", snapshotState.name);
}
enumerationProgress.getSnapshotTracker().arrive();
});
} else {
SnapshotState oldState = VsphereEnumerationHelper.convertOnlyResultToDocument(result, SnapshotState.class);
updateSnapshot(service, enumerationProgress, vm, oldState, snapshotState, current.getId().toString()).thenCompose(updatedSnapshotState -> trackAndProcessChildSnapshots(service, current, enumerationProgress, vm, vmSelfLink, updatedSnapshotState)).whenComplete((ss, e) -> {
if (e != null) {
service.logSevere("Updating of snapshot with name {%s}, selfLink {%s} failed", snapshotState.name, oldState.documentSelfLink);
}
enumerationProgress.getSnapshotTracker().arrive();
});
}
});
});
}
Aggregations