Search in sources :

Example 1 with ListInstanceProfilesRequest

use of com.amazonaws.services.identitymanagement.model.ListInstanceProfilesRequest in project cloudbreak by hortonworks.

the class AwsPlatformResources method getAccessConfigByInstanceProfile.

private Set<CloudAccessConfig> getAccessConfigByInstanceProfile(AmazonIdentityManagementClient client) {
    LOGGER.info("Get all Instance profiles from Amazon");
    String queryFailedMessage = "Could not get instance profiles from Amazon: ";
    try {
        boolean finished = false;
        String marker = null;
        Set<InstanceProfile> instanceProfiles = new LinkedHashSet<>();
        while (!finished) {
            ListInstanceProfilesRequest listInstanceProfilesRequest = new ListInstanceProfilesRequest();
            listInstanceProfilesRequest.setMaxItems(fetchMaxItems);
            if (isNotEmpty(marker)) {
                listInstanceProfilesRequest.setMarker(marker);
            }
            LOGGER.debug("About to fetch instance profiles...");
            ListInstanceProfilesResult listInstanceProfilesResult = client.listInstanceProfiles(listInstanceProfilesRequest);
            List<InstanceProfile> fetchedInstanceProfiles = listInstanceProfilesResult.getInstanceProfiles();
            instanceProfiles.addAll(fetchedInstanceProfiles);
            if (listInstanceProfilesResult.isTruncated()) {
                marker = listInstanceProfilesResult.getMarker();
            } else {
                finished = true;
            }
        }
        LOGGER.debug("The total of {} instance profile(s) has fetched.", instanceProfiles.size());
        return instanceProfiles.stream().map(this::instanceProfileToCloudAccessConfig).collect(Collectors.toSet());
    } catch (AmazonServiceException ase) {
        if (ase.getStatusCode() == UNAUTHORIZED) {
            LOGGER.error("Could not get instance profiles because the user does not have enough permission.", ase);
            throw new CloudUnauthorizedException(ase.getMessage(), ase);
        } else {
            LOGGER.info(queryFailedMessage, ase);
            throw new CloudConnectorException(ase.getMessage(), ase);
        }
    } catch (Exception e) {
        LOGGER.warn(queryFailedMessage, e);
        throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ListInstanceProfilesRequest(com.amazonaws.services.identitymanagement.model.ListInstanceProfilesRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) InstanceProfile(com.amazonaws.services.identitymanagement.model.InstanceProfile) AmazonServiceException(com.amazonaws.AmazonServiceException) ListInstanceProfilesResult(com.amazonaws.services.identitymanagement.model.ListInstanceProfilesResult) CloudUnauthorizedException(com.sequenceiq.cloudbreak.cloud.exception.CloudUnauthorizedException) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) SdkClientException(com.amazonaws.SdkClientException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) PermanentlyFailedException(com.sequenceiq.cloudbreak.util.PermanentlyFailedException) CloudUnauthorizedException(com.sequenceiq.cloudbreak.cloud.exception.CloudUnauthorizedException)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)1 SdkClientException (com.amazonaws.SdkClientException)1 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 InstanceProfile (com.amazonaws.services.identitymanagement.model.InstanceProfile)1 ListInstanceProfilesRequest (com.amazonaws.services.identitymanagement.model.ListInstanceProfilesRequest)1 ListInstanceProfilesResult (com.amazonaws.services.identitymanagement.model.ListInstanceProfilesResult)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudUnauthorizedException (com.sequenceiq.cloudbreak.cloud.exception.CloudUnauthorizedException)1 PermanentlyFailedException (com.sequenceiq.cloudbreak.util.PermanentlyFailedException)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1