Search in sources :

Example 1 with DescribeRegionsRequest

use of com.amazonaws.services.ec2.model.DescribeRegionsRequest in project cloudbreak by hortonworks.

the class AwsCredentialConnector method verifyAccessKeySecretKeyIsAssumable.

private CloudCredentialStatus verifyAccessKeySecretKeyIsAssumable(CloudCredential cloudCredential) {
    AwsCredentialView awsCredential = new AwsCredentialView(cloudCredential);
    try {
        AmazonEC2Client access = awsClient.createAccess(cloudCredential);
        DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
        access.describeRegions(describeRegionsRequest);
    } catch (AmazonClientException ae) {
        String errorMessage = "Unable to verify AWS credentials: please make sure the access key and secret key is correct";
        LOGGER.error(errorMessage, ae);
        return new CloudCredentialStatus(cloudCredential, CredentialStatus.FAILED, ae, errorMessage);
    } catch (RuntimeException e) {
        String errorMessage = String.format("Could not verify keys '%s': check if the keys exists and if it's created with the correct external ID", awsCredential.getAccessKey());
        LOGGER.error(errorMessage, e);
        return new CloudCredentialStatus(cloudCredential, CredentialStatus.FAILED, e, errorMessage);
    }
    return new CloudCredentialStatus(cloudCredential, CredentialStatus.CREATED);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AmazonClientException(com.amazonaws.AmazonClientException) DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)

Example 2 with DescribeRegionsRequest

use of com.amazonaws.services.ec2.model.DescribeRegionsRequest in project photon-model by vmware.

the class AWSReservedInstancePlanService method getRegionAsyncHandler.

private AsyncHandler<DescribeRegionsRequest, DescribeRegionsResult> getRegionAsyncHandler(AWSReservedInstanceContext context) {
    AWSReservedInstancePlanService service = this;
    return new AsyncHandler<DescribeRegionsRequest, DescribeRegionsResult>() {

        @Override
        public void onError(Exception e) {
            log(Level.WARNING, "Error while fetching the regions for compute " + context.computeDesc.documentSelfLink + " " + Utils.toString(e));
        }

        @Override
        public void onSuccess(DescribeRegionsRequest request, DescribeRegionsResult describeRegionsResult) {
            List<Region> regions = describeRegionsResult.getRegions();
            if (CollectionUtils.isEmpty(regions)) {
                log(Level.INFO, "No regions exist for compute" + context.computeDesc.documentSelfLink);
                return;
            }
            AtomicInteger currentStageTaskCount = new AtomicInteger(regions.size());
            /*
                 * Fetch all the regions from AWS and collect reserved instances plans for
                 * only those regions which are supported by SDK.
                 */
            for (Region region : regions) {
                try {
                    Regions r = Regions.fromName(region.getRegionName());
                    if (r == null) {
                        new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context).checkAndPatchReservedInstancesPlans();
                        continue;
                    }
                } catch (Exception e) {
                    log(Level.WARNING, e.getMessage());
                    new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context).checkAndPatchReservedInstancesPlans();
                    continue;
                }
                service.ec2ClientManager.getOrCreateEC2ClientAsync(context.parentAuth, region.getRegionName(), service).whenComplete((ec2Client, t) -> {
                    if (t != null) {
                        getFailureConsumer(context, "Error while creating EC2 client for").accept(t);
                        new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context).checkAndPatchReservedInstancesPlans();
                        logWarning("client is null for region %s for compute %s", region.getRegionName(), context.computeDesc.documentSelfLink);
                        return;
                    }
                    ec2Client.describeReservedInstancesAsync(new AWSReservedInstanceAsyncHandler(service.getHost(), currentStageTaskCount, region, context));
                });
            }
        }
    };
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) DescribeRegionsResult(com.amazonaws.services.ec2.model.DescribeRegionsResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Region(com.amazonaws.services.ec2.model.Region) DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) Regions(com.amazonaws.regions.Regions)

Example 3 with DescribeRegionsRequest

use of com.amazonaws.services.ec2.model.DescribeRegionsRequest in project cloudbreak by hortonworks.

the class AwsPlatformResources method regions.

@Override
@Cacheable(cacheNames = "cloudResourceRegionCache", key = "#cloudCredential?.id")
public CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    AmazonEC2Client ec2Client = awsClient.createAccess(cloudCredential);
    Map<Region, List<AvailabilityZone>> regionListMap = new HashMap<>();
    Map<Region, String> displayNames = new HashMap<>();
    DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
    DescribeRegionsResult describeRegionsResult = ec2Client.describeRegions(describeRegionsRequest);
    String defaultRegion = awsZoneParameterDefault;
    for (com.amazonaws.services.ec2.model.Region awsRegion : describeRegionsResult.getRegions()) {
        if (region == null || Strings.isNullOrEmpty(region.value()) || awsRegion.getRegionName().equals(region.value())) {
            DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest = new DescribeAvailabilityZonesRequest();
            ec2Client.setRegion(RegionUtils.getRegion(awsRegion.getRegionName()));
            Filter filter = new Filter();
            filter.setName("region-name");
            Collection<String> list = new ArrayList<>();
            list.add(awsRegion.getRegionName());
            filter.setValues(list);
            describeAvailabilityZonesRequest.withFilters(filter);
            DescribeAvailabilityZonesResult describeAvailabilityZonesResult = ec2Client.describeAvailabilityZones(describeAvailabilityZonesRequest);
            List<AvailabilityZone> tmpAz = new ArrayList<>();
            for (com.amazonaws.services.ec2.model.AvailabilityZone availabilityZone : describeAvailabilityZonesResult.getAvailabilityZones()) {
                tmpAz.add(availabilityZone(availabilityZone.getZoneName()));
            }
            regionListMap.put(region(awsRegion.getRegionName()), tmpAz);
            DisplayName displayName = regionDisplayNames.get(region(awsRegion.getRegionName()));
            if (displayName == null || Strings.isNullOrEmpty(displayName.value())) {
                displayNames.put(region(awsRegion.getRegionName()), awsRegion.getRegionName());
            } else {
                displayNames.put(region(awsRegion.getRegionName()), displayName.value());
            }
        }
    }
    if (region != null && !Strings.isNullOrEmpty(region.value())) {
        defaultRegion = region.value();
    }
    return new CloudRegions(regionListMap, displayNames, defaultRegion);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) HashMap(java.util.HashMap) DescribeAvailabilityZonesRequest(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesRequest) ArrayList(java.util.ArrayList) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) CloudRegions(com.sequenceiq.cloudbreak.cloud.model.CloudRegions) DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult) DescribeRegionsResult(com.amazonaws.services.ec2.model.DescribeRegionsResult) Filter(com.amazonaws.services.ec2.model.Filter) DisplayName(com.sequenceiq.cloudbreak.cloud.model.DisplayName) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Cacheable(org.springframework.cache.annotation.Cacheable)

Aggregations

DescribeRegionsRequest (com.amazonaws.services.ec2.model.DescribeRegionsRequest)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)2 DescribeRegionsResult (com.amazonaws.services.ec2.model.DescribeRegionsResult)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 Regions (com.amazonaws.regions.Regions)1 DescribeAvailabilityZonesRequest (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesRequest)1 DescribeAvailabilityZonesResult (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 Region (com.amazonaws.services.ec2.model.Region)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)1 AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)1 CloudCredentialStatus (com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)1 CloudRegions (com.sequenceiq.cloudbreak.cloud.model.CloudRegions)1 DisplayName (com.sequenceiq.cloudbreak.cloud.model.DisplayName)1 Region (com.sequenceiq.cloudbreak.cloud.model.Region)1 ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1