From a88bf65134cd059e0ea71a09b47cb10e5b08bdb8 Mon Sep 17 00:00:00 2001 From: Adam Lowe Date: Wed, 4 Jul 2012 18:05:34 +0100 Subject: [PATCH 1/2] openstack-nova: fixing floating IP deallocation in RemoveFloatingUpFromNodeAndDeallocate --- .../config/NovaComputeServiceContextModule.java | 7 ++++--- .../functions/AllocateAndAddFloatingIpToNode.java | 4 ++-- .../RemoveFloatingIpFromNodeAndDeallocate.java | 11 ++++++----- .../compute/loaders/LoadFloatingIpsForInstance.java | 13 ++++--------- .../loaders/LoadFloatingIpsForInstanceTest.java | 7 +++---- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/config/NovaComputeServiceContextModule.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/config/NovaComputeServiceContextModule.java index 7217db047b..addd649956 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/config/NovaComputeServiceContextModule.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/config/NovaComputeServiceContextModule.java @@ -60,6 +60,7 @@ import org.jclouds.openstack.nova.v2_0.compute.loaders.LoadFloatingIpsForInstanc import org.jclouds.openstack.nova.v2_0.compute.options.NovaTemplateOptions; import org.jclouds.openstack.nova.v2_0.compute.predicates.GetImageWhenImageInZoneHasActiveStatusPredicateWithResult; import org.jclouds.openstack.nova.v2_0.compute.strategy.ApplyNovaTemplateOptionsCreateNodesWithGroupEncodedIntoNameThenAddToSet; +import org.jclouds.openstack.nova.v2_0.domain.FloatingIP; import org.jclouds.openstack.nova.v2_0.domain.KeyPair; import org.jclouds.openstack.nova.v2_0.domain.Server; import org.jclouds.openstack.nova.v2_0.domain.zonescoped.FlavorInZone; @@ -128,7 +129,7 @@ public class NovaComputeServiceContextModule extends bind(TemplateOptions.class).to(NovaTemplateOptions.class); - bind(new TypeLiteral>>() { + bind(new TypeLiteral>>() { }).annotatedWith(Names.named("FLOATINGIP")).to(LoadFloatingIpsForInstance.class); bind(new TypeLiteral>() { @@ -162,8 +163,8 @@ public class NovaComputeServiceContextModule extends @Provides @Singleton @Named("FLOATINGIP") - protected LoadingCache> instanceToFloatingIps( - @Named("FLOATINGIP") CacheLoader> in) { + protected LoadingCache> instanceToFloatingIps( + @Named("FLOATINGIP") CacheLoader> in) { return CacheBuilder.newBuilder().build(in); } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/AllocateAndAddFloatingIpToNode.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/AllocateAndAddFloatingIpToNode.java index 3cd8884785..b2bcbb67e1 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/AllocateAndAddFloatingIpToNode.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/AllocateAndAddFloatingIpToNode.java @@ -61,11 +61,11 @@ public class AllocateAndAddFloatingIpToNode implements private final Predicate> nodeRunning; private final NovaClient novaClient; - private final LoadingCache> floatingIpCache; + private final LoadingCache> floatingIpCache; @Inject public AllocateAndAddFloatingIpToNode(@Named(TIMEOUT_NODE_RUNNING) Predicate> nodeRunning, - NovaClient novaClient, @Named("FLOATINGIP") LoadingCache> floatingIpCache) { + NovaClient novaClient, @Named("FLOATINGIP") LoadingCache> floatingIpCache) { this.nodeRunning = checkNotNull(nodeRunning, "nodeRunning"); this.novaClient = checkNotNull(novaClient, "novaClient"); this.floatingIpCache = checkNotNull(floatingIpCache, "floatingIpCache"); diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/RemoveFloatingIpFromNodeAndDeallocate.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/RemoveFloatingIpFromNodeAndDeallocate.java index 0449389268..84fee485be 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/RemoveFloatingIpFromNodeAndDeallocate.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/functions/RemoveFloatingIpFromNodeAndDeallocate.java @@ -27,6 +27,7 @@ import javax.inject.Named; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.logging.Logger; import org.jclouds.openstack.nova.v2_0.NovaClient; +import org.jclouds.openstack.nova.v2_0.domain.FloatingIP; import org.jclouds.openstack.nova.v2_0.domain.zonescoped.ZoneAndId; import org.jclouds.openstack.nova.v2_0.extensions.FloatingIPClient; @@ -46,11 +47,11 @@ public class RemoveFloatingIpFromNodeAndDeallocate implements Function> floatingIpCache; + private final LoadingCache> floatingIpCache; @Inject public RemoveFloatingIpFromNodeAndDeallocate(NovaClient novaClient, - @Named("FLOATINGIP") LoadingCache> floatingIpCache) { + @Named("FLOATINGIP") LoadingCache> floatingIpCache) { this.novaClient = checkNotNull(novaClient, "novaClient"); this.floatingIpCache = checkNotNull(floatingIpCache, "floatingIpCache"); } @@ -58,11 +59,11 @@ public class RemoveFloatingIpFromNodeAndDeallocate implements Function> removing floatingIp(%s) from node(%s)", ip, id); - floatingIpClient.removeFloatingIPFromServer(ip, id.getId()); + floatingIpClient.removeFloatingIPFromServer(ip.getIp(), id.getId()); logger.debug(">> deallocating floatingIp(%s)", ip); - floatingIpClient.deallocate(ip); + floatingIpClient.deallocate(ip.getId()); } floatingIpCache.invalidate(id); return id; diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstance.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstance.java index 40ca3f62fd..c4ebf12e96 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstance.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstance.java @@ -41,7 +41,7 @@ import com.google.common.collect.Iterables; * @author Adam Lowe */ @Singleton -public class LoadFloatingIpsForInstance extends CacheLoader> { +public class LoadFloatingIpsForInstance extends CacheLoader> { private final NovaClient client; @Inject @@ -50,22 +50,17 @@ public class LoadFloatingIpsForInstance extends CacheLoader load(final ZoneAndId key) throws Exception { + public Iterable load(final ZoneAndId key) throws Exception { String zone = key.getZone(); Optional ipClientOptional = client.getFloatingIPExtensionForZone(zone); if (ipClientOptional.isPresent()) { - return Iterables.transform(Iterables.filter(ipClientOptional.get().listFloatingIPs(), + return Iterables.filter(ipClientOptional.get().listFloatingIPs(), new Predicate() { @Override public boolean apply(FloatingIP input) { return key.getId().equals(input.getInstanceId()); } - }), new Function() { - @Override - public String apply(FloatingIP input) { - return input.getIp(); - } - }); + }); } return ImmutableSet.of(); } diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstanceTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstanceTest.java index 3235df533f..0a87bf89ce 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstanceTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/loaders/LoadFloatingIpsForInstanceTest.java @@ -45,18 +45,17 @@ public class LoadFloatingIpsForInstanceTest { public void testReturnsPublicIpOnMatch() throws Exception { NovaClient client = createMock(NovaClient.class); FloatingIPClient ipClient = createMock(FloatingIPClient.class); + FloatingIP testIp = FloatingIP.builder().id("1").ip("1.1.1.1").fixedIp("10.1.1.1").instanceId("i-blah").build(); expect(client.getFloatingIPExtensionForZone("Zone")).andReturn(Optional.of(ipClient)).atLeastOnce(); - expect(ipClient.listFloatingIPs()).andReturn( - ImmutableSet.of(FloatingIP.builder().id("1").ip("1.1.1.1").fixedIp("10.1.1.1").instanceId("i-blah").build())) - .atLeastOnce(); + expect(ipClient.listFloatingIPs()).andReturn(ImmutableSet.of(testIp)).atLeastOnce(); replay(client); replay(ipClient); LoadFloatingIpsForInstance parser = new LoadFloatingIpsForInstance(client); - assertEquals(ImmutableSet.copyOf(parser.load(ZoneAndId.fromZoneAndId("Zone", "i-blah"))), ImmutableSet.of("1.1.1.1")); + assertEquals(ImmutableSet.copyOf(parser.load(ZoneAndId.fromZoneAndId("Zone", "i-blah"))), ImmutableSet.of(testIp)); verify(client); verify(ipClient); From 228e3facfa5038d48f12b337df5061d87f1029ef Mon Sep 17 00:00:00 2001 From: Adam Lowe Date: Wed, 4 Jul 2012 18:27:05 +0100 Subject: [PATCH 2/2] openstack-nova: allowing 'null' ipProtocols returned by hpcloud-compute --- .../org/jclouds/openstack/nova/v2_0/domain/Ingress.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Ingress.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Ingress.java index ed4245ad46..29d47a4612 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Ingress.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v2_0/domain/Ingress.java @@ -24,6 +24,8 @@ import java.beans.ConstructorProperties; import javax.inject.Named; +import org.jclouds.javax.annotation.Nullable; + import com.google.common.annotations.Beta; import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; @@ -104,8 +106,8 @@ public class Ingress { @ConstructorProperties({ "ip_protocol", "from_port", "to_port" }) - protected Ingress(IpProtocol ipProtocol, int fromPort, int toPort) { - this.ipProtocol = checkNotNull(ipProtocol, "ipProtocol"); + protected Ingress(@Nullable IpProtocol ipProtocol, int fromPort, int toPort) { + this.ipProtocol = ipProtocol == null ? IpProtocol.UNRECOGNIZED : ipProtocol; this.fromPort = fromPort; this.toPort = toPort; }