Search in sources :

Example 1 with ComputePowerRequest

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

the class AzurePowerService 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.complete();
    AzurePowerContext ctx = new AzurePowerContext(this, request);
    if (request.isMockRequest) {
        updateComputeState(ctx);
    } else {
        ctx.populateBaseContext(BaseAdapterStage.VMDESC).whenComplete((ignoreCtx, e) -> {
            if (e != null) {
                logSevere("Error populating base context during Azure power state operation %s for resource %s failed with error %s", request.powerState, request.resourceReference, Utils.toString(e));
                ctx.finishExceptionally(e);
                return;
            }
            ctx.vmName = ctx.child.name != null ? ctx.child.name : ctx.child.id;
            ctx.rgName = AzureUtils.getResourceGroupName(ctx);
            applyPowerOperation(ctx);
        });
    }
}
Also used : ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest)

Example 2 with ComputePowerRequest

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

the class AWSPowerService method handlePatch.

@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ComputePowerRequest pr = op.getBody(ComputePowerRequest.class);
    op.complete();
    if (pr.isMockRequest) {
        updateComputeState(pr, new DefaultAdapterContext(this, pr));
    } else {
        new DefaultAdapterContext(this, pr).populateBaseContext(BaseAdapterStage.VMDESC).whenComplete((c, e) -> {
            this.clientManager.getOrCreateEC2ClientAsync(c.endpointAuth, c.child.description.regionId, this).whenComplete((client, t) -> {
                if (t != null) {
                    c.taskManager.patchTaskToFailure(t);
                    return;
                }
                applyPowerOperation(client, pr, c);
            });
        });
    }
}
Also used : ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext)

Example 3 with ComputePowerRequest

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

the class AWSPowerService method updateComputeState.

private void updateComputeState(ComputePowerRequest pr, DefaultAdapterContext c) {
    ComputeState state = new ComputeState();
    state.powerState = pr.powerState;
    if (OFF.equals(pr.powerState)) {
        // clear IP address in case of power-off
        state.address = "";
    }
    Operation.createPatch(pr.resourceReference).setBody(state).setCompletion((o, e) -> {
        if (e != null) {
            c.taskManager.patchTaskToFailure(e);
            return;
        }
        c.taskManager.finishTask();
    }).sendWith(this);
}
Also used : 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) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Example 4 with ComputePowerRequest

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

the class AWSPowerService method powerOn.

private void powerOn(AmazonEC2AsyncClient client, ComputePowerRequest pr, DefaultAdapterContext c) {
    OperationContext opContext = OperationContext.getOperationContext();
    StartInstancesRequest request = new StartInstancesRequest();
    request.withInstanceIds(c.child.id);
    client.startInstancesAsync(request, new AsyncHandler<StartInstancesRequest, StartInstancesResult>() {

        @Override
        public void onSuccess(StartInstancesRequest request, StartInstancesResult result) {
            AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", 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) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest)

Example 5 with ComputePowerRequest

use of com.vmware.photon.controller.model.adapterapi.ComputePowerRequest 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)

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