use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType in project photon-model by vmware.
the class VSphereAdapterSnapshotService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
op.setStatusCode(Operation.STATUS_CODE_CREATED);
op.complete();
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
if (request.isMockRequest) {
mgr.patchTask(TaskStage.FINISHED);
return;
}
if (MapUtils.isEmpty(request.payload)) {
mgr.patchTaskToFailure(new IllegalArgumentException("Payload is missing for snapshot operation"));
return;
}
SnapshotRequestType requestType = SnapshotRequestType.fromString(request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_REQUEST_TYPE));
if (requestType == null) {
mgr.patchTaskToFailure(new IllegalArgumentException("No Request Type is specified in the payload for snapshot operation"));
return;
}
String computeLink = request.resourceLink();
String snapshotLink = request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_DOCUMENT_LINK);
if ((SnapshotRequestType.DELETE.equals(requestType) || SnapshotRequestType.REVERT.equals(requestType)) && (StringUtils.isBlank(snapshotLink))) {
mgr.patchTaskToFailure(new IllegalArgumentException("No Snapshot Link is specified in the payload for snapshot operation"));
return;
}
switch(requestType) {
case CREATE:
SnapshotContext createSnapshotContext = new SnapshotContext(populateAndGetSnapshotState(request), mgr, requestType, op);
createSnapshotContext.snapshotMemory = Boolean.valueOf(request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_MEMORY));
DeferredResult.completed(createSnapshotContext).thenCompose(this::thenSetComputeDescription).thenCompose(this::thenSetParentComputeDescription).thenCompose(this::querySnapshotStates).thenCompose(this::performSnapshotOperation).whenComplete((context, err) -> {
if (err != null) {
mgr.patchTaskToFailure(err);
return;
}
mgr.finishTask();
});
break;
case DELETE:
Operation.createGet(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshotLink)).setCompletion((o, e) -> {
if (e != null) {
mgr.patchTaskToFailure(e);
return;
}
SnapshotContext deleteSnapshotContext = new SnapshotContext(o.getBody(SnapshotState.class), mgr, requestType, op);
if (!computeLink.equals(deleteSnapshotContext.snapshotState.computeLink)) {
mgr.patchTaskToFailure(new IllegalArgumentException("Snapshot does not belong to the specified compute."));
return;
}
DeferredResult.completed(deleteSnapshotContext).thenCompose(this::thenSetComputeDescription).thenCompose(this::thenSetParentComputeDescription).thenCompose(this::performSnapshotOperation).whenComplete((context, err) -> {
if (err != null) {
mgr.patchTaskToFailure(err);
return;
}
mgr.finishTask();
});
}).sendWith(this);
break;
case REVERT:
Operation.createGet(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshotLink)).setCompletion((o, e) -> {
if (e != null) {
mgr.patchTaskToFailure(e);
return;
}
SnapshotContext revertSnapshotContext = new SnapshotContext(o.getBody(SnapshotState.class), mgr, requestType, op);
if (!computeLink.equals(revertSnapshotContext.snapshotState.computeLink)) {
mgr.patchTaskToFailure(new IllegalArgumentException("Snapshot does not belong to the specified compute."));
return;
}
DeferredResult.completed(revertSnapshotContext).thenCompose(this::thenSetComputeDescription).thenCompose(this::thenSetParentComputeDescription).thenCompose(this::querySnapshotStates).thenCompose(this::performSnapshotOperation).whenComplete((context, err) -> {
if (err != null) {
mgr.patchTaskToFailure(err);
return;
}
mgr.finishTask();
});
}).sendWith(this);
break;
default:
mgr.patchTaskToFailure(new IllegalStateException(String.format("Unknown Operation %s for a Snapshot", requestType)));
break;
}
}
Aggregations