com.amazonaws.services.identitymanagement.model.GetUserResult Java Examples

The following examples show how to use com.amazonaws.services.identitymanagement.model.GetUserResult. 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: AWSClients.java    From aws-codedeploy-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Via the default provider chain (i.e., global keys for this Jenkins instance),  return the account ID for the
 * currently authenticated user.
 * @param proxyHost hostname of the proxy to use (if any)
 * @param proxyPort port of the proxy to use (if any)
 * @return 12-digit account id
 */
public static String getAccountId(String proxyHost, int proxyPort) {

    String arn = "";
    try {
        ClientConfiguration clientCfg = new ClientConfiguration();
        if (proxyHost != null && proxyPort > 0 ) {
            clientCfg.setProxyHost(proxyHost);
            clientCfg.setProxyPort(proxyPort);
        }
        AmazonIdentityManagementClient iam = new AmazonIdentityManagementClient(clientCfg);
        GetUserResult user = iam.getUser();
        arn = user.getUser().getArn();
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().compareTo("AccessDenied") == 0) {
            String msg = e.getMessage();
            int arnIdx = msg.indexOf("arn:aws");
            if (arnIdx != -1) {
                int arnSpace = msg.indexOf(" ", arnIdx);
                arn = msg.substring(arnIdx, arnSpace);
            }
        }
    }

    String accountId = arn.split(":")[ARN_ACCOUNT_ID_INDEX];
    return accountId;
}
 
Example #2
Source File: AmazonRdsDataSourceUserTagsFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void getObject_instanceWithTagsConfiguredWithCustomResourceResolverAndCustomRegion_mapWithTagsReturned()
		throws Exception {
	// Arrange
	AmazonRDS amazonRds = mock(AmazonRDS.class);
	ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class);
	AmazonIdentityManagement amazonIdentityManagement = mock(
			AmazonIdentityManagement.class);
	AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean(
			amazonRds, "test", amazonIdentityManagement);
	factoryBean.setResourceIdResolver(resourceIdResolver);
	factoryBean.setRegion(Region.getRegion(Regions.EU_WEST_1));

	when(resourceIdResolver.resolveToPhysicalResourceId("test"))
			.thenReturn("stack-test");
	when(amazonIdentityManagement.getUser()).thenReturn(
			new GetUserResult().withUser(new User("/", "aemruli", "123456789012",
					"arn:aws:iam::1234567890:user/aemruli", new Date())));
	when(amazonRds.listTagsForResource(new ListTagsForResourceRequest()
			.withResourceName("arn:aws:rds:eu-west-1:1234567890:db:stack-test")))
					.thenReturn(new ListTagsForResourceResult().withTagList(
							new Tag().withKey("key1").withValue("value1"),
							new Tag().withKey("key2").withValue("value2")));

	// Act
	factoryBean.afterPropertiesSet();
	Map<String, String> userTagMap = factoryBean.getObject();

	// Assert
	assertThat(userTagMap.get("key1")).isEqualTo("value1");
	assertThat(userTagMap.get("key2")).isEqualTo("value2");
}
 
Example #3
Source File: AmazonRdsDataSourceUserTagsFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void getObject_instanceWithOutTags_emptyMapReturned() throws Exception {
	// Arrange
	AmazonRDS amazonRds = mock(AmazonRDS.class);
	ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class);
	AmazonIdentityManagement amazonIdentityManagement = mock(
			AmazonIdentityManagement.class);
	AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean(
			amazonRds, "test", amazonIdentityManagement);
	factoryBean.setResourceIdResolver(resourceIdResolver);
	factoryBean.setResourceIdResolver(resourceIdResolver);
	factoryBean.setRegion(Region.getRegion(Regions.EU_WEST_1));

	when(resourceIdResolver.resolveToPhysicalResourceId("test"))
			.thenReturn("stack-test");
	when(amazonIdentityManagement.getUser()).thenReturn(
			new GetUserResult().withUser(new User("/", "aemruli", "123456789012",
					"arn:aws:iam::1234567890:user/aemruli", new Date())));
	when(amazonRds.listTagsForResource(new ListTagsForResourceRequest()
			.withResourceName("arn:aws:rds:eu-west-1:1234567890:db:stack-test")))
					.thenReturn(new ListTagsForResourceResult());

	// Act
	factoryBean.afterPropertiesSet();
	Map<String, String> userTagMap = factoryBean.getObject();

	// Assert
	assertThat(userTagMap.isEmpty()).isTrue();
}
 
Example #4
Source File: AmazonRdsDataSourceUserTagsFactoryBeanTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void getObject_instanceWithTagsAndNoResourceIdResolverAndDefaultRegion_mapWithTagsReturned()
		throws Exception {
	// Arrange
	AmazonRDS amazonRds = mock(AmazonRDS.class);
	AmazonIdentityManagement amazonIdentityManagement = mock(
			AmazonIdentityManagement.class);

	AmazonRdsDataSourceUserTagsFactoryBean factoryBean = new AmazonRdsDataSourceUserTagsFactoryBean(
			amazonRds, "test", amazonIdentityManagement);

	when(amazonIdentityManagement.getUser()).thenReturn(
			new GetUserResult().withUser(new User("/", "aemruli", "123456789012",
					"arn:aws:iam::1234567890:user/aemruli", new Date())));
	when(amazonRds.listTagsForResource(new ListTagsForResourceRequest()
			.withResourceName("arn:aws:rds:us-west-2:1234567890:db:test")))
					.thenReturn(new ListTagsForResourceResult().withTagList(
							new Tag().withKey("key1").withValue("value1"),
							new Tag().withKey("key2").withValue("value2")));

	// Act
	factoryBean.afterPropertiesSet();
	Map<String, String> userTagMap = factoryBean.getObject();

	// Assert
	assertThat(userTagMap.get("key1")).isEqualTo("value1");
	assertThat(userTagMap.get("key2")).isEqualTo("value2");
}
 
Example #5
Source File: UserImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public boolean load(GetUserRequest request, ResultCapture<GetUserResult>
        extractor) {

    return resource.load(request, extractor);
}
 
Example #6
Source File: User.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>User</code> resource, and any conflicting parameter value set in
 * the request will be overridden:
 * <ul>
 *   <li>
 *     <b><code>UserName</code></b>
 *         - mapped from the <code>Name</code> identifier.
 *   </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 GetUserRequest
 */
boolean load(GetUserRequest request, ResultCapture<GetUserResult> extractor)
        ;