Search in sources :

Example 1 with CreateTagsRequest

use of com.amazonaws.services.ec2.model.CreateTagsRequest in project GNS by MobilityFirst.

the class AWSEC2 method addInstanceTags.

/**
   * Adds keys and values from tagmap to the tags of the given instance.
   *
   * @param ec2
   * @param createdInstanceId
   * @param tagmap
   */
public static void addInstanceTags(AmazonEC2 ec2, String createdInstanceId, Map<String, String> tagmap) {
    List<String> resources = new LinkedList<>();
    resources.add(createdInstanceId);
    List<Tag> tags = new LinkedList<>();
    for (Entry<String, String> entry : tagmap.entrySet()) {
        Tag nameTag = new Tag(entry.getKey(), entry.getValue());
        tags.add(nameTag);
    }
    CreateTagsRequest ctr = new CreateTagsRequest(resources, tags);
    ec2.createTags(ctr);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 2 with CreateTagsRequest

use of com.amazonaws.services.ec2.model.CreateTagsRequest 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 3 with CreateTagsRequest

use of com.amazonaws.services.ec2.model.CreateTagsRequest 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 4 with CreateTagsRequest

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

the class AWSNetworkClient method createNameTagAsync.

public DeferredResult<Void> createNameTagAsync(String resourceId, String name) {
    Tag nameTag = new Tag().withKey(AWS_TAG_NAME).withValue(name);
    CreateTagsRequest request = new CreateTagsRequest().withResources(resourceId).withTags(nameTag);
    String message = "Name tag AWS resource with id [" + resourceId + "] with name [" + name + "].";
    AWSDeferredResultAsyncHandler<CreateTagsRequest, CreateTagsResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
    this.client.createTagsAsync(request, handler);
    return handler.toDeferredResult().thenApply(result -> (Void) null);
}
Also used : CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag)

Example 5 with CreateTagsRequest

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

the class TestAWSSetupUtils method tagResources.

/**
 * Tags resources with a given tag key and value.
 */
public static void tagResources(AmazonEC2Client client, List<String> resourceIds, String key, String value) {
    Tag tag = new Tag(key, value);
    CreateTagsRequest tagsRequest = new CreateTagsRequest(resourceIds, Arrays.asList(tag));
    client.createTags(tagsRequest);
}
Also used : CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag)

Aggregations

CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)7 CreateTagsResult (com.amazonaws.services.ec2.model.CreateTagsResult)3 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 LinkedList (java.util.LinkedList)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)1 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)1 Collection (java.util.Collection)1 Message (org.apache.camel.Message)1