public Region getRegionByName(String regionName) { try { DescribeRegionsResult regionResult = ec2_.describeRegions(new DescribeRegionsRequest().withRegionNames(regionName)); com.amazonaws.services.ec2.model.Region region = org.excalibur.core.util.Lists2.first(regionResult.getRegions()); return new Region().setEndpoint(region.getEndpoint()).setName(region.getRegionName()); } catch (AmazonClientException exception) { LOG.error("Invalid region name [{}]. Message error: [{}]", regionName, exception.getMessage()); return null; } }
@Override public AmazonEC2 load(Provider provider) { String region = Optional.fromNullable(provider.getOptions().get(ProviderOptions.REGION)) .or(ProviderOptions.DEFAULT_REGION); AWSCredentials credentials = new BasicAWSCredentials(provider.getAccessKey(), provider.getSecretKey()); AmazonEC2 client = new AmazonEC2Client(credentials, new ClientConfiguration() .withUserAgent(PROVISIONR_USER_AGENT)); if (provider.getEndpoint().isPresent()) { LOG.info("Using endpoint {} as configured", provider.getEndpoint().get()); client.setEndpoint(provider.getEndpoint().get()); } else { LOG.info(">> Searching endpoint for region {}", region); DescribeRegionsRequest request = new DescribeRegionsRequest().withRegionNames(region); DescribeRegionsResult result = client.describeRegions(request); checkArgument(result.getRegions().size() == 1, "Invalid region name %s. Expected one result found %s", region, result.getRegions()); LOG.info("<< Using endpoint {} for region {}", result.getRegions().get(0).getEndpoint(), region); client.setEndpoint(result.getRegions().get(0).getEndpoint()); } return client; }
@Override public DescribeRegionsResult describeRegions(DescribeRegionsRequest describeRegionsRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
@Override @Cacheable(cacheNames = "cloudResourceRegionCache", key = "#cloudCredential?.id") public CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters) throws Exception { 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"); List<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); }