mirror of https://github.com/apache/jclouds.git
Merge pull request #696 from aplowe/openstack-nova2
Openstack nova: Fixing floating ip deallocation and an issue with hpcloud compute
This commit is contained in:
commit
bc94e367ce
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue