public ListBoxModel doFillRegionItems(@QueryParameter final String credentialsId, @QueryParameter final String region) throws IOException, ServletException { final List<Region> regionList; try { final AmazonEC2 client = connect(credentialsId, null); final DescribeRegionsResult regions=client.describeRegions(); regionList=regions.getRegions(); } catch(final Exception ex) { //Ignore bad exceptions return new ListBoxModel(); } final ListBoxModel model = new ListBoxModel(); for(final Region reg : regionList) { model.add(new ListBoxModel.Option(reg.getRegionName(), reg.getRegionName())); } return model; }
private static String getEndpointForRegion(AmazonEC2Client client, String regionName) { requireNonNull(client, "client is null"); requireNonNull(regionName, "regionName is null"); LOG.info(">> Describing all regions to find endpoint for '{}'", regionName); DescribeRegionsResult result = client.describeRegions(); List<String> regions = Lists.newArrayListWithExpectedSize(result.getRegions().size()); for (Region candidate : result.getRegions()) { regions.add(candidate.getRegionName()); if (candidate.getRegionName().equals(regionName)) { LOG.info("<< Found endpoint '{}' for region '{}'", candidate.getEndpoint(), regionName); return candidate.getEndpoint(); } } throw new IllegalArgumentException(String.format("Unable to find an endpoint for region '%s'. " + "Choose one of the following regions: %s", regionName, Joiner.on(", ").join(regions))); }
public ListBoxModel doFillRegionItems(@QueryParameter boolean useInstanceProfileForCredentials, @QueryParameter String accessId, @QueryParameter String secretKey, @QueryParameter String region) throws IOException, ServletException { ListBoxModel model = new ListBoxModel(); if (testMode) { model.add(DEFAULT_EC2_HOST); return model; } if (useInstanceProfileForCredentials || (!StringUtils.isEmpty(accessId) && !StringUtils.isEmpty(secretKey))) { AWSCredentialsProvider credentialsProvider = createCredentialsProvider(useInstanceProfileForCredentials, accessId, secretKey); AmazonEC2 client = connect(credentialsProvider, new URL("http://ec2.amazonaws.com")); DescribeRegionsResult regions = client.describeRegions(); List<Region> regionList = regions.getRegions(); for (Region r : regionList) { String name = r.getRegionName(); model.add(name, name); } } return model; }
public static void main(String[] args) { final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); DescribeRegionsResult regions_response = ec2.describeRegions(); for(Region region : regions_response.getRegions()) { System.out.printf( "Found region %s " + "with endpoint %s", region.getRegionName(), region.getEndpoint()); } DescribeAvailabilityZonesResult zones_response = ec2.describeAvailabilityZones(); for(AvailabilityZone zone : zones_response.getAvailabilityZones()) { System.out.printf( "Found availability zone %s " + "with status %s " + "in region %s", zone.getZoneName(), zone.getState(), zone.getRegionName()); } }
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; } }
public HashMap<Region, List> listRegionInstances(AWSCredentials awsCreds) { AmazonEC2Client ec2Client = new AmazonEC2Client(awsCreds); List<Region> regions = new ArrayList(); DescribeRegionsResult descRegionsResult = ec2Client.describeRegions(); if (descRegionsResult != null) { regions = descRegionsResult.getRegions(); } HashMap<Region, List> regionInstances = new HashMap(); ExecutorService listInstanceExecutor = Executors.newFixedThreadPool(8); for (Region region : regions) { List<Instance> instances = new ArrayList(); regionInstances.put(region, instances); Runnable worker = new ListInstanceRunnable(awsCreds, region, instances); listInstanceExecutor.execute(worker); } listInstanceExecutor.shutdown(); try { listInstanceExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { LOG.error("Caught InterruptedException: " + e.getMessage()); } return regionInstances; }
public HashMap<Region, List> listRegionVpcs(AWSCredentials awsCreds) { AmazonEC2Client ec2Client = new AmazonEC2Client(awsCreds); List<Region> regions = new ArrayList(); DescribeRegionsResult descRegionsResult = ec2Client.describeRegions(); if (descRegionsResult != null) { regions = descRegionsResult.getRegions(); } HashMap<Region, List> regionVpcs = new HashMap(); ExecutorService listVPCExecutor = Executors.newFixedThreadPool(8); for (Region region : regions) { List<Vpc> vpcs = new ArrayList(); regionVpcs.put(region, vpcs); Runnable worker = new ListVPCRunnable(awsCreds, region, vpcs); listVPCExecutor.execute(worker); } listVPCExecutor.shutdown(); try { listVPCExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { LOG.error("Caught InterruptedException: " + e.getMessage()); } return regionVpcs; }
public HashMap<Region, List> listRegionSubnets(AWSCredentials awsCreds) { AmazonEC2Client ec2Client = new AmazonEC2Client(awsCreds); List<Region> regions = new ArrayList(); DescribeRegionsResult descRegionsResult = ec2Client.describeRegions(); if (descRegionsResult != null) { regions = descRegionsResult.getRegions(); } HashMap<Region, List> regionSubnetsMap = new HashMap(); ExecutorService listSubnetExecutor = Executors.newFixedThreadPool(8); for (Region region : regions) { List<Subnet> subnets = new ArrayList(); regionSubnetsMap.put(region, subnets); Runnable worker = new ListSubnetRunnable(awsCreds, region, subnets); listSubnetExecutor.execute(worker); } listSubnetExecutor.shutdown(); try { listSubnetExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { LOG.error("Caught InterruptedException: " + e.getMessage()); } return regionSubnetsMap; }
public HashMap<Region, List> listRegionRouteTables(AWSCredentials awsCreds) { AmazonEC2Client ec2Client = new AmazonEC2Client(awsCreds); List<Region> regions = new ArrayList(); DescribeRegionsResult descRegionsResult = ec2Client.describeRegions(); if (descRegionsResult != null) { regions = descRegionsResult.getRegions(); } HashMap<Region, List> regionRouteTablesMap = new HashMap(); ExecutorService listRouteTablesExecutor = Executors.newFixedThreadPool(8); for (Region region : regions) { List<RouteTable> routeTables = new ArrayList(); regionRouteTablesMap.put(region, routeTables); Runnable worker = new ListRouteTableRunnable(awsCreds, region, routeTables); listRouteTablesExecutor.execute(worker); } listRouteTablesExecutor.shutdown(); try { listRouteTablesExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { LOG.error("Caught InterruptedException: " + e.getMessage()); } return regionRouteTablesMap; }
public BaseAction(String[] args, AWSCredentials awsCreds) { this.args = args; this.awsCreds = awsCreds; ec2Client = new AmazonEC2Client(awsCreds); DescribeRegionsResult descRegionsResult = ec2Client.describeRegions(); if (descRegionsResult != null) { regions = descRegionsResult.getRegions(); } }
@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 public DescribeRegionsResult describeRegions() 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); }