@Test public void testResourceNaming() throws Throwable { boolean tagFound = false; AmazonEC2AsyncClient client = TestUtils.getClient(this.privateKeyId,this.privateKey,this.region,false); //create something to name AWSNetworkClient svc = new AWSNetworkClient(client); String vpcID = svc.createVPC("10.20.0.0/16"); AWSUtils.tagResourcesWithName(client, TEST_NAME, vpcID); List<TagDescription> tags = AWSUtils.getResourceTags(vpcID,client); for (TagDescription tagDesc:tags) { if (tagDesc.getKey().equalsIgnoreCase(AWS_TAG_NAME)) { assertTrue(tagDesc.getValue().equalsIgnoreCase(TEST_NAME)); tagFound = true; break; } } // ensure we found the tag assertTrue(tagFound); svc.deleteVPC(vpcID); }
/** * Default constructor */ public ElapsedTimeAggregatorTest() { AmazonEC2 ec2Client = mock(AmazonEC2.class); AmazonCloudWatch cloudWatchClient = mock(AmazonCloudWatch.class); Region region = Region.getRegion(Regions.US_WEST_1); when(ec2Client.describeTags(any(DescribeTagsRequest.class))). thenReturn(new DescribeTagsResult()); instanceOnlyAggregator = new ElapsedTimeAggregator("TEST", region, "i-500f6ca6", null, ec2Client, cloudWatchClient); when(ec2Client.describeTags(any(DescribeTagsRequest.class))). thenReturn(new DescribeTagsResult().withTags( new TagDescription(). withKey("aws:autoscaling:groupName"). withValue("TEST") )); asgAggregator = new ElapsedTimeAggregator("TEST", region, "i-500f6ca6", null, ec2Client, cloudWatchClient); }
private List<Instance> runningInstances(String resourceId) { Tag tag = new EC2TagHelper(env).resourceId(resourceId); DescribeTagsRequest request = new DescribeTagsRequest() .withFilters(new Filter("key").withValues(tag.getKey()), new Filter("value").withValues(tag.getValue()), new Filter("resource-type").withValues("instance")); List<TagDescription> remoteTags = AWS.ec2.describeTags(request); List<String> instanceIds = remoteTags.stream().map(TagDescription::getResourceId).collect(Collectors.toList()); if (instanceIds.isEmpty()) { com.amazonaws.services.autoscaling.model.AutoScalingGroup asGroup = AWS.as.describeASGroup(env.name + "-" + this.resourceId); if (asGroup == null) throw new Error("can not find any running instance or asGroup, id=" + this.resourceId); instanceIds = asGroup.getInstances().stream() .map(com.amazonaws.services.autoscaling.model.Instance::getInstanceId) .collect(Collectors.toList()); } logger.info("find instanceId, {} => {}", resourceId, instanceIds); List<Instance> instances = AWS.ec2.describeInstances(instanceIds) .stream().filter(instance -> "running".equals(instance.getState().getName())).collect(Collectors.toList()); if (instances.isEmpty()) throw new Error("can not find any running instance, id=" + resourceId); return instances; }
/** * Test delete Tags. */ @Test(timeout = TIMEOUT_LEVEL1) public final void describerTagsTest() { log.info("Describe Tags test"); createTagsTest(); List<TagDescription> tagsDesc = getTags(); Assert.assertNotNull("tag Desc should not be null", tagsDesc); Collection<String> resources = new ArrayList<String>(); Collection<Tag> tags = new ArrayList<Tag>(); for(TagDescription tagDesc : tagsDesc) { Tag tag = new Tag(); tag.setKey(tagDesc.getKey()); tag.setValue(tagDesc.getValue()); tags.add(tag); resources.add(tagDesc.getResourceId()); } Assert.assertTrue("Tags should be created.", deleteTags(resources, tags)); }
public static List<TagDescription> getResourceTags(String resourceID, AmazonEC2AsyncClient client) { Filter resource = new Filter().withName(AWS_FILTER_RESOURCE_ID) .withValues(resourceID); DescribeTagsRequest req = new DescribeTagsRequest() .withFilters(resource); DescribeTagsResult result = client.describeTags(req); return result.getTags(); }
private List<EnvTag> loadEnvTags(Environment env) { Map<String, EnvTag> tags = Maps.newHashMap(); DescribeTagsRequest request = new DescribeTagsRequest() .withFilters(new Filter("key").withValues(new EC2TagHelper(env).prefix() + ":*")); List<TagDescription> remoteTags = AWS.ec2.describeTags(request); for (TagDescription remoteTag : remoteTags) { String remoteResourceId = remoteTag.getResourceId(); EnvTag tag = tags.computeIfAbsent(remoteResourceId, key -> new EnvTag(remoteTag)); tag.addField(remoteTag); } return new ArrayList<>(tags.values()); }
@Override protected Map<String, String> createInstance() throws Exception { LinkedHashMap<String, String> properties = new LinkedHashMap<>(); DescribeTagsResult tags = this.amazonEc2.describeTags(new DescribeTagsRequest().withFilters( new Filter("resource-id", Collections.singletonList(this.idProvider.getCurrentInstanceId())), new Filter("resource-type", Collections.singletonList("instance")))); for (TagDescription tag : tags.getTags()) { properties.put(tag.getKey(), tag.getValue()); } return properties; }
@Test public void getObject_userTagDataAvailable_objectContainsAllAvailableKeys() throws Exception { //Arrange AmazonEC2 amazonEC2 = mock(AmazonEC2.class); InstanceIdProvider instanceIdProvider = mock(InstanceIdProvider.class); when(instanceIdProvider.getCurrentInstanceId()).thenReturn("1234567890"); DescribeTagsRequest describeTagsRequest = new DescribeTagsRequest().withFilters( new Filter("resource-id", Collections.singletonList("1234567890")), new Filter("resource-type", Collections.singletonList("instance"))); DescribeTagsResult describeTagsResult = new DescribeTagsResult().withTags( new TagDescription().withKey("keyA").withResourceType(ResourceType.Instance).withValue("valueA"), new TagDescription().withKey("keyB").withResourceType(ResourceType.Instance).withValue("valueB") ); when(amazonEC2.describeTags(describeTagsRequest)).thenReturn(describeTagsResult); AmazonEc2InstanceUserTagsFactoryBean amazonEc2InstanceUserTagsFactoryBean = new AmazonEc2InstanceUserTagsFactoryBean(amazonEC2, instanceIdProvider); //Act amazonEc2InstanceUserTagsFactoryBean.afterPropertiesSet(); Map<String, String> resultMap = amazonEc2InstanceUserTagsFactoryBean.getObject(); //Assert assertEquals("valueA", resultMap.get("keyA")); assertEquals("valueB", resultMap.get("keyB")); assertFalse(resultMap.containsKey("keyC")); }
/** * Describe Tags. * @return TagsDescription */ protected final List<TagDescription> getTags() { DescribeTagsResult result = amazonEC2Client.describeTags(); List<TagDescription> tagsDesc = null; if (result != null) { tagsDesc = result.getTags(); } return tagsDesc; }
/** * Gets a map of tags for an AWS EC2 instance * @param instanceId the EC2 instance ID * @return Map of AWS tags */ public Map<String, String> getTags(String instanceId) { Filter filter = new Filter("resource-id", Arrays.asList(instanceId)); DescribeTagsResult result = amazonEC2Client.describeTags(new DescribeTagsRequest().withFilters(filter)); return result.getTags().stream().collect(Collectors.toMap(TagDescription::getKey, TagDescription::getValue)); }
public List<TagDescription> describeTags(DescribeTagsRequest request) { logger.info("describe tags, request={}", request); DescribeTagsResult result = ec2.describeTags(request); Asserts.isNull(result.getNextToken(), "tags pagination is not supported yet, token={}", result.getNextToken()); return result.getTags(); }
public EnvTag(TagDescription tag) { String resourceType = tag.getResourceType(); resourceClass = Asserts.notNull(RESOURCE_TYPES.get(resourceType), "not supported resourceType, type={}", resourceType); remoteResourceId = tag.getResourceId(); }
public void addField(TagDescription remoteTag) { String key = remoteTag.getKey(); int index = key.lastIndexOf(':'); String field = key.substring(index + 1); fields.put(field, remoteTag.getValue()); }