* 'master' of https://github.com/grkvlt/jclouds:
  The describeAddressesInRegion call returns empty set on not found, not exception
  PublicIpInstanceIdPair has different id format to node
  Issue 757: Remove old public IPs when associating  elastic IP addresses
This commit is contained in:
Adrian Cole 2011-12-21 03:46:00 -08:00
commit 20f3e52731
3 changed files with 30 additions and 28 deletions

View File

@ -53,7 +53,11 @@ public class AddElasticIpsToNodemetadata implements Function<NodeMetadata, NodeM
this.cache = checkNotNull(cache, "cache"); this.cache = checkNotNull(cache, "cache");
} }
//TODO: can there be multiple elastic ips on one instance? // Note: Instances only have one Internet routable IP address. When an Elastic IP is associated to an
// instance, the instance's existing Public IP address mapping is removed and is no longer valid for this instance
// http://aws.amazon.com/articles/1346
// TODO can there be multiple elastic ips on one instance?
@Override @Override
public NodeMetadata apply(NodeMetadata arg0) { public NodeMetadata apply(NodeMetadata arg0) {
String[] parts = AWSUtils.parseHandle(arg0.getId()); String[] parts = AWSUtils.parseHandle(arg0.getId());
@ -61,8 +65,9 @@ public class AddElasticIpsToNodemetadata implements Function<NodeMetadata, NodeM
String instanceId = parts[1]; String instanceId = parts[1];
try { try {
String publicIp = cache.get(new RegionAndName(region, instanceId)); String publicIp = cache.get(new RegionAndName(region, instanceId));
return NodeMetadataBuilder.fromNodeMetadata(arg0).publicAddresses( // Replace existing public addresses with elastic IP (see note above)
ImmutableSet.<String> builder().addAll(arg0.getPublicAddresses()).add(publicIp).build()).build(); return NodeMetadataBuilder.fromNodeMetadata(arg0)
.publicAddresses(ImmutableSet.<String> builder().add(publicIp).build()).build();
} catch (CacheLoader.InvalidCacheLoadException e) { } catch (CacheLoader.InvalidCacheLoadException e) {
// no ip was found // no ip was found
return arg0; return arg0;

View File

@ -18,14 +18,11 @@
*/ */
package org.jclouds.ec2.compute; package org.jclouds.ec2.compute;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import com.google.common.base.Splitter;
import org.jclouds.compute.BaseComputeServiceLiveTest; import org.jclouds.compute.BaseComputeServiceLiveTest;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.compute.ComputeServiceContextFactory;
@ -69,6 +66,8 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.inject.Module; import com.google.inject.Module;
import static org.testng.Assert.*;
/** /**
* *
* @author Adrian Cole * @author Adrian Cole
@ -175,7 +174,7 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
} }
} }
@Test(enabled = true, dependsOnMethods = "testCompareSizes") @Test(enabled = true) //, dependsOnMethods = "testCompareSizes")
public void testAutoIpAllocation() throws Exception { public void testAutoIpAllocation() throws Exception {
ComputeServiceContext context = null; ComputeServiceContext context = null;
String group = this.group + "aip"; String group = this.group + "aip";
@ -189,42 +188,40 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
// create a node // create a node
Set<? extends NodeMetadata> nodes = Set<? extends NodeMetadata> nodes =
context.getComputeService().createNodesInGroup(group, 1); context.getComputeService().createNodesInGroup(group, 1);
assertTrue(nodes.size() == 1); assertEquals(nodes.size(), 1, "One node should have been created");
// Get public IPs (on EC2 we should get 2, on Nova only 1) // Get public IPs (We should get 1)
NodeMetadata node = Iterables.get(nodes, 0); NodeMetadata node = Iterables.get(nodes, 0);
String region = node.getLocation().getParent().getId(); String region = node.getLocation().getParent().getId();
Set<String> publicIps = node.getPublicAddresses(); Set<String> publicIps = node.getPublicAddresses();
assertFalse(Iterables.isEmpty(publicIps), String.format("no public addresses attached to node %s", node));
assertEquals(Iterables.size(publicIps), 1);
// Check that all ips are public and port 22 is accessible // Check that the address is public and port 22 is accessible
for (String ip: publicIps) { String ip = Iterables.getOnlyElement(publicIps);
assertFalse(InetAddresses2.isPrivateIPAddress(ip)); assertFalse(InetAddresses2.isPrivateIPAddress(ip));
IPSocket socket = new IPSocket(ip, 22); IPSocket socket = new IPSocket(ip, 22);
assert socketTester.apply(socket) : String.format("failed to open socket %s on node %s", socket, assertTrue(socketTester.apply(socket), String.format("failed to open socket %s on node %s", socket, node));
node);
}
// check that there is an elastic ip correlating to it // check that there is an elastic ip correlating to it
EC2Client ec2 = EC2Client.class.cast(context.getProviderSpecificContext().getApi()); EC2Client ec2 = EC2Client.class.cast(context.getProviderSpecificContext().getApi());
Set<PublicIpInstanceIdPair> ipidpairs = Set<PublicIpInstanceIdPair> ipidpairs =
ec2.getElasticIPAddressServices().describeAddressesInRegion(region, publicIps.toArray(new String[0])); ec2.getElasticIPAddressServices().describeAddressesInRegion(region, publicIps.toArray(new String[0]));
assert (ipidpairs.size() == 1) : ipidpairs; assertEquals(ipidpairs.size(), 1, String.format("there should only be one address pair (%s)",
Iterables.toString(ipidpairs)));
// check that the elastic ip is in node.publicAddresses // check that the elastic ip is in node.publicAddresses
PublicIpInstanceIdPair ipidpair = Iterables.get(ipidpairs, 0); PublicIpInstanceIdPair ipidpair = Iterables.get(ipidpairs, 0);
assertTrue(ipidpair.getInstanceId() == node.getId()); assertEquals(region + "/" + ipidpair.getInstanceId(), node.getId());
// delete the node // delete the node
context.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group)); context.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group));
// check that the ip is deallocated // check that the ip is deallocated
try { Set<PublicIpInstanceIdPair> ipidcheck =
ec2.getElasticIPAddressServices().describeAddressesInRegion(region, ipidpair.getPublicIp()); ec2.getElasticIPAddressServices().describeAddressesInRegion(region, ipidpair.getPublicIp());
assert false; assertTrue(Iterables.isEmpty(ipidcheck), String.format("there should be no address pairs (%s)",
} catch (HttpResponseException e) { Iterables.toString(ipidcheck)));
// do nothing. .. it is expected to fail.
}
} finally { } finally {
context.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group)); context.getComputeService().destroyNodesMatching(NodePredicates.inGroup(group));
if (context != null) if (context != null)

View File

@ -45,14 +45,14 @@ public class AddElasticIpsToNodemetadataTest {
.build(); .build();
@Test @Test
public void testReturnsNodeWithExtraIpWhenFoundInCacheAndNodeHadAPublicIp() throws Exception { public void testReturnsNodeWithElasticIpWhenFoundInCacheAndNodeHadAPublicIp() throws Exception {
RegionAndName key = new RegionAndName("us-east-1", node.getProviderId()); RegionAndName key = new RegionAndName("us-east-1", node.getProviderId());
String val = "1.1.1.1"; String val = "1.1.1.1";
LoadingCache<RegionAndName, String> cache = cacheOf(key, val); LoadingCache<RegionAndName, String> cache = cacheOf(key, val);
AddElasticIpsToNodemetadata fn = new AddElasticIpsToNodemetadata(cache); AddElasticIpsToNodemetadata fn = new AddElasticIpsToNodemetadata(cache);
assertEquals(fn.apply(node).getPublicAddresses(), ImmutableSet.of("174.129.173.155", "1.1.1.1")); assertEquals(fn.apply(node).getPublicAddresses(), ImmutableSet.of("1.1.1.1"));
} }
@Test @Test