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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations