public AmazonEC2 load(Provider provider) throws Exception {
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;
}