Search in sources :

Example 61 with Instance

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

the class EC2Producer method createAndRunInstance.

private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) {
    String ami;
    InstanceType instanceType;
    int minCount;
    int maxCount;
    boolean monitoring;
    String kernelId;
    boolean ebsOptimized;
    Collection securityGroups;
    String keyName;
    String clientToken;
    Placement placement;
    RunInstancesRequest request = new RunInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) {
        ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class);
        request.withImageId(ami);
    } else {
        throw new IllegalArgumentException("AMI must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) {
        instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class);
        request.withInstanceType(instanceType.toString());
    } else {
        throw new IllegalArgumentException("Instance Type must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) {
        minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class);
        request.withMinCount(minCount);
    } else {
        throw new IllegalArgumentException("Min instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) {
        maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class);
        request.withMaxCount(maxCount);
    } else {
        throw new IllegalArgumentException("Max instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) {
        monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class);
        request.withMonitoring(monitoring);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) {
        kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class);
        request.withKernelId(kernelId);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) {
        ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class);
        request.withEbsOptimized(ebsOptimized);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) {
        securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class);
        request.withSecurityGroups(securityGroups);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) {
        keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class);
        request.withKeyName(keyName);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) {
        clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class);
        request.withClientToken(clientToken);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) {
        placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class);
        request.withPlacement(placement);
    }
    RunInstancesResult result;
    try {
        result = ec2Client.runInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString());
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) Endpoint(org.apache.camel.Endpoint) Placement(com.amazonaws.services.ec2.model.Placement) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) InstanceType(com.amazonaws.services.ec2.model.InstanceType)

Example 62 with Instance

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

the class AmazonEC2ClientMock method describeInstances.

@Override
public DescribeInstancesResult describeInstances(DescribeInstancesRequest describeInstancesRequest) {
    DescribeInstancesResult result = new DescribeInstancesResult();
    if (describeInstancesRequest.getInstanceIds().isEmpty()) {
        Collection<Reservation> list = new ArrayList<Reservation>();
        Reservation res = new Reservation();
        res.setOwnerId("1");
        res.setRequesterId("user-test");
        res.setReservationId("res-1");
        Collection<Instance> instances = new ArrayList();
        Instance ins = new Instance();
        ins.setImageId("id-1");
        ins.setInstanceType(InstanceType.T2Micro);
        ins.setInstanceId("instance-1");
        instances.add(ins);
        Instance ins1 = new Instance();
        ins1.setImageId("id-2");
        ins1.setInstanceType(InstanceType.T2Micro);
        ins1.setInstanceId("instance-2");
        instances.add(ins1);
        res.setInstances(instances);
        list.add(res);
        result.setReservations(list);
    } else {
        if (describeInstancesRequest.getInstanceIds().contains("instance-1")) {
            Collection<Reservation> list = new ArrayList<Reservation>();
            Reservation res = new Reservation();
            res.setOwnerId("1");
            res.setRequesterId("user-test");
            res.setReservationId("res-1");
            Collection<Instance> instances = new ArrayList();
            Instance ins = new Instance();
            ins.setImageId("id-1");
            ins.setInstanceType(InstanceType.T2Micro);
            ins.setInstanceId("instance-1");
            instances.add(ins);
            res.setInstances(instances);
            list.add(res);
            result.setReservations(list);
        }
    }
    return result;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList)

Example 63 with Instance

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

the class AmazonEC2ClientMock method runInstances.

@Override
public RunInstancesResult runInstances(RunInstancesRequest runInstancesRequest) {
    RunInstancesResult result = new RunInstancesResult();
    if (runInstancesRequest.getImageId().equals("test-1")) {
        Reservation res = new Reservation();
        res.setOwnerId("1");
        res.setRequesterId("user-test");
        res.setReservationId("res-1");
        Collection<Instance> instances = new ArrayList();
        Instance ins = new Instance();
        ins.setImageId(runInstancesRequest.getImageId());
        ins.setInstanceType(runInstancesRequest.getInstanceType());
        ins.setInstanceId("instance-1");
        if (runInstancesRequest.getSecurityGroups() != null) {
            if (runInstancesRequest.getSecurityGroups().contains("secgroup-1") && runInstancesRequest.getSecurityGroups().contains("secgroup-2")) {
                GroupIdentifier id1 = new GroupIdentifier();
                id1.setGroupId("id-1");
                id1.setGroupName("secgroup-1");
                GroupIdentifier id2 = new GroupIdentifier();
                id2.setGroupId("id-2");
                id2.setGroupName("secgroup-2");
                Collection secGroups = new ArrayList<GroupIdentifier>();
                secGroups.add(id1);
                secGroups.add(id2);
                ins.setSecurityGroups(secGroups);
            } else if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName())) {
                if (ObjectHelper.isNotEmpty(runInstancesRequest.getKeyName().contains("keypair-1"))) {
                    GroupIdentifier id1 = new GroupIdentifier();
                    id1.setGroupId("id-3");
                    id1.setGroupName("secgroup-3");
                    GroupIdentifier id2 = new GroupIdentifier();
                    id2.setGroupId("id-4");
                    id2.setGroupName("secgroup-4");
                    Collection secGroups = new ArrayList<GroupIdentifier>();
                    secGroups.add(id1);
                    secGroups.add(id2);
                    ins.setSecurityGroups(secGroups);
                }
            }
        }
        instances.add(ins);
        res.setInstances(instances);
        result.setReservation(res);
    } else {
        throw new AmazonServiceException("The image-id doesn't exists");
    }
    return result;
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Example 64 with Instance

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

the class EC2ComponentSpringTest method ec2CreateAndRunTestWithKeyPair.

@Test
public void ec2CreateAndRunTestWithKeyPair() throws Exception {
    Exchange exchange = template.request("direct:createAndRun", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(EC2Constants.OPERATION, EC2Operations.createAndRunInstances);
            exchange.getIn().setHeader(EC2Constants.IMAGE_ID, "test-1");
            exchange.getIn().setHeader(EC2Constants.INSTANCE_TYPE, InstanceType.T2Micro);
            exchange.getIn().setHeader(EC2Constants.INSTANCE_MIN_COUNT, 1);
            exchange.getIn().setHeader(EC2Constants.INSTANCE_MAX_COUNT, 1);
            exchange.getIn().setHeader(EC2Constants.INSTANCES_KEY_PAIR, "keypair-1");
        }
    });
    RunInstancesResult resultGet = (RunInstancesResult) exchange.getOut().getBody();
    assertEquals(resultGet.getReservation().getInstances().get(0).getImageId(), "test-1");
    assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceType(), InstanceType.T2Micro.toString());
    assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceId(), "instance-1");
    assertEquals(resultGet.getReservation().getInstances().get(0).getSecurityGroups().size(), 2);
    assertEquals(resultGet.getReservation().getInstances().get(0).getSecurityGroups().get(0).getGroupId(), "id-3");
    assertEquals(resultGet.getReservation().getInstances().get(0).getSecurityGroups().get(1).getGroupId(), "id-4");
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) Test(org.junit.Test)

Example 65 with Instance

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

the class EC2ProducerTest method ec2DescribeSpecificInstancesTest.

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

        @Override
        public void process(Exchange exchange) throws Exception {
            Collection l = new ArrayList();
            l.add("instance-1");
            exchange.getIn().setHeader(EC2Constants.INSTANCES_IDS, l);
        }
    });
    assertMockEndpointsSatisfied();
    DescribeInstancesResult resultGet = (DescribeInstancesResult) exchange.getIn().getBody();
    assertEquals(resultGet.getReservations().size(), 1);
    assertEquals(resultGet.getReservations().get(0).getInstances().size(), 1);
}
Also used : Exchange(org.apache.camel.Exchange) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Processor(org.apache.camel.Processor) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)33 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)20 Reservation (com.amazonaws.services.ec2.model.Reservation)16 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)11 Test (org.junit.Test)10 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)7 Filter (com.amazonaws.services.ec2.model.Filter)7 Tag (com.amazonaws.services.ec2.model.Tag)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)7 ArrayList (java.util.ArrayList)7 Exchange (org.apache.camel.Exchange)7 Processor (org.apache.camel.Processor)7 AmazonServiceException (com.amazonaws.AmazonServiceException)6 LinkedList (java.util.LinkedList)6 AWSCredentials (com.amazonaws.auth.AWSCredentials)5 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)5 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)5