/** * Calculates DynamoDB end-point. * * Algorithm details: * <ol> * <li> Use endpoint in job configuration "dynamodb.endpoint" value if available * <li> Use endpoint from region in job configuration "dynamodb.region" value if available * <li> Use endpoint from region in job configuration "dynamodb.regionid" value if available * <li> Use endpoint from EC2 Metadata of instance if available * <li> If all previous attempts at retrieving endpoint fail, default to us-east-1 endpoint * </ol> * * @param conf Job Configuration * @param region optional preferred region * @return end-point for DynamoDb service */ public static String getDynamoDBEndpoint(Configuration conf, String region) { String endpoint = getValueFromConf(conf, DynamoDBConstants.ENDPOINT); if (Strings.isNullOrEmpty(endpoint)) { if (Strings.isNullOrEmpty(region)) { region = getValueFromConf(conf, DynamoDBConstants.REGION); } if (Strings.isNullOrEmpty(region)) { region = getValueFromConf(conf, DynamoDBConstants.REGION_ID); } if (Strings.isNullOrEmpty(region)) { try { region = EC2MetadataUtils.getEC2InstanceRegion(); } catch (Exception e) { log.warn(String.format("Exception when attempting to get AWS region information. Will " + "ignore and default " + "to %s", DynamoDBConstants.DEFAULT_AWS_REGION), e); } } if (Strings.isNullOrEmpty(region)) { region = DynamoDBConstants.DEFAULT_AWS_REGION; } endpoint = RegionUtils.getRegion(region).getServiceEndpoint(ServiceAbbreviations.Dynamodb); } log.info("Using endpoint for DynamoDB: " + endpoint); return endpoint; }
/** * Tests that we can make an elb client. */ @Test public void testMakeRegionalEC2Client() { AmazonEC2Client ec2 = awsClientFactory.makeRegionalEc2Client(); assertEquals(ServiceAbbreviations.EC2, ec2.getServiceName()); }
/** * Tests that we can make an elb client. */ @Test public void testMakeRegionalELBClient() { AmazonElasticLoadBalancingClient elb = awsClientFactory.makeRegionalElbClient(); assertEquals(ServiceAbbreviations.ElasticLoadbalancing, elb.getServiceName()); }
/** * Tests that we can make an rds client. */ @Test public void testMakeRegionalRDSClient() { AmazonRDSClient rds = awsClientFactory.makeRegionalRdsClient(); assertEquals(ServiceAbbreviations.RDS, rds.getServiceName()); }
@Test public void getsEndpointFromRegion() { when(RegionUtils.getRegion(TEST_REGION)).thenReturn(region); when(region.getServiceEndpoint(ServiceAbbreviations.Dynamodb)).thenReturn(TEST_ENDPOINT); conf.set(DynamoDBConstants.REGION, TEST_REGION); assertEquals(TEST_ENDPOINT, DynamoDBUtil.getDynamoDBEndpoint(conf, null)); verify(region).getServiceEndpoint(ServiceAbbreviations.Dynamodb); }
@Test public void getsEndpointFromEc2InstanceRegion() { when(EC2MetadataUtils.getEC2InstanceRegion()).thenReturn("ec2-instance-region"); when(RegionUtils.getRegion("ec2-instance-region")).thenReturn(region); when(region.getServiceEndpoint(ServiceAbbreviations.Dynamodb)).thenReturn(TEST_ENDPOINT); assertEquals(TEST_ENDPOINT, DynamoDBUtil.getDynamoDBEndpoint(conf, null)); PowerMockito.verifyStatic(); EC2MetadataUtils.getEC2InstanceRegion(); verify(region, never()).getServiceEndpoint(TEST_REGION); }
@Test public void getsEndpointFromDefaultAwsRegion() { PowerMockito.mockStatic(RegionUtils.class); when(EC2MetadataUtils.getEC2InstanceRegion()).thenThrow(new AmazonClientException("Unable to " + "get region from EC2 instance data")); when(RegionUtils.getRegion(DynamoDBConstants.DEFAULT_AWS_REGION)).thenReturn(region); when(region.getServiceEndpoint(ServiceAbbreviations.Dynamodb)).thenReturn(TEST_ENDPOINT); assertEquals(TEST_ENDPOINT, DynamoDBUtil.getDynamoDBEndpoint(conf, null)); PowerMockito.verifyStatic(); RegionUtils.getRegion(DynamoDBConstants.DEFAULT_AWS_REGION); }
@Test public void getsEndpointFromConf() { conf.set(DynamoDBConstants.ENDPOINT, TEST_ENDPOINT); assertEquals(TEST_ENDPOINT, DynamoDBUtil.getDynamoDBEndpoint(conf, null)); verify(region, never()).getServiceEndpoint(ServiceAbbreviations.Dynamodb); }