Search in sources :

Example 1 with SnapshotRequestType

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;
    }
}
Also used : Service(com.vmware.xenon.common.Service) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) StringUtils(org.apache.commons.lang3.StringUtils) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ArrayList(java.util.ArrayList) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) SnapshotRequestType(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType) BiConsumer(java.util.function.BiConsumer) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) TaskInfo(com.vmware.vim25.TaskInfo) OperationSequence(com.vmware.xenon.common.OperationSequence) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) MapUtils(org.apache.commons.collections.MapUtils) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Optional(java.util.Optional) VSphereConstants(com.vmware.photon.controller.model.adapters.vsphere.constants.VSphereConstants) FactoryService(com.vmware.xenon.common.FactoryService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) TaskInfoState(com.vmware.vim25.TaskInfoState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) SnapshotRequestType(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType)

Aggregations

ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)1 IAAS_API_ENABLED (com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED)1 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)1 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)1 ResourceOperationSpecService (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService)1 TargetCriteria (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria)1 ResourceOperationUtils.handleAdapterResourceOperationRegistration (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration)1 TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)1 VSphereConstants (com.vmware.photon.controller.model.adapters.vsphere.constants.VSphereConstants)1 Connection (com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection)1 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)1 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)1 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)1 SessionUtil (com.vmware.photon.controller.model.resources.SessionUtil)1 SnapshotService (com.vmware.photon.controller.model.resources.SnapshotService)1 SnapshotRequestType (com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType)1 SnapshotState (com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)1 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)1 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1