Search in sources :

Example 31 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class AzureLifecycleOperationServiceTest method triggerSuspend.

private void triggerSuspend() {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest request = new ResourceOperationRequest();
    request.isMockRequest = this.isMock;
    request.operation = ResourceOperation.SUSPEND.operation;
    request.resourceReference = UriUtils.buildUri(this.host, this.vmState.documentSelfLink);
    request.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 restartOp = Operation.createPatch(UriUtils.buildUri(this.host, AzureLifecycleOperationService.SELF_LINK)).setBody(request).setReferer("/tests2").setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    });
    this.host.send(restartOp);
    ctx.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) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)

Example 32 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class VSphereAdapterD2PowerOpsService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
    op.complete();
    logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
    VSphereVMContext vmContext = new VSphereVMContext(this, request, op);
    vmContext.pool = VSphereIOThreadPoolAllocator.getPool(this);
    VSphereVMContext.populateVMContextThen(this, vmContext, ctx -> {
        if (request.isMockRequest) {
            patchComputeAndCompleteRequest(ctx, request.operation);
            return;
        }
        handleResourceOperationRequest(ctx, request.operation);
    });
}
Also used : ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 33 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class VSphereAdapterResizeComputeService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
    op.complete();
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    if (request.payload != null && !request.payload.isEmpty()) {
        logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
        final String newCpu = request.payload.get(COMPUTE_CPU_COUNT);
        final String newMemory = request.payload.get(COMPUTE_MEMORY_IN_MBYTES);
        final String rebootVMFlag = request.payload.get(REBOOT_VM_FLAG);
        if ((newCpu == null || newCpu.isEmpty()) && (newMemory == null || newMemory.isEmpty())) {
            // payload doesn't contain anything so patch task to FINISHED and return
            mgr.patchTask(TaskState.TaskStage.FINISHED);
            return;
        }
        VSphereVMContext vmContext = new VSphereVMContext(this, request, op);
        vmContext.pool = VSphereIOThreadPoolAllocator.getPool(this);
        VSphereVMContext.populateVMContextThen(this, vmContext, ctx -> {
            int newCpuCount;
            long newMemoryInMBytes;
            boolean rebootVM;
            try {
                if (newCpu != null && !newCpu.isEmpty()) {
                    newCpuCount = Integer.parseInt(newCpu);
                } else {
                    newCpuCount = toIntExact(ctx.child.description.cpuCount);
                }
                if (newMemory != null && !newMemory.isEmpty()) {
                    newMemoryInMBytes = Long.parseLong(newMemory);
                } else {
                    newMemoryInMBytes = ctx.child.description.totalMemoryBytes / MEGA_BYTES_TO_BYTES_CONSTANT;
                }
            } catch (NumberFormatException numberEx) {
                // NumberFormatException for all the parseXXX
                logWarning(String.format("Request payload has values for CPU: %s or memory: %s in incorrect types " + numberEx.getMessage(), newCpu, newMemory));
                ctx.failWithMessage(String.format("Request payload has values for CPU: %s or memory: %s in incorrect types ", newCpu, newMemory), numberEx);
                return;
            }
            if (rebootVMFlag != null && !rebootVMFlag.isEmpty()) {
                rebootVM = Boolean.valueOf(rebootVMFlag);
            } else {
                rebootVM = true;
            }
            if (request.isMockRequest) {
                patchComputeAndCompleteRequest(ctx, newCpuCount, newMemoryInMBytes);
                return;
            }
            handleResourceOperationRequest(ctx, newCpuCount, newMemoryInMBytes, rebootVM);
        });
    } else {
        mgr.patchTaskToFailure(new IllegalArgumentException("Request payload is required for resize compute operation"));
    }
}
Also used : TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 34 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class VSphereComputeDiskManagementService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
    try {
        validateInputRequest(request);
    } catch (Exception e) {
        op.fail(e);
        return;
    }
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    TaskManager taskManager = new TaskManager(this, request.taskReference, request.resourceLink());
    logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
    VSphereVMDiskContext.populateVMDiskContextThen(this, createInitialContext(taskManager, request, op), ctx -> {
        if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
            handleAttachDisk(ctx);
        } else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
            handleDetachDisk(ctx);
        } else {
            IllegalArgumentException exception = new IllegalArgumentException(String.format("Unknown Operation %s for a disk", request.operation));
            ctx.fail(exception);
        }
    });
}
Also used : TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 35 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class AzureComputeDiskDay2Service method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("Body is required"));
        return;
    }
    ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
    try {
        validateInputRequest(request);
    } catch (Exception ex) {
        op.fail(ex);
        return;
    }
    op.complete();
    // initialize context object
    AzureComputeDiskDay2Context context = new AzureComputeDiskDay2Context(this, request);
    DeferredResult<AzureComputeDiskDay2Context> execution = DeferredResult.completed(context);
    // common steps for both attach / detach ops
    execution = execution.thenCompose(this::getComputeState).thenCompose(this::getDiskState).thenCompose(this::configureAzureSDKClient).thenCompose(this::getAzureVirtualMachine);
    if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
        execution = execution.thenCompose(this::getAzureDisk).thenCompose(this::performDiskAttachment).thenCompose(this::updateComputeStateAndDiskState);
    } else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
        execution = execution.thenCompose(this::performDiskDetach).thenCompose(this::updateComputeStateAndDiskState);
    }
    execution.whenComplete((o, ex) -> context.finish(ex));
}
Also used : ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Aggregations

ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)36 Operation (com.vmware.xenon.common.Operation)20 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)19 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)16 ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)8 TestContext (com.vmware.xenon.common.test.TestContext)8 TestRequestSender (com.vmware.xenon.common.test.TestRequestSender)7 DefaultAdapterContext (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext)5 StatelessService (com.vmware.xenon.common.StatelessService)5 ResourceOperationUtils.handleAdapterResourceOperationRegistration (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration)4 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)4 DiskService (com.vmware.photon.controller.model.resources.DiskService)4 SnapshotState (com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 FactoryService (com.vmware.xenon.common.FactoryService)4 Service (com.vmware.xenon.common.Service)4 ServiceDocument (com.vmware.xenon.common.ServiceDocument)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3