Search in sources :

Example 1 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse in project photon-model by vmware.

the class ResourceRemovalTaskService method deallocateIpAddressesForResources.

private void deallocateIpAddressesForResources(ResourceRemovalTaskState currentState, QueryTask queryTask, String subTaskLink) {
    // handle empty pages
    if (queryTask.results.documentCount == 0) {
        sendSelfPatch(TaskState.TaskStage.FINISHED, SubStage.FINISHED, null);
        return;
    }
    if (subTaskLink == null) {
        createSubTaskForIpDeallocationCallbacks(currentState, queryTask, link -> deallocateIpAddressesForResources(currentState, queryTask, link));
        return;
    }
    logFine("Starting ip deallocation of %d compute resources using sub task", queryTask.results.documentCount);
    // for each compute resource link in the results, call ResourceIPDeallocationTaskService
    for (String resourceLink : queryTask.results.documentLinks) {
        ResourceIPDeallocationTaskState deallocateTask = new ResourceIPDeallocationTaskState();
        deallocateTask.resourceLink = resourceLink;
        deallocateTask.tenantLinks = currentState.tenantLinks;
        deallocateTask.serviceTaskCallback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), subTaskLink));
        deallocateTask.serviceTaskCallback.onSuccessFinishTask();
        // Similar to instance deletes - a failure for one network interface deallocate will fail the task
        deallocateTask.serviceTaskCallback.onErrorFailTask();
        this.sendRequest(Operation.createPost(this, ResourceIPDeallocationTaskService.FACTORY_LINK).setBody(deallocateTask).setCompletion((o, ex) -> {
            if (ex != null) {
                logSevere("Failure deallocating IPs for resource: [%s]: %s", resourceLink, ex);
                ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.finish(resourceLink);
                sendPatch(subTaskLink, subTaskPatchBody);
            } else {
                logFine("Sent deallocation IPs request for resource [%s]", resourceLink);
            }
        }));
    }
}
Also used : Service(com.vmware.xenon.common.Service) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) 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) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ResourceIPDeallocationTaskState(com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState) List(java.util.List) Stream(java.util.stream.Stream) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) ResourceIPDeallocationTaskState(com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState)

Example 2 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse in project photon-model by vmware.

the class ResourceRemovalTaskService method doInstanceDeletes.

private void doInstanceDeletes(ResourceRemovalTaskState currentState, QueryTask queryTask, String subTaskLink) {
    // handle empty pages
    if (queryTask.results.documentCount == 0) {
        sendSelfPatch(currentState.taskInfo.stage, SubStage.DEALLOCATE_IP_ADDRESSES, s -> {
            s.nextPageLink = null;
        });
        return;
    }
    if (subTaskLink == null) {
        createSubTaskForDeleteCallbacks(currentState, queryTask, link -> doInstanceDeletes(currentState, queryTask, link));
        return;
    }
    logFine(() -> String.format("Starting delete of %d compute resources using sub task %s", queryTask.results.documentLinks.size(), subTaskLink));
    // a DELETE request to its associated instance service.
    for (String resourceLink : queryTask.results.documentLinks) {
        URI u = ComputeStateWithDescription.buildUri(UriUtils.buildUri(getHost(), resourceLink));
        sendRequest(Operation.createGet(u).setCompletion((o, e) -> {
            if (e != null) {
                // we do not fail task if one delete failed ...
                // send a FINISHED patch which is what a sub task
                // would do. Since the
                // current state
                // is still at REMOVING_RESOURCES, we will just
                // increment a counter
                ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.fail(resourceLink, e);
                sendPatch(subTaskLink, subTaskPatchBody);
                return;
            }
            sendInstanceDelete(resourceLink, subTaskLink, o, currentState);
        }));
    }
}
Also used : Service(com.vmware.xenon.common.Service) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) URI(java.net.URI) EnumSet(java.util.EnumSet) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) 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) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ResourceIPDeallocationTaskState(com.vmware.photon.controller.model.tasks.ResourceIPDeallocationTaskService.ResourceIPDeallocationTaskState) List(java.util.List) Stream(java.util.stream.Stream) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) URI(java.net.URI)

Example 3 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse in project photon-model by vmware.

the class AWSPowerServiceTest method changePowerState.

private void changePowerState(ComputeDescription cd, String computeLink, PowerState powerState) {
    String taskLink = UUID.randomUUID().toString();
    ComputePowerRequest powerRequest = new ComputePowerRequest();
    powerRequest.isMockRequest = this.isMock;
    powerRequest.powerState = powerState;
    powerRequest.resourceReference = UriUtils.buildUri(this.host, computeLink);
    powerRequest.taskReference = UriUtils.buildUri(this.host, taskLink);
    TestContext ctx = this.host.testCreate(2);
    createTaskResultListener(this.host, taskLink, (u) -> {
        if (u.getAction() != Action.PATCH) {
            return false;
        }
        ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
        if (TaskState.isFailed(response.taskInfo)) {
            ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
        } else if (TaskState.isFinished(response.taskInfo)) {
            ctx.completeIteration();
        }
        return true;
    });
    Operation powerOp = Operation.createPatch(cd.powerAdapterReference).setBody(powerRequest).setReferer("/boza").setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    });
    this.host.send(powerOp);
    ctx.await();
    ComputeState compute = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeLink));
    assertEquals(powerState, compute.powerState);
    if (PowerState.OFF.equals(powerState)) {
        assertTrue(compute.address.isEmpty());
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Example 4 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse in project photon-model by vmware.

the class TaskManager method createTaskPatch.

public Operation createTaskPatch(TaskStage stage) {
    ResourceOperationResponse body = ResourceOperationResponse.finish(this.resourceLink);
    body.taskInfo.stage = stage;
    return Operation.createPatch(this.taskReference).setBody(body);
}
Also used : ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)

Example 5 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse in project photon-model by vmware.

the class BaseVSphereAdapterTest method createSnapshotAndWaitFailure.

protected void createSnapshotAndWaitFailure(ComputeState vm) throws Throwable {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest snapshotRequest = getCreateSnapshotRequest(ResourceOperation.CREATE_SNAPSHOT.operation, vm.documentSelfLink, taskLink);
    Operation createSnapshotOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterSnapshotService.SELF_LINK)).setBody(snapshotRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> Assert.assertNull(e));
    TestContext ctx2 = this.host.testCreate(1);
    createTaskResultListener(this.host, taskLink, (u) -> {
        if (u.getAction() != Service.Action.PATCH) {
            return false;
        }
        ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
        if (TaskState.isFailed(response.taskInfo)) {
            ctx2.completeIteration();
            return true;
        } else {
            ctx2.completeIteration();
            return false;
        }
    });
    this.host.send(createSnapshotOp);
    ctx2.await();
}
Also used : ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestContext(com.vmware.xenon.common.test.TestContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Aggregations

ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)17 Operation (com.vmware.xenon.common.Operation)14 TestContext (com.vmware.xenon.common.test.TestContext)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)8 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)8 UriPaths (com.vmware.photon.controller.model.UriPaths)4 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)4 PropertyUsageOption (com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption)4 TaskState (com.vmware.xenon.common.TaskState)4 UriUtils (com.vmware.xenon.common.UriUtils)4 Utils (com.vmware.xenon.common.Utils)4 TaskService (com.vmware.xenon.services.common.TaskService)4 List (java.util.List)4 Consumer (java.util.function.Consumer)4 ComputeInstanceRequest (com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest)3 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)3 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)3 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)3 SnapshotService (com.vmware.photon.controller.model.resources.SnapshotService)3