Search in sources :

Example 1 with DefaultAdapterContext

use of com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext 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 2 with DefaultAdapterContext

use of com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext 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 3 with DefaultAdapterContext

use of com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext 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 4 with DefaultAdapterContext

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

the class AWSRebootService 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());
    if (request.isMockRequest) {
        updateComputeState(request, new DefaultAdapterContext(this, request));
    } else {
        new DefaultAdapterContext(this, request).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;
                }
                reboot(client, request, c);
            });
        });
    }
}
Also used : DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 5 with DefaultAdapterContext

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

the class AWSRebootService method updateComputeState.

private void updateComputeState(ResourceOperationRequest ror, DefaultAdapterContext c) {
    ComputeState state = new ComputeState();
    state.powerState = ComputeService.PowerState.ON;
    Operation.createPatch(ror.resourceReference).setBody(state).setCompletion((o, e) -> {
        if (e != null) {
            c.taskManager.patchTaskToFailure(e);
            return;
        }
        c.taskManager.finishTask();
    }).sendWith(this);
}
Also used : Service(com.vmware.xenon.common.Service) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) RebootInstancesResult(com.amazonaws.services.ec2.model.RebootInstancesResult) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ServiceDocument(com.vmware.xenon.common.ServiceDocument) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AsyncHandler(com.amazonaws.handlers.AsyncHandler) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) FactoryService(com.vmware.xenon.common.FactoryService) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) ResourceType(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceType) 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)

Aggregations

DefaultAdapterContext (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext)11 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)8 AWSClientManager (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager)8 AWSClientManagerFactory (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory)8 BaseAdapterStage (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage)8 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)8 Operation (com.vmware.xenon.common.Operation)8 StatelessService (com.vmware.xenon.common.StatelessService)8 StartInstancesRequest (com.amazonaws.services.ec2.model.StartInstancesRequest)7 StartInstancesResult (com.amazonaws.services.ec2.model.StartInstancesResult)7 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)7 StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)7 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)7 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)5 ResourceOperationUtils.handleAdapterResourceOperationRegistration (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration)5 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)5 FactoryService (com.vmware.xenon.common.FactoryService)5 Service (com.vmware.xenon.common.Service)5 ServiceDocument (com.vmware.xenon.common.ServiceDocument)5 AsyncHandler (com.amazonaws.handlers.AsyncHandler)4