use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class EndpointRemovalTaskServiceTest method createSnapshotState.
private static void createSnapshotState(BaseModelTest test, String endpointLink, List<String> tenantLinks) throws Throwable {
SnapshotState snapshot = new SnapshotState();
snapshot.name = "snapshot";
snapshot.parentLink = null;
snapshot.computeLink = "vmLink";
snapshot.tenantLinks = tenantLinks;
snapshot.endpointLinks = new HashSet<String>();
snapshot.endpointLinks.add(endpointLink);
snapshot.regionId = "region-id";
test.postServiceSynchronously(SnapshotService.FACTORY_LINK, snapshot, SnapshotState.class);
}
use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereAdapterSnapshotService method getChildSnapshots.
private DeferredResult<List<SnapshotState>> getChildSnapshots(String snapshotLink) {
DeferredResult<List<SnapshotState>> snapshotStates = new DeferredResult<>();
// find the child snapshots for the given snapshot document link
QueryTask qTask = getQueryWithFilters(snapshotLink, SnapshotState.FIELD_NAME_PARENT_LINK);
QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((o, e) -> {
if (e != null) {
logWarning(String.format("Failure retrieving the child snapshots %s", Utils.toString(e)));
snapshotStates.fail(e);
return;
}
QueryResultsProcessor rp = QueryResultsProcessor.create(o);
List<SnapshotState> snapshotsTemp = new ArrayList<>();
if (rp.hasResults()) {
snapshotsTemp = rp.streamDocuments(SnapshotState.class).collect(Collectors.toList());
}
snapshotStates.complete(snapshotsTemp);
});
return snapshotStates;
}
use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereAdapterSnapshotService method querySnapshotStates.
private DeferredResult<SnapshotContext> querySnapshotStates(SnapshotContext context) {
// find if for the compute a snapshot already exist or not and if yes, get the current snapshot (among the snapshots that may exist)
QueryTask.Query snapshotQuery = QueryTask.Query.Builder.create().addKindFieldClause(SnapshotState.class).addFieldClause(SnapshotState.FIELD_NAME_COMPUTE_LINK, context.snapshotState.computeLink).addFieldClause(SnapshotState.FIELD_NAME_IS_CURRENT, "true").build();
QueryTask qTask = QueryTask.Builder.createDirectTask().setQuery(snapshotQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QueryTask.QuerySpecification.QueryOption.INDEXED_METADATA).build();
return QueryUtils.startInventoryQueryTask(this, qTask).thenApply(op -> {
QueryResultsProcessor rp = QueryResultsProcessor.create(op);
if (rp.hasResults()) {
Optional<SnapshotState> snapshotStateOptional = rp.streamDocuments(SnapshotState.class).findFirst();
if (snapshotStateOptional.isPresent()) {
context.existingSnapshotState = snapshotStateOptional.get();
if (context.requestType == SnapshotService.SnapshotRequestType.CREATE) {
context.snapshotState.parentLink = context.existingSnapshotState.documentSelfLink;
}
}
}
return context;
});
}
use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereAdapterSnapshotService method deleteSnapshot.
private void deleteSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
// 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 removed", snapshotMoref)));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Deleting snapshot with name %s", context.snapshotState.name);
task = connection.getVimPort().removeSnapshotTask(snapshotMoref, REMOVE_CHILDREN, SNAPSHOT_CONSOLIDATION);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
logSevere("Deleting the snapshot %s failed", context.snapshotState.name);
deferredResult.fail(e);
return;
}
logInfo("Deleted the snapshot with name %s successfully", context.snapshotState.name);
// Once the actual snapshot delete is successful, process the update of the children
// snapshot states and the next current snapshot
final List<SnapshotState> childSnapshots = new ArrayList<>();
DeferredResult<List<SnapshotState>> dr = getChildSnapshots(snapshot.documentSelfLink);
List<SnapshotState> updatedChildSnapshots = new ArrayList<>();
dr.whenComplete((o, e) -> {
if (e != null) {
logSevere("Retrieving the details of children snapshots failed");
deferredResult.fail(e);
return;
}
childSnapshots.addAll(o);
logInfo("Retrieving the details of %s children snapshots ", childSnapshots.size());
// Update the children
if (!childSnapshots.isEmpty()) {
logInfo("Updating the state of child snapshots of: %s", snapshot.name);
childSnapshots.forEach(c -> {
c.parentLink = snapshot.parentLink;
updatedChildSnapshots.add(c);
});
context.snapshotOperations = updatedChildSnapshots.stream().map(childSnapshot -> Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getHost(), childSnapshot.documentSelfLink)).setBody(childSnapshot).setReferer(getUri())).collect(Collectors.toList());
}
processNextStepsForDeleteOperation(context, deferredResult);
});
}
use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.
the class VSphereAdapterSnapshotService method createSnapshot.
private void createSnapshot(Connection connection, SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
ManagedObjectReference vmMoRef = CustomProperties.of(context.computeDescription).getMoRef(CustomProperties.MOREF);
if (vmMoRef == null) {
deferredResult.fail(new IllegalStateException("Cannot find VM to snapshot"));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Creating snapshot for compute resource %s", context.computeDescription.name);
task = connection.getVimPort().createSnapshotTask(vmMoRef, context.snapshotState.name, context.snapshotState.description, context.snapshotMemory, false);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
deferredResult.fail(e);
return;
}
CustomProperties.of(context.snapshotState).put(CustomProperties.MOREF, (ManagedObjectReference) info.getResult());
// mark this as current snapshot
context.snapshotState.isCurrent = true;
// create a new xenon SnapshotState
logInfo(String.format("Creating a new snapshot state for compute : %s", context.computeDescription.name));
Operation createSnapshotState = Operation.createPost(PhotonModelUriUtils.createInventoryUri(getHost(), SnapshotService.FACTORY_LINK)).setBody(context.snapshotState);
if (context.existingSnapshotState != null) {
// un-mark old snapshot as current snapshot
context.existingSnapshotState.isCurrent = false;
Operation patchOldSnapshot = Operation.createPatch(UriUtils.buildUri(getHost(), context.existingSnapshotState.documentSelfLink)).setBody(context.existingSnapshotState);
OperationSequence.create(createSnapshotState).next(patchOldSnapshot).setCompletion((o, e) -> {
if (e != null && !e.isEmpty()) {
deferredResult.fail(e.values().iterator().next());
}
deferredResult.complete(context);
}).sendWith(this);
} else {
context.computeDescription.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, "true");
// patch compute adding the property '_hasSnapshots' with true
Operation patchCompute = Operation.createPatch(UriUtils.buildUri(getHost(), context.snapshotState.computeLink)).setBody(context.computeDescription);
OperationSequence.create(createSnapshotState).next(patchCompute).setCompletion((o, e) -> {
if (e != null && !e.isEmpty()) {
deferredResult.fail(e.values().iterator().next());
}
logInfo(String.format("Created a new snapshot state for compute : %s", context.computeDescription.name));
deferredResult.complete(context);
}).sendWith(this);
}
}
Aggregations