com.amazonaws.services.ec2.model.DescribeTagsRequest Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.DescribeTagsRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceTaggingManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param resourceId
 * @param clientMap
 * @return
 */
private String getEC2PacManTagValue(String resourceId, Map<String, Object> clientMap) {
    AmazonEC2 ec2Client = (AmazonEC2) clientMap.get("client");
    DescribeTagsRequest describeTagsRequest = new DescribeTagsRequest();
    Filter filter = new Filter("resource-id");
    filter.setValues(Arrays.asList(resourceId));
    describeTagsRequest.setFilters(Arrays.asList(filter));
    DescribeTagsResult describeTagsResult = ec2Client.describeTags(describeTagsRequest);
    List<TagDescription> descriptions = describeTagsResult.getTags();
    TagDescription tagDescription = null;
    Optional<TagDescription> optional = descriptions.stream()
            .filter(obj -> obj.getKey().equals(PacmanSdkConstants.PACMAN_AUTO_FIX_TAG_NAME)).findAny();
    if (optional.isPresent()) {
        tagDescription = optional.get();
    } else {
        return null;
    }
    return tagDescription.getValue();
}
 
Example #2
Source File: ResourceTaggingManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * get the value of pacman tag from app elb
 * @param resourceId
 * @param clientMap
 * @return
 */
private String getAppElbPacManTagValue(String resourceId, Map<String, Object> clientMap) {

    try{
        AmazonElasticLoadBalancingClient client = (AmazonElasticLoadBalancingClient) clientMap.get(PacmanSdkConstants.CLIENT);
        com.amazonaws.services.elasticloadbalancingv2.model.DescribeTagsRequest describeTagsRequest =   new com.amazonaws.services.elasticloadbalancingv2.model.DescribeTagsRequest();
        describeTagsRequest.withResourceArns(resourceId);
        com.amazonaws.services.elasticloadbalancingv2.model.DescribeTagsResult describeTagsResult = client.describeTags(describeTagsRequest);
        List<com.amazonaws.services.elasticloadbalancingv2.model.TagDescription> descriptions = describeTagsResult.getTagDescriptions();
        com.amazonaws.services.elasticloadbalancingv2.model.Tag tag=null;
        Optional<com.amazonaws.services.elasticloadbalancingv2.model.Tag> optional=null;;
        if(descriptions!=null && descriptions.size()>0){
             optional = descriptions.get(0).getTags().stream()
            .filter(obj -> obj.getKey().equals(CommonUtils.getPropValue(PacmanSdkConstants.PACMAN_AUTO_FIX_TAG_NAME))).findAny();
        }
        if (optional.isPresent()) {
            tag = optional.get();
        } else {
            return null;
        }
        return tag.getValue();
    }catch (Exception e) {
        logger.error("error whiel getting pacman tag valye for " + resourceId,e);
        return null;
    }
}
 
Example #3
Source File: ResourceTaggingManagerTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the pacman tag value 2.
 *
 * @throws Exception the exception
 */
@Test
   public void getPacmanTagValue2() throws Exception{
	PowerMockito.mockStatic(CommonUtils.class);
	Map<String, Object> clientMap = Maps.newHashMap();
	clientMap.put("client", ec2Mock);
	Map<String, String> pacTag = Maps.newHashMap();
	List<TagSet> existingTargets = Lists.newArrayList();
	TagSet tagSet = new TagSet();
	tagSet.setTag("test", "value2");
	existingTargets.add(tagSet);
	DescribeTagsResult descriptions = getDescribeTagsResult();
	PowerMockito.when(ec2Mock.describeTags(any(DescribeTagsRequest.class))).thenReturn(descriptions);
	PowerMockito.when(bucketTaggingConfiguration.getAllTagSets()).thenReturn(existingTargets);
	PowerMockito.when(s3Mock.getBucketTaggingConfiguration(anyString())).thenReturn(bucketTaggingConfiguration);
	PowerMockito.when(CommonUtils.getPropValue(anyString())).thenReturn("test");


	final ResourceTaggingManager classUnderTest = PowerMockito.spy(new ResourceTaggingManager());
	assertNull(classUnderTest.getPacmanTagValue("resourceId", clientMap, AWSService.EC2));
}
 
Example #4
Source File: TaupageExpirationTimeProviderImpl.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Override
@Cacheable(cacheNames = "ami-expiration-time", cacheManager = "oneMinuteTTLCacheManager")
public ZonedDateTime getExpirationTime(String regionName, String imageOwner, String imageId) {
    // tags are only visible in the owning account of the image
    final AmazonEC2Client ec2 = clientProvider.getClient(AmazonEC2Client.class, imageOwner,
            Region.getRegion(Regions.fromName(regionName)));
    final DescribeTagsRequest tagsRequest = new DescribeTagsRequest().withFilters(
            new Filter("resource-id").withValues(imageId),
            new Filter("resource-type").withValues("image"),
            new Filter("key").withValues(TAG_KEY));
    return ec2.describeTags(tagsRequest).getTags().stream()
            .findFirst()
            .map(TagDescription::getValue)
            .map(value -> ZonedDateTime.parse(value, ISO_DATE_TIME))
            .orElse(null);
}
 
Example #5
Source File: TaupageExpirationTimeProviderImplTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void getExpirationTime() {
    final DescribeTagsResult response = new DescribeTagsResult()
            .withTags(new TagDescription()
                    .withResourceType("image")
                    .withResourceId(IMAGE_ID)
                    .withKey(TaupageExpirationTimeProviderImpl.TAG_KEY)
                    .withValue("2018-06-20T03:00:00+02:00"));

    when(mockEC2Client.describeTags(any(DescribeTagsRequest.class))).thenReturn(response);

    final ZonedDateTime result = expirationTimeProvider.getExpirationTime(REGION_NAME, IMAGE_OWNER, IMAGE_ID);
    assertThat(result).isEqualTo(ZonedDateTime.of(2018, 6, 20, 3, 0, 0, 0, ZoneOffset.ofHours(2)));

    verify(mockClientProvider).getClient(eq(AmazonEC2Client.class), eq(IMAGE_OWNER), eq(getRegion(fromName(REGION_NAME))));
    verify(mockEC2Client).describeTags(
            eq(new DescribeTagsRequest().withFilters(
                    new Filter("resource-id").withValues(IMAGE_ID),
                    new Filter("resource-type").withValues("image"),
                    new Filter("key").withValues(TaupageExpirationTimeProviderImpl.TAG_KEY))));
}
 
Example #6
Source File: AmazonEc2InstanceUserTagsFactoryBean.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #7
Source File: TaupageExpirationTimeProviderImplTest.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Test
public void getAbsentExpirationTime() {
    when(mockEC2Client.describeTags(any(DescribeTagsRequest.class))).thenReturn(new DescribeTagsResult());

    final ZonedDateTime result = expirationTimeProvider.getExpirationTime(REGION_NAME, IMAGE_OWNER, IMAGE_ID);
    assertThat(result).isNull();

    verify(mockClientProvider).getClient(eq(AmazonEC2Client.class), eq(IMAGE_OWNER), eq(getRegion(fromName(REGION_NAME))));
    verify(mockEC2Client).describeTags(
            eq(new DescribeTagsRequest().withFilters(
                    new Filter("resource-id").withValues(IMAGE_ID),
                    new Filter("resource-type").withValues("image"),
                    new Filter("key").withValues(TaupageExpirationTimeProviderImpl.TAG_KEY))));
}
 
Example #8
Source File: AwsUtils.java    From chassis with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: AutoDetectingStackNameProvider.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
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;
	}
 
Example #10
Source File: AmazonEc2InstanceUserTagsFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
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
	assertThat(resultMap.get("keyA")).isEqualTo("valueA");
	assertThat(resultMap.get("keyB")).isEqualTo("valueB");
	assertThat(resultMap.containsKey("keyC")).isFalse();
}
 
Example #11
Source File: TagImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public boolean load(DescribeTagsRequest request) {
    return load(request, null);
}
 
Example #12
Source File: TagImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public boolean load(DescribeTagsRequest request,
        ResultCapture<DescribeTagsResult> extractor) {

    return resource.load(request, extractor);
}
 
Example #13
Source File: Tag.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
 
Example #14
Source File: Tag.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * 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);