Search in sources :

Example 1 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class AzureComputeStatsGatherer method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    op.complete();
    ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
    TaskManager taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
    AzureStatsDataHolder statsData = new AzureStatsDataHolder();
    statsData.statsRequest = statsRequest;
    statsData.taskManager = taskManager;
    // TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-1336
    getVMDescription(statsData);
}
Also used : ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager)

Example 2 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class AzureCostStatsService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required."));
        return;
    }
    op.complete();
    ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
    TaskManager taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
    if (statsRequest.isMockRequest) {
        // patch status to parent task
        taskManager.finishTask();
        return;
    }
    Context context = new Context(statsRequest);
    context.taskManager = taskManager;
    handleRequest(context);
}
Also used : OperationContext(com.vmware.xenon.common.OperationContext) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager)

Example 3 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class AWSMockStatsService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    op.complete();
    ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
    AWSMockStatsDataHolder statsData = new AWSMockStatsDataHolder();
    statsData.statsRequest = statsRequest;
    statsData.taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
    if (statsRequest.isMockRequest) {
        // patch status to parent task
        statsData.taskManager.finishTask();
        return;
    }
    getVMDescription(statsData);
}
Also used : ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager)

Example 4 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager in project photon-model by vmware.

the class GCPStatsService method handlePatch.

/**
 * The REST PATCH request handler. This is the entry of starting stats collection.
 * @param patch Operation which should contain request body.
 */
@Override
public void handlePatch(Operation patch) {
    setOperationHandlerInvokeTimeStat(patch);
    if (!patch.hasBody()) {
        patch.fail(new IllegalArgumentException("body is required"));
        return;
    }
    patch.complete();
    ComputeStatsRequest statsRequest = patch.getBody(ComputeStatsRequest.class);
    GCPStatsDataHolder statsData = new GCPStatsDataHolder(patch);
    statsData.statsRequest = statsRequest;
    statsData.taskManager = new TaskManager(this, statsRequest.taskReference, statsRequest.resourceLink());
    // If mock mode is enabled, patch back to the parent.
    if (statsData.statsRequest.isMockRequest) {
        statsData.stage = StatsCollectionStage.FINISHED;
        handleStatsRequest(statsData);
    } else {
        statsData.stage = StatsCollectionStage.VM_DESC;
        handleStatsRequest(statsData);
    }
}
Also used : ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager)

Example 5 with TaskManager

use of com.vmware.photon.controller.model.adapters.util.TaskManager 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

TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)21 StatelessService (com.vmware.xenon.common.StatelessService)9 DeferredResult (com.vmware.xenon.common.DeferredResult)8 List (java.util.List)8 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)7 Operation (com.vmware.xenon.common.Operation)7 URI (java.net.URI)7 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)6 ArrayList (java.util.ArrayList)6 Connection (com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection)4 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)4 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)4 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 TaskStage (com.vmware.xenon.common.TaskState.TaskStage)4 QueryTask (com.vmware.xenon.services.common.QueryTask)4 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)3 DatacenterLister (com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)3 Element (com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element)3 RpcException (com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException)3