use of software.amazon.awssdk.services.ec2.model.Tag 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);
}
use of software.amazon.awssdk.services.ec2.model.Tag in project Gatekeeper by FINRAOS.
the class Ec2LookupServiceTests method fakeInstance.
private Reservation fakeInstance(String instanceID, String instanceIP, String instanceName, String instanceApplication, String platform) {
Reservation container = new Reservation();
List<Instance> instances = new ArrayList<>();
Instance i = new Instance();
List<Tag> tags = new ArrayList<>();
i.setInstanceId(instanceID);
i.setPrivateIpAddress(instanceIP);
Tag nameTag = new Tag();
nameTag.setKey("Name");
nameTag.setValue(instanceName);
Tag applicationTag = new Tag();
applicationTag.setKey("Application");
applicationTag.setValue(instanceApplication);
tags.add(applicationTag);
tags.add(nameTag);
i.setTags(tags);
i.setPlatform(platform);
instances.add(i);
container.setInstances(instances);
return container;
}
use of software.amazon.awssdk.services.ec2.model.Tag in project cloudbreak by hortonworks.
the class AwsMetaDataCollectorTest method collectAlreadyTaggedOneGroup.
@Test
public void collectAlreadyTaggedOneGroup() {
List<CloudInstance> vms = new ArrayList<>();
List<Volume> volumes = new ArrayList<>();
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
List<String> gatewayIds = Collections.singletonList("i-1");
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
Instance instance = Mockito.mock(Instance.class);
when(instance.getInstanceId()).thenReturn("i-1");
when(instance.getPrivateIpAddress()).thenReturn("privateIp");
when(instance.getPublicIpAddress()).thenReturn("publicIp");
Tag tag = new Tag();
tag.setKey("cbname");
tag.setValue("somevalue");
when(instance.getTags()).thenReturn(Collections.singletonList(tag));
List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance));
when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
AuthenticatedContext ac = authenticatedContext();
List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
verify(amazonEC2Client, never()).createTags(any(CreateTagsRequest.class));
Assert.assertEquals(1, statuses.size());
Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp());
Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp());
}
use of software.amazon.awssdk.services.ec2.model.Tag in project cloudbreak by hortonworks.
the class AwsMetaDataCollectorTest method collectNewOldIsTagged.
@Test
public void collectNewOldIsTagged() {
List<CloudInstance> vms = new ArrayList<>();
List<Volume> volumes = new ArrayList<>();
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
List<String> gatewayIds = Arrays.asList("i-1", "i-new");
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
Instance instance1 = Mockito.mock(Instance.class);
when(instance1.getInstanceId()).thenReturn("i-1");
when(instance1.getPrivateIpAddress()).thenReturn("privateIp1");
when(instance1.getPublicIpAddress()).thenReturn("publicIp1");
Tag tag = new Tag();
tag.setKey("cbname");
tag.setValue("somevalue");
when(instance1.getTags()).thenReturn(Collections.singletonList(tag));
Instance instance2 = Mockito.mock(Instance.class);
when(instance2.getInstanceId()).thenReturn("i-new");
when(instance2.getPrivateIpAddress()).thenReturn("privateIp2");
when(instance2.getPublicIpAddress()).thenReturn("publicIp2");
List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance1, instance2));
when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
AuthenticatedContext ac = authenticatedContext();
List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
verify(amazonEC2Client, times(1)).createTags(any(CreateTagsRequest.class));
Assert.assertEquals(1, statuses.size());
Assert.assertEquals("i-new", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
Assert.assertEquals("privateIp2", statuses.get(0).getMetaData().getPrivateIp());
Assert.assertEquals("publicIp2", statuses.get(0).getMetaData().getPublicIp());
}
use of software.amazon.awssdk.services.ec2.model.Tag 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 reservation_id = run_response.getReservation().getInstances().get(0).getInstanceId();
Tag tag = new Tag().withKey("Name").withValue(name);
CreateTagsRequest tag_request = new CreateTagsRequest().withResources(reservation_id).withTags(tag);
CreateTagsResult tag_response = ec2.createTags(tag_request);
System.out.printf("Successfully started EC2 instance %s based on AMI %s", reservation_id, ami_id);
}
Aggregations