Search in sources :

Example 6 with ComputePowerRequest

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

the class AWSPowerService method powerOff.

private void powerOff(AmazonEC2AsyncClient client, ComputePowerRequest pr, DefaultAdapterContext c) {
    OperationContext opContext = OperationContext.getOperationContext();
    StopInstancesRequest request = new StopInstancesRequest();
    request.withInstanceIds(c.child.id);
    client.stopInstancesAsync(request, new AsyncHandler<StopInstancesRequest, StopInstancesResult>() {

        @Override
        public void onSuccess(StopInstancesRequest request, StopInstancesResult result) {
            AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
                OperationContext.restoreOperationContext(opContext);
                if (e != null) {
                    onError(e);
                    return;
                }
                updateComputeState(pr, c);
            });
        }

        @Override
        public void onError(Exception e) {
            OperationContext.restoreOperationContext(opContext);
            c.taskManager.patchTaskToFailure(e);
        }
    });
}
Also used : OperationContext(com.vmware.xenon.common.OperationContext) ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) OperationContext(com.vmware.xenon.common.OperationContext) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OFF(com.vmware.photon.controller.model.resources.ComputeService.PowerState.OFF) AsyncHandler(com.amazonaws.handlers.AsyncHandler) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest)

Example 7 with ComputePowerRequest

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

the class AzurePowerServiceTest method triggerPowerChange.

private void triggerPowerChange(PowerState powerState, URI powerAdapterReference) {
    String taskLink = UUID.randomUUID().toString();
    ComputePowerRequest powerRequest = new ComputePowerRequest();
    powerRequest.isMockRequest = this.isMock;
    powerRequest.powerState = powerState;
    powerRequest.resourceReference = UriUtils.buildUri(this.host, this.vmState.documentSelfLink);
    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(powerAdapterReference).setBody(powerRequest).setReferer("/tests").setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    });
    this.host.send(powerOp);
    ctx.await();
}
Also used : 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 8 with ComputePowerRequest

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

the class VSphereAdapterPowerService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ComputePowerRequest request = op.getBody(ComputePowerRequest.class);
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    ProvisionContext.populateContextThen(this, createInitialContext(request, op), ctx -> {
        if (request.isMockRequest) {
            patchComputeAndCompleteRequest(request, ctx);
            return;
        }
        handlePowerRequest(ctx, request);
    });
}
Also used : ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest)

Aggregations

ComputePowerRequest (com.vmware.photon.controller.model.adapterapi.ComputePowerRequest)8 Operation (com.vmware.xenon.common.Operation)5 DefaultAdapterContext (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext)4 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)4 AsyncHandler (com.amazonaws.handlers.AsyncHandler)3 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3 StartInstancesRequest (com.amazonaws.services.ec2.model.StartInstancesRequest)3 StartInstancesResult (com.amazonaws.services.ec2.model.StartInstancesResult)3 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)3 StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)3 AWSClientManager (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager)3 AWSClientManagerFactory (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory)3 BaseAdapterStage (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage)3 OFF (com.vmware.photon.controller.model.resources.ComputeService.PowerState.OFF)3 OperationContext (com.vmware.xenon.common.OperationContext)3 StatelessService (com.vmware.xenon.common.StatelessService)3 ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)2 TestContext (com.vmware.xenon.common.test.TestContext)2