AddElasticIpsToNodemetadata implements Function<NodeMetadata, NodeMetadata> { @Override public NodeMetadata apply(NodeMetadata arg0) { String[] parts = AWSUtils.parseHandle(arg0.getId()); String region = parts[0]; String instanceId = parts[1]; try { String publicIp = cache.get(new RegionAndName(region, instanceId)); return NodeMetadataBuilder.fromNodeMetadata(arg0) .publicAddresses(ImmutableSet.<String> builder().add(publicIp).build()).build(); } catch (CacheLoader.InvalidCacheLoadException e) { return arg0; } catch (ExecutionException e) { throw Throwables.propagate(e); } } @Inject protected  AddElasticIpsToNodemetadata(@Named("ELASTICIP") LoadingCache<RegionAndName, String> cache); @Override NodeMetadata apply(NodeMetadata arg0);  }
@Test public void testReturnsNodeWithElasticIpWhenFoundInCacheAndNodeHadAPublicIp() throws Exception { RegionAndName key = new RegionAndName("us-east-1", node.getProviderId()); String val = "1.1.1.1"; LoadingCache<RegionAndName, String> cache = cacheOf(key, val); AddElasticIpsToNodemetadata fn = new AddElasticIpsToNodemetadata(cache); assertEquals(fn.apply(node).getPublicAddresses(), ImmutableSet.of("1.1.1.1")); }
@Test public void testReturnsNodeWithIpWhenFoundInCacheAndNodeHadNoPublicIp() throws Exception { RegionAndName key = new RegionAndName("us-east-1", node.getProviderId()); String val = "1.1.1.1"; LoadingCache<RegionAndName, String> cache = cacheOf(key, val); AddElasticIpsToNodemetadata fn = new AddElasticIpsToNodemetadata(cache); assertEquals(fn.apply( NodeMetadataBuilder.fromNodeMetadata(node).publicAddresses(ImmutableSet.<String> of()).build()) .getPublicAddresses(), ImmutableSet.of("1.1.1.1")); }
@Test public void testReturnsSameNodeWhenNotFoundInCache() throws Exception { RegionAndName key = new RegionAndName("us-east-1", node.getProviderId()); String val = null; LoadingCache<RegionAndName, String> cache = cacheOf(key, val); AddElasticIpsToNodemetadata fn = new AddElasticIpsToNodemetadata(cache); assertEquals(fn.apply(node).getPublicAddresses(), ImmutableSet.of("174.129.173.155")); }