use of com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder in project photon-model by vmware.
the class AWSEndpointAdapterService method getAccountId.
/**
* Method gets the aws accountId from the specified credentials.
* @param privateKeyId
* @param privateKey
* @return account ID
*/
private String getAccountId(String privateKeyId, String privateKey) {
AWSCredentials awsCredentials = new BasicAWSCredentials(privateKeyId, privateKey);
AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
AmazonIdentityManagementClientBuilder amazonIdentityManagementClientBuilder = AmazonIdentityManagementClientBuilder.standard().withCredentials(awsStaticCredentialsProvider).withRegion(Regions.DEFAULT_REGION);
AmazonIdentityManagementClient iamClient = (AmazonIdentityManagementClient) amazonIdentityManagementClientBuilder.build();
String userId = null;
try {
if ((iamClient.getUser() != null) && (iamClient.getUser().getUser() != null) && (iamClient.getUser().getUser().getArn() != null)) {
return getAccountId(iamClient.getUser().getUser().getArn());
}
} catch (AmazonServiceException ex) {
if (ex.getErrorCode().compareTo("AccessDenied") == 0) {
String msg = ex.getMessage();
userId = msg.split(":", 7)[5];
} else {
logSevere("Exception getting the accountId %s", ex);
}
}
return userId;
}
Aggregations