Search in sources :

Example 6 with DefaultAdapterContext

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

the class AWSResetService method startInstance.

private void startInstance(AmazonEC2AsyncClient client, DefaultAdapterContext c) {
    StartInstancesRequest startRequest = new StartInstancesRequest();
    startRequest.withInstanceIds(c.child.id);
    client.startInstancesAsync(startRequest, new AWSAsyncHandler<StartInstancesRequest, StartInstancesResult>() {

        @Override
        protected void handleError(Exception e) {
            c.taskManager.patchTaskToFailure(e);
        }

        @Override
        protected void handleSuccess(StartInstancesRequest request, StartInstancesResult result) {
            AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
                if (e == null) {
                    c.taskManager.finishTask();
                } else {
                    c.taskManager.patchTaskToFailure(e);
                }
            });
        }
    });
}
Also used : Service(com.vmware.xenon.common.Service) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) ServiceDocument(com.vmware.xenon.common.ServiceDocument) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) 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) 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) ResourceOperationUtils(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils) 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 7 with DefaultAdapterContext

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

the class AWSResetService method updateComputeState.

private void updateComputeState(DefaultAdapterContext c) {
    ComputeState state = new ComputeState();
    state.powerState = ComputeService.PowerState.ON;
    Operation.createPatch(c.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) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) ServiceDocument(com.vmware.xenon.common.ServiceDocument) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) 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) 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) ResourceOperationUtils(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils) 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 8 with DefaultAdapterContext

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

the class AWSResetService 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(() -> String.format("Handle operation %s for compute %s.", request.operation, request.resourceLink()));
    if (request.isMockRequest) {
        updateComputeState(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;
                }
                reset(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 9 with DefaultAdapterContext

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

the class AWSComputeDiskDay2Service 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 {
        validateRequest(request);
        op.complete();
    } catch (Exception e) {
        op.fail(e);
        return;
    }
    // initialize context with baseAdapterContext and request information
    DeferredResult<DiskContext> drDiskContext = new DefaultAdapterContext(this, request).populateBaseContext(BaseAdapterStage.VMDESC).thenApply(c -> {
        DiskContext context = new DiskContext();
        context.baseAdapterContext = c;
        context.request = request;
        return context;
    });
    Function<DiskContext, DeferredResult<DiskContext>> fn;
    // handle resource operation
    if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
        fn = this::performAttachOperation;
    } else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
        fn = this::performDetachOperation;
    } else {
        drDiskContext.thenApply(c -> {
            Throwable err = new IllegalArgumentException(String.format("Unknown Operation %s for a disk", request.operation));
            c.baseAdapterContext.taskManager.patchTaskToFailure(err);
            return c;
        });
        return;
    }
    drDiskContext.thenCompose(this::setComputeState).thenCompose(this::setDiskState).thenCompose(this::setClient).thenCompose(fn).whenComplete((ctx, e) -> {
        if (ctx.error == null) {
            ctx.baseAdapterContext.taskManager.finishTask();
        } else {
            ctx.baseAdapterContext.taskManager.patchTaskToFailure(ctx.error);
        }
    });
}
Also used : Service(com.vmware.xenon.common.Service) Arrays(java.util.Arrays) DetachVolumeRequest(com.amazonaws.services.ec2.model.DetachVolumeRequest) Pair(com.vmware.photon.controller.model.adapters.util.Pair) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) ServiceDocument(com.vmware.xenon.common.ServiceDocument) DetachVolumeResult(com.amazonaws.services.ec2.model.DetachVolumeResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) AWS_DISK_OPERATION_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_DISK_OPERATION_TIMEOUT_MINUTES) DEVICE_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_NAME) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) Utils(com.vmware.xenon.common.Utils) AttachVolumeResult(com.amazonaws.services.ec2.model.AttachVolumeResult) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AWSBlockDeviceNameMapper(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSBlockDeviceNameMapper) Map(java.util.Map) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) Reservation(com.amazonaws.services.ec2.model.Reservation) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) AWS_INSTANCE_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_INSTANCE_ID_PREFIX) List(java.util.List) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) Optional(java.util.Optional) FactoryService(com.vmware.xenon.common.FactoryService) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationContext(com.vmware.xenon.common.OperationContext) HashMap(java.util.HashMap) AWS_VOLUME_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VOLUME_ID_PREFIX) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Volume(com.amazonaws.services.ec2.model.Volume) Instance(com.amazonaws.services.ec2.model.Instance) AWSStorageType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSStorageType) Operation(com.vmware.xenon.common.Operation) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 10 with DefaultAdapterContext

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

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