Search in sources :

Example 1 with TerminateInstancesResult

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

the class EC2ProducerTest method ec2TerminateTest.

@Test
public void ec2TerminateTest() throws Exception {
    mock.expectedMessageCount(1);
    Exchange exchange = template.request("direct:terminate", 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();
    TerminateInstancesResult resultGet = (TerminateInstancesResult) exchange.getIn().getBody();
    assertEquals(resultGet.getTerminatingInstances().get(0).getInstanceId(), "test-1");
    assertEquals(resultGet.getTerminatingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString());
    assertEquals(resultGet.getTerminatingInstances().get(0).getCurrentState().getName(), InstanceStateName.Terminated.toString());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) Test(org.junit.Test)

Example 2 with TerminateInstancesResult

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

the class EC2Producer method terminateInstances.

private void terminateInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    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");
    }
    TerminateInstancesResult result;
    try {
        result = ec2Client.terminateInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Terminate Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Terminating instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Example 3 with TerminateInstancesResult

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

the class AmazonEC2ClientMock method terminateInstances.

@Override
public TerminateInstancesResult terminateInstances(TerminateInstancesRequest terminateInstancesRequest) {
    TerminateInstancesResult result = new TerminateInstancesResult();
    if (terminateInstancesRequest.getInstanceIds().contains("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.Terminated);
        sc.setPreviousState(previousState);
        sc.setCurrentState(newState);
        sc.setInstanceId("test-1");
        coll.add(sc);
        result.setTerminatingInstances(coll);
    } else {
        throw new AmazonServiceException("The image-id doesn't exists");
    }
    return result;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Example 4 with TerminateInstancesResult

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

the class EC2ComponentSpringTest method terminateInstances.

@Test
public void terminateInstances() {
    Exchange exchange = template.request("direct:terminate", 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);
        }
    });
    TerminateInstancesResult resultGet = (TerminateInstancesResult) exchange.getOut().getBody();
    assertEquals(resultGet.getTerminatingInstances().get(0).getInstanceId(), "test-1");
    assertEquals(resultGet.getTerminatingInstances().get(0).getPreviousState().getName(), InstanceStateName.Running.toString());
    assertEquals(resultGet.getTerminatingInstances().get(0).getCurrentState().getName(), InstanceStateName.Terminated.toString());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) Test(org.junit.Test)

Aggregations

TerminateInstancesResult (com.amazonaws.services.ec2.model.TerminateInstancesResult)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 Test (org.junit.Test)2 InstanceState (com.amazonaws.services.ec2.model.InstanceState)1 InstanceStateChange (com.amazonaws.services.ec2.model.InstanceStateChange)1 TerminateInstancesRequest (com.amazonaws.services.ec2.model.TerminateInstancesRequest)1 Message (org.apache.camel.Message)1