Search in sources :

Example 31 with TaskState

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);
}
Also used : SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) TaskState(com.vmware.xenon.common.TaskState)

Example 32 with TaskState

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;
    }
}
Also used : TaskState(com.vmware.xenon.common.TaskState)

Example 33 with TaskState

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);
            }
        }));
    })));
}
Also used : SecurityGroupInstanceRequest(com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) Set(java.util.Set) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Consumer(java.util.function.Consumer) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) Map(java.util.Map) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) EXPAND(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.EXPAND) InstanceRequestType(com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest.InstanceRequestType) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) FIXED_ITEM_NAME(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.FIXED_ITEM_NAME) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) SecurityGroupInstanceRequest(com.vmware.photon.controller.model.adapterapi.SecurityGroupInstanceRequest) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState)

Example 34 with TaskState

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);
}
Also used : ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) TaskState(com.vmware.xenon.common.TaskState)

Example 35 with TaskState

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);
}
Also used : ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) ProvisionSecurityGroupTaskState(com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState) TaskState(com.vmware.xenon.common.TaskState)

Aggregations

TaskState (com.vmware.xenon.common.TaskState)45 EndpointAllocationTaskState (com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState)6 NicSecurityGroupsTaskState (com.vmware.photon.controller.model.tasks.NicSecurityGroupsTaskService.NicSecurityGroupsTaskState)5 ProvisionSubnetTaskState (com.vmware.photon.controller.model.tasks.ProvisionSubnetTaskService.ProvisionSubnetTaskState)5 Operation (com.vmware.xenon.common.Operation)5 ImageEnumerationTaskState (com.vmware.photon.controller.model.tasks.ImageEnumerationTaskService.ImageEnumerationTaskState)4 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)3 IPAddressAllocationTaskState (com.vmware.photon.controller.model.tasks.IPAddressAllocationTaskService.IPAddressAllocationTaskState)3 ProvisionSecurityGroupTaskState (com.vmware.photon.controller.model.tasks.ProvisionSecurityGroupTaskService.ProvisionSecurityGroupTaskState)3 SubTaskState (com.vmware.photon.controller.model.tasks.SubTaskService.SubTaskState)3 HashMap (java.util.HashMap)3 UriPaths (com.vmware.photon.controller.model.UriPaths)2 EndpointType (com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType)2 ProvisionDiskTaskState (com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState)2 ResourceIPDeallocationTaskState (com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState)2 PropertyUsageOption (com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption)2 UriUtils (com.vmware.xenon.common.UriUtils)2 Utils (com.vmware.xenon.common.Utils)2 TaskService (com.vmware.xenon.services.common.TaskService)2 List (java.util.List)2