Search in sources :

Example 1 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project camel by apache.

the class EC2Producer method stopInstances.

private void stopInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    StopInstancesRequest request = new StopInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
        instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
        request.withInstanceIds(instanceIds);
    } else {
        throw new IllegalArgumentException("Instances Ids must be specified");
    }
    StopInstancesResult result;
    try {
        result = ec2Client.stopInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Stop Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Stopping instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest)

Example 2 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project camel by apache.

the class EC2ComponentSpringTest method stopInstances.

@Test
public void stopInstances() {
    Exchange exchange = template.request("direct:stop", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Collection l = new ArrayList();
            l.add("test-1");
            exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l);
        }
    });
    StopInstancesResult resultGet = (StopInstancesResult) exchange.getOut().getBody();
    assertEquals(resultGet.getStoppingInstances().get(0).getInstanceId(), "test-1");
    assertEquals(resultGet.getStoppingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString());
    assertEquals(resultGet.getStoppingInstances().get(0).getCurrentState().getName(), InstanceStateName.Stopped.toString());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Test(org.junit.Test)

Example 3 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project camel by apache.

the class EC2ProducerTest method ec2StopTest.

@Test
public void ec2StopTest() throws Exception {
    mock.expectedMessageCount(1);
    Exchange exchange = template.request("direct:stop", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Collection l = new ArrayList();
            l.add("test-1");
            exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l);
        }
    });
    assertMockEndpointsSatisfied();
    StopInstancesResult resultGet = (StopInstancesResult) exchange.getIn().getBody();
    assertEquals(resultGet.getStoppingInstances().get(0).getInstanceId(), "test-1");
    assertEquals(resultGet.getStoppingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString());
    assertEquals(resultGet.getStoppingInstances().get(0).getCurrentState().getName(), InstanceStateName.Stopped.toString());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Test(org.junit.Test)

Example 4 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project photon-model by vmware.

the class AWSComputeDiskDay2Service method performDetachOperation.

private DeferredResult<DiskContext> performDetachOperation(DiskContext context) {
    DeferredResult<DiskContext> dr = new DeferredResult<>();
    try {
        validateDetachInfo(context.diskState);
        if (context.request.isMockRequest) {
            updateComputeAndDiskState(dr, context, null);
            return dr;
        }
        String instanceId = context.computeState.id;
        if (instanceId == null || !instanceId.startsWith(AWS_INSTANCE_ID_PREFIX)) {
            return logAndGetFailedDr(context, "compute id cannot be empty");
        }
        String diskId = context.diskState.id;
        if (diskId == null || !diskId.startsWith(AWS_VOLUME_ID_PREFIX)) {
            return logAndGetFailedDr(context, "disk id cannot be empty");
        }
        // stop the instance, detach the disk and then start the instance.
        if (context.baseAdapterContext.child.powerState.equals(ComputeService.PowerState.ON)) {
            StopInstancesRequest stopRequest = new StopInstancesRequest();
            stopRequest.withInstanceIds(context.baseAdapterContext.child.id);
            context.amazonEC2Client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {

                @Override
                protected void handleError(Exception e) {
                    service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start compute. %s", Utils.toString(e)));
                    OperationContext.restoreOperationContext(this.opContext);
                    context.error = e;
                    dr.complete(context);
                }

                @Override
                protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
                    OperationContext.restoreOperationContext(this.opContext);
                    AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", context.amazonEC2Client, (is, e) -> {
                        if (e != null) {
                            service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to stop " + "the compute. %s", Utils.toString(e)));
                            context.error = e;
                            dr.complete(context);
                            return;
                        }
                        logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully stopped " + "the instance %s", instanceId));
                        // detach disk from the instance.
                        detachVolume(context, dr, instanceId, diskId, true);
                    });
                }
            });
        } else {
            detachVolume(context, dr, instanceId, diskId, false);
        }
    } catch (Exception e) {
        context.error = e;
        return DeferredResult.completed(context);
    }
    return dr;
}
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) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 5 with StopInstancesResult

use of com.amazonaws.services.ec2.model.StopInstancesResult in project camel by apache.

the class AmazonEC2ClientMock method stopInstances.

@Override
public StopInstancesResult stopInstances(StopInstancesRequest stopInstancesRequest) {
    StopInstancesResult result = new StopInstancesResult();
    if (stopInstancesRequest.getInstanceIds().get(0).equals("test-1")) {
        Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>();
        InstanceStateChange sc = new InstanceStateChange();
        InstanceState previousState = new InstanceState();
        previousState.setCode(80);
        previousState.setName(InstanceStateName.Running);
        InstanceState newState = new InstanceState();
        newState.setCode(16);
        newState.setName(InstanceStateName.Stopped);
        sc.setPreviousState(previousState);
        sc.setCurrentState(newState);
        sc.setInstanceId("test-1");
        coll.add(sc);
        result.setStoppingInstances(coll);
    } else {
        throw new AmazonServiceException("The image-id doesn't exists");
    }
    return result;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Aggregations

StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)9 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)6 ArrayList (java.util.ArrayList)5 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3 StartInstancesRequest (com.amazonaws.services.ec2.model.StartInstancesRequest)3 StartInstancesResult (com.amazonaws.services.ec2.model.StartInstancesResult)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 DefaultAdapterContext (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext)3 Collection (java.util.Collection)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 InstanceStateChange (com.amazonaws.services.ec2.model.InstanceStateChange)2 AWSAsyncHandler (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler)2 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)2 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)2 ResourceOperationUtils.handleAdapterResourceOperationRegistration (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration)2 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)2 Operation (com.vmware.xenon.common.Operation)2 StatelessService (com.vmware.xenon.common.StatelessService)2