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 AWSReservedInstanceAsyncHandler(ServiceHost host, AtomicInteger count, Region region, AWSReservedInstanceContext context) { this.count = count; this.region = region; this.host = host; this.context = context; }
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()); } }
@Override protected Region convertObject(RegionInfo from) { Region to = new Region(); to.setRegionName(from.getName()); to.setEndpoint(from.getUrl()); return to; }
private String findRegion() { for (Region region : amazonEC2.describeRegions().getRegions()) { if (this.availabilityZone.startsWith(region.getRegionName())) { return region.getRegionName(); } } throw new BootstrapException("Unable to determine region"); }
private static com.amazonaws.regions.Region eqRegion(final com.amazonaws.regions.Region region) { EasyMock.reportMatcher(new IArgumentMatcher() { @Override public boolean matches(Object o) { com.amazonaws.regions.Region givenRegion = (com.amazonaws.regions.Region) o; return givenRegion.getName().equals(region.getName()); } @Override public void appendTo(StringBuffer stringBuffer) { stringBuffer.append("eqReqion for ").append(region.getName()); } }); return region; }
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 ListInstanceRunnable(AWSCredentials awsCreds, Region region, List<Instance> instances) { this.region = region; this.instances = instances; ec2Client = new AmazonEC2Client(awsCreds); ec2Client.setEndpoint(region.getEndpoint()); LOG.debug("Set endpoint to " + region.getEndpoint()); }
public List<Vpc> listVpcs(AWSCredentials awsCreds) { List<Vpc> vpcs = new ArrayList(); HashMap<Region, List> regionVpcMap = listRegionVpcs(awsCreds); for (List<Vpc> regionVpcs : regionVpcMap.values()) { vpcs.addAll(regionVpcs); } return vpcs; }
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 List<Subnet> listSubnets(AWSCredentials awsCreds) { List<Subnet> subnets = new ArrayList(); HashMap<Region, List> regionSubnetMap = listRegionSubnets(awsCreds); for (List<Subnet> regionSubnets : regionSubnetMap.values()) { subnets.addAll(regionSubnets); } return subnets; }
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 ListVPCRunnable(AWSCredentials awsCreds, Region region, List<Vpc> vpcs) { this.region = region; this.vpcs = vpcs; ec2Client = new AmazonEC2Client(awsCreds); ec2Client.setEndpoint(region.getEndpoint()); LOG.debug("Set endpoint to " + region.getEndpoint()); }
public ListSubnetRunnable(AWSCredentials awsCreds, Region region, List<Subnet> subnets) { this.region = region; this.subnets = subnets; ec2Client = new AmazonEC2Client(awsCreds); ec2Client.setEndpoint(region.getEndpoint()); LOG.debug("Set endpoint to " + region.getEndpoint()); }
public ListRouteTableRunnable(AWSCredentials awsCreds, Region region, List<RouteTable> routeTables) { this.region = region; this.routeTables = routeTables; ec2Client = new AmazonEC2Client(awsCreds); ec2Client.setEndpoint(region.getEndpoint()); LOG.debug("Set endpoint to " + region.getEndpoint()); }
private void initAmazonClients() { amazonEC2.setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(this.region))); amazonElasticLoadBalancing.setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(this.region))); }
public AWSDatabaseHolder(Config config) { maxAgeInMs = config.getDuration("maxAge", TimeUnit.MILLISECONDS); final DefaultAWSCredentialsProviderChain awsCredentialsProviderChain = new DefaultAWSCredentialsProviderChain(); final ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setRetryPolicy(new RetryPolicy(null, null, config.getInt("maxErrorRetry"), true)); final AmazonEC2Client bootstrapEC2Client = new AmazonEC2Client(awsCredentialsProviderChain); ec2Clients = Maps.newHashMap(); rdsClients = Maps.newHashMap(); sqsClients = Maps.newHashMap(); dynamoDBClients = Maps.newHashMap(); elasticacheClients = Maps.newHashMap(); final List<Region> ec2Regions = bootstrapEC2Client.describeRegions().getRegions(); for (Region region : ec2Regions) { final String regionName = region.getRegionName(); final String endpoint = region.getEndpoint(); log.debug("Adding ec2 region {}", region); final AmazonEC2Client ec2Client = new AmazonEC2Client(awsCredentialsProviderChain, clientConfig); ec2Client.setEndpoint(endpoint); ec2Clients.put(regionName, ec2Client); final AmazonRDSClient rdsClient = new AmazonRDSClient(awsCredentialsProviderChain, clientConfig); rdsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "rds.")); rdsClients.put(regionName, rdsClient); final AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(awsCredentialsProviderChain, clientConfig); dynamoDBClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "dynamodb.")); dynamoDBClients.put(regionName, dynamoDBClient); final AmazonSQSClient sqsClient = new AmazonSQSClient(awsCredentialsProviderChain, clientConfig); sqsClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "sqs.")); sqsClients.put(regionName, sqsClient); final AmazonElastiCacheClient elastiCacheClient = new AmazonElastiCacheClient (awsCredentialsProviderChain, clientConfig); elastiCacheClient.setEndpoint(endpoint.replaceFirst("ec2\\.", "elasticache.")); elasticacheClients.put(regionName, elastiCacheClient); } this.iamClient = new AmazonIdentityManagementClient(awsCredentialsProviderChain, clientConfig); if (config.hasPath("accountNumber")) { this.awsAccountNumber = config.getString("accountNumber"); } else { this.awsAccountNumber = null; } rebuild(); }
public Region getRegion() { return region; }
public void setRegion(Region region) { this.region = region; }
private void launchInstances(List<VPNEndpoint> vpnEndpoints) throws Exception { ApplicationConfig appConfig = ApplicationConfig.getInstance(); for (VPNEndpoint vpnEndpoint : vpnEndpoints) { Region region = vpnEndpoint.getRegion(); ec2Client.setEndpoint(region.getEndpoint()); // Get the AMI for the region String amiKey = "ami." + region.getRegionName(); String amiId = appConfig.get(amiKey); if (amiId == null) { String msg = "Unable to find AMI in " + region.getRegionName(); LOG.error(msg); throw new RuntimeException(msg); } // Get the security group for the instance String securityGroupId = vpnEndpoint.getSecurityGroupId(); List<String> securityGroupIds = new ArrayList(); securityGroupIds.add(securityGroupId); // Setup the instance request object LOG.debug("Setting up RunInstancesRequest for instance in " + vpnEndpoint.getVpc().getCidrBlock() + " - " + region.getEndpoint()); RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); runInstancesRequest.setMinCount(1); runInstancesRequest.setMaxCount(1); runInstancesRequest.setImageId(amiId); runInstancesRequest.setSecurityGroupIds(securityGroupIds); runInstancesRequest.setInstanceType(InstanceType.T1Micro); // TODO: Make this configurable runInstancesRequest.setSubnetId(vpnEndpoint.getSubnet().getSubnetId()); runInstancesRequest.setUserData(generateCloudInitScript(vpnEndpoint, vpnEndpoints)); //runInstancesRequest.setKeyName("amazon"); // TODO: Remove this or make this configurable // Launch the instance LOG.debug("Issuing runInstances with: " + runInstancesRequest); RunInstancesResult result = ec2Client.runInstances(runInstancesRequest); Reservation reservation = result.getReservation(); Instance instance = reservation.getInstances().get(0); // Should be just one vpnEndpoint.setInstance(instance); LOG.debug("Launched instance: " + instance); } }