Search in sources :

Example 1 with CreateTagsResult

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

the class EC2Producer method createTags.

private void createTags(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    Collection tags;
    CreateTagsRequest request = new CreateTagsRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
        instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
        request.withResources(instanceIds);
    } else {
        throw new IllegalArgumentException("Instances Ids must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS))) {
        tags = exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS, Collection.class);
        request.withTags(tags);
    } else {
        throw new IllegalArgumentException("Tags must be specified");
    }
    CreateTagsResult result = new CreateTagsResult();
    try {
        result = ec2Client.createTags(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Create tags command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Created tags [{}] on resources with Ids [{}] ", Arrays.toString(tags.toArray()), Arrays.toString(instanceIds.toArray()));
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) Message(org.apache.camel.Message) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection)

Example 2 with CreateTagsResult

use of com.amazonaws.services.ec2.model.CreateTagsResult in project aws-doc-sdk-examples by awsdocs.

the class CreateInstance method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an instance name and AMI image id\n" + "Ex: CreateInstance <instance-name> <ami-image-id>\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String name = args[0];
    String ami_id = args[1];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    RunInstancesRequest run_request = new RunInstancesRequest().withImageId(ami_id).withInstanceType(InstanceType.T1Micro).withMaxCount(1).withMinCount(1);
    RunInstancesResult run_response = ec2.runInstances(run_request);
    String instance_id = run_response.getReservation().getReservationId();
    Tag tag = new Tag().withKey("Name").withValue(name);
    CreateTagsRequest tag_request = new CreateTagsRequest().withTags(tag);
    CreateTagsResult tag_response = ec2.createTags(tag_request);
    System.out.printf("Successfully started EC2 instance %s based on AMI %s", instance_id, ami_id);
}
Also used : CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) Tag(com.amazonaws.services.ec2.model.Tag)

Example 3 with CreateTagsResult

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

the class EC2ProducerTest method ec2CreateTagsTest.

@Test
public void ec2CreateTagsTest() throws Exception {
    mock.expectedMessageCount(1);
    Exchange exchange = template.request("direct:createTags", 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);
            Collection tags = new ArrayList();
            tags.add("pacific");
            exchange.getIn().setHeader(EC2Constants.INSTANCES_TAGS, tags);
        }
    });
    assertMockEndpointsSatisfied();
    CreateTagsResult resultGet = (CreateTagsResult) exchange.getIn().getBody();
    assertNotNull(resultGet);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

CreateTagsResult (com.amazonaws.services.ec2.model.CreateTagsResult)3 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)2 Collection (java.util.Collection)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)1 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)1 Tag (com.amazonaws.services.ec2.model.Tag)1 ArrayList (java.util.ArrayList)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 Processor (org.apache.camel.Processor)1 Test (org.junit.Test)1