/** * 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; }
private String autoDetectStackName(String instanceId) { Assert.notNull(instanceId, "No valid instance id defined"); DescribeStackResourcesResult describeStackResourcesResult = this.amazonCloudFormationClient.describeStackResources(new DescribeStackResourcesRequest().withPhysicalResourceId(instanceId)); if (describeStackResourcesResult != null && describeStackResourcesResult.getStackResources() != null && !describeStackResourcesResult.getStackResources().isEmpty()) { return describeStackResourcesResult.getStackResources().get(0).getStackName(); } if (this.amazonEc2Client != null) { DescribeTagsResult describeTagsResult = this.amazonEc2Client.describeTags(new DescribeTagsRequest().withFilters( new Filter("resource-id", Collections.singletonList(instanceId)), new Filter("resource-type", Collections.singletonList("instance")), new Filter("key", Collections.singletonList("aws:cloudformation:stack-name")))); if (describeTagsResult != null && describeTagsResult.getTags() != null && !describeTagsResult.getTags().isEmpty()) { return describeTagsResult.getTags().get(0).getValue(); } } return null; }
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()); }
/** * Fetches and instance's name Tag or null if it does not have one * @param instanceId * @param amazonEC2 * @return */ public static String getInstanceName(String instanceId, AmazonEC2 amazonEC2){ DescribeTagsResult result = amazonEC2.describeTags(new DescribeTagsRequest().withFilters( new Filter().withName("resource-id").withValues(instanceId), new Filter().withName("resource-type").withValues("instance"), new Filter().withName("key").withValues(TAG_KEY_NAME))); if(result.getTags().isEmpty()){ return null; } String name = result.getTags().get(0).getValue(); return name == null || name.trim().equals("") ? null : name; }
@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")); }
@Override public DescribeTagsResult describeTags(DescribeTagsRequest describeTagsRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
/** * 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(); }
@Override public boolean load(DescribeTagsRequest request) { return load(request, null); }
@Override public boolean load(DescribeTagsRequest request, ResultCapture<DescribeTagsResult> extractor) { return resource.load(request, extractor); }
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet. * The following request parameters will be populated from the data of this * <code>Tag</code> resource, and any conflicting parameter value set in the * request will be overridden: * <ul> * <li> * <b><code>Filters[0].Values.0</code></b> * - mapped from the <code>Key</code> identifier. * </li> * <li> * <b><code>Filters[1].Values.0</code></b> * - mapped from the <code>Value</code> identifier. * </li> * <li> * <b><code>Filters[0].Name</code></b> * - constant value <code>key</code>. * </li> * <li> * <b><code>Filters[1].Name</code></b> * - constant value <code>value</code>. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeTagsRequest */ boolean load(DescribeTagsRequest request);
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet, and use a ResultCapture to retrieve the low-level * client response * The following request parameters will be populated from the data of this * <code>Tag</code> resource, and any conflicting parameter value set in the * request will be overridden: * <ul> * <li> * <b><code>Filters[0].Values.0</code></b> * - mapped from the <code>Key</code> identifier. * </li> * <li> * <b><code>Filters[1].Values.0</code></b> * - mapped from the <code>Value</code> identifier. * </li> * <li> * <b><code>Filters[0].Name</code></b> * - constant value <code>key</code>. * </li> * <li> * <b><code>Filters[1].Name</code></b> * - constant value <code>value</code>. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeTagsRequest */ boolean load(DescribeTagsRequest request, ResultCapture<DescribeTagsResult> extractor);