use of com.vmware.xenon.common.TaskState in project photon-model by vmware.
the class SnapshotTaskService method validateSnapshotAndStart.
private void validateSnapshotAndStart(Operation startPost, Operation get, Throwable e, SnapshotTaskState state) {
if (e != null) {
logWarning(() -> String.format("Failure retrieving snapshot state (%s): %s", get.getUri(), e.toString()));
startPost.complete();
failTask(e);
return;
}
SnapshotService.SnapshotState snapshotState = get.getBody(SnapshotService.SnapshotState.class);
if (snapshotState.name == null || snapshotState.name.isEmpty()) {
failTask(new IllegalArgumentException("Invalid snapshot name"));
return;
}
if (snapshotState.computeLink == null || snapshotState.computeLink.isEmpty()) {
failTask(new IllegalArgumentException("Invalid computeReference"));
return;
}
// we can complete start operation now, it will index and cache the
// update state
startPost.complete();
SnapshotTaskState newState = new SnapshotTaskState();
newState.taskInfo = new TaskState();
newState.taskInfo.stage = TaskState.TaskStage.STARTED;
newState.snapshotLink = state.snapshotLink;
newState.snapshotAdapterReference = state.snapshotAdapterReference;
newState.isMockRequest = state.isMockRequest;
// now we are ready to start our self-running state machine
sendSelfPatch(newState);
}
use of com.vmware.xenon.common.TaskState in project photon-model by vmware.
the class SnapshotTaskService method validateState.
public void validateState(SnapshotTaskState state) {
if (state.snapshotLink == null || state.snapshotLink.isEmpty()) {
throw new IllegalArgumentException("snapshotLink is required");
}
if (state.snapshotAdapterReference == null) {
throw new IllegalArgumentException("snapshotAdapterReference required");
}
if (state.taskInfo == null || state.taskInfo.stage == null) {
state.taskInfo = new TaskState();
state.taskInfo.stage = TaskState.TaskStage.CREATED;
}
if (state.documentExpirationTimeMicros == 0) {
state.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + SnapshotTaskState.DEFAULT_EXPIRATION_MICROS;
}
}
use of com.vmware.xenon.common.TaskState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskService method patchAdapter.
private void patchAdapter(ProvisionSecurityGroupTaskState taskState, String subTaskLink) {
if (subTaskLink == null) {
createSubTask(taskState, link -> patchAdapter(taskState, link));
return;
}
taskState.securityGroupDescriptionLinks.forEach(sgLink -> sendRequest(Operation.createGet(UriUtils.buildUri(this.getHost(), sgLink)).setCompletion((o, e) -> {
if (e != null) {
// don't fail the task; just update the subtask, which will
// handle the failure if necessary
ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.fail(sgLink, e);
updateSubTask(subTaskLink, subTaskPatchBody);
return;
}
SecurityGroupState securityGroupState = o.getBody(SecurityGroupState.class);
SecurityGroupInstanceRequest req = toReq(securityGroupState, taskState, sgLink, subTaskLink);
sendRequest(Operation.createPatch(securityGroupState.instanceAdapterReference).setBody(req).setCompletion((oo, ee) -> {
if (ee != null) {
ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.fail(sgLink, ee);
updateSubTask(subTaskLink, subTaskPatchBody);
}
}));
})));
}
use of com.vmware.xenon.common.TaskState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskService method handleStart.
@Override
public void handleStart(Operation start) {
if (!start.hasBody()) {
start.fail(new IllegalArgumentException("body is required"));
return;
}
ProvisionSecurityGroupTaskState state = start.getBody(ProvisionSecurityGroupTaskState.class);
try {
state.validate();
} catch (Exception e) {
start.fail(e);
return;
}
state.taskInfo = new TaskState();
state.taskInfo.stage = TaskState.TaskStage.CREATED;
state.taskSubStage = SubStage.CREATED;
start.complete();
// start the task
sendSelfPatch(TaskState.TaskStage.CREATED, null);
}
use of com.vmware.xenon.common.TaskState in project photon-model by vmware.
the class ProvisionSecurityGroupTaskService method sendSelfPatch.
private void sendSelfPatch(TaskState.TaskStage stage, Throwable e) {
ProvisionSecurityGroupTaskState body = new ProvisionSecurityGroupTaskState();
body.taskInfo = new TaskState();
if (e == null) {
body.taskInfo.stage = stage;
} else {
body.taskInfo.stage = TaskState.TaskStage.FAILED;
body.taskInfo.failure = Utils.toServiceErrorResponse(e);
logWarning(() -> String.format("Patching to failed: %s", Utils.toString(e)));
}
sendSelfPatch(body);
}
Aggregations