openstack-nova: fixing floating IP deallocation in RemoveFloatingUpFromNodeAndDeallocate

This commit is contained in:
Adam Lowe 2012-07-04 18:05:34 +01:00
parent 570f33a387
commit a88bf65134
5 changed files with 19 additions and 23 deletions

View File

@ -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<CacheLoader<ZoneAndId, Iterable<String>>>() {
bind(new TypeLiteral<CacheLoader<ZoneAndId, Iterable<FloatingIP>>>() {
}).annotatedWith(Names.named("FLOATINGIP")).to(LoadFloatingIpsForInstance.class);
bind(new TypeLiteral<Function<ZoneSecurityGroupNameAndPorts, SecurityGroupInZone>>() {
@ -162,8 +163,8 @@ public class NovaComputeServiceContextModule extends
@Provides
@Singleton
@Named("FLOATINGIP")
protected LoadingCache<ZoneAndId, Iterable<String>> instanceToFloatingIps(
@Named("FLOATINGIP") CacheLoader<ZoneAndId, Iterable<String>> in) {
protected LoadingCache<ZoneAndId, Iterable<FloatingIP>> instanceToFloatingIps(
@Named("FLOATINGIP") CacheLoader<ZoneAndId, Iterable<FloatingIP>> in) {
return CacheBuilder.newBuilder().build(in);
}

View File

@ -61,11 +61,11 @@ public class AllocateAndAddFloatingIpToNode implements
private final Predicate<AtomicReference<NodeMetadata>> nodeRunning;
private final NovaClient novaClient;
private final LoadingCache<ZoneAndId, Iterable<String>> floatingIpCache;
private final LoadingCache<ZoneAndId, Iterable<FloatingIP>> floatingIpCache;
@Inject
public AllocateAndAddFloatingIpToNode(@Named(TIMEOUT_NODE_RUNNING) Predicate<AtomicReference<NodeMetadata>> nodeRunning,
NovaClient novaClient, @Named("FLOATINGIP") LoadingCache<ZoneAndId, Iterable<String>> floatingIpCache) {
NovaClient novaClient, @Named("FLOATINGIP") LoadingCache<ZoneAndId, Iterable<FloatingIP>> floatingIpCache) {
this.nodeRunning = checkNotNull(nodeRunning, "nodeRunning");
this.novaClient = checkNotNull(novaClient, "novaClient");
this.floatingIpCache = checkNotNull(floatingIpCache, "floatingIpCache");

View File

@ -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<ZoneAndId
protected Logger logger = Logger.NULL;
private final NovaClient novaClient;
private final LoadingCache<ZoneAndId, Iterable<String>> floatingIpCache;
private final LoadingCache<ZoneAndId, Iterable<FloatingIP>> floatingIpCache;
@Inject
public RemoveFloatingIpFromNodeAndDeallocate(NovaClient novaClient,
@Named("FLOATINGIP") LoadingCache<ZoneAndId, Iterable<String>> floatingIpCache) {
@Named("FLOATINGIP") LoadingCache<ZoneAndId, Iterable<FloatingIP>> floatingIpCache) {
this.novaClient = checkNotNull(novaClient, "novaClient");
this.floatingIpCache = checkNotNull(floatingIpCache, "floatingIpCache");
}
@ -58,11 +59,11 @@ public class RemoveFloatingIpFromNodeAndDeallocate implements Function<ZoneAndId
@Override
public ZoneAndId apply(ZoneAndId id) {
FloatingIPClient floatingIpClient = novaClient.getFloatingIPExtensionForZone(id.getZone()).get();
for (String ip : floatingIpCache.getUnchecked(id)) {
for (FloatingIP ip : floatingIpCache.getUnchecked(id)) {
logger.debug(">> 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;

View File

@ -41,7 +41,7 @@ import com.google.common.collect.Iterables;
* @author Adam Lowe
*/
@Singleton
public class LoadFloatingIpsForInstance extends CacheLoader<ZoneAndId, Iterable<String>> {
public class LoadFloatingIpsForInstance extends CacheLoader<ZoneAndId, Iterable<FloatingIP>> {
private final NovaClient client;
@Inject
@ -50,22 +50,17 @@ public class LoadFloatingIpsForInstance extends CacheLoader<ZoneAndId, Iterable<
}
@Override
public Iterable<String> load(final ZoneAndId key) throws Exception {
public Iterable<FloatingIP> load(final ZoneAndId key) throws Exception {
String zone = key.getZone();
Optional<FloatingIPClient> ipClientOptional = client.getFloatingIPExtensionForZone(zone);
if (ipClientOptional.isPresent()) {
return Iterables.transform(Iterables.filter(ipClientOptional.get().listFloatingIPs(),
return Iterables.filter(ipClientOptional.get().listFloatingIPs(),
new Predicate<FloatingIP>() {
@Override
public boolean apply(FloatingIP input) {
return key.getId().equals(input.getInstanceId());
}
}), new Function<FloatingIP, String>() {
@Override
public String apply(FloatingIP input) {
return input.getIp();
}
});
});
}
return ImmutableSet.of();
}

View File

@ -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.<FloatingIP>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.<FloatingIP>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);