openstack-nova: adjusting in case multiple floating ips are possible

This commit is contained in:
Adam Lowe 2012-03-16 18:16:25 +00:00 committed by Adrian Cole
parent a58474a674
commit cc76f96553
1 changed files with 26 additions and 18 deletions

View File

@ -18,49 +18,57 @@
*/
package org.jclouds.openstack.nova.v1_1.compute.functions;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.jclouds.openstack.nova.v1_1.NovaClient;
import org.jclouds.openstack.nova.v1_1.compute.domain.RegionAndName;
import org.jclouds.openstack.nova.v1_1.domain.FloatingIP;
import org.jclouds.openstack.nova.v1_1.extensions.FloatingIPClient;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* @author Adam Lowe
*/
@Singleton
public class LoadFloatingIpForInstanceOrNull extends CacheLoader<RegionAndName, String> {
public class LoadFloatingIpsForInstance extends CacheLoader<RegionAndName, Iterable<String>> {
private final NovaClient client;
private final Iterable<String> regions;
@Inject
public LoadFloatingIpForInstanceOrNull(NovaClient client) {
public LoadFloatingIpsForInstance(NovaClient client) {
this.client = client;
this.regions = client.getConfiguredRegions();
}
@Override
public String load(final RegionAndName key) throws Exception {
Optional<FloatingIPClient> ipClientOptional = client.getFloatingIPExtensionForRegion(key.getRegion());
public Iterable<String> load(final RegionAndName key) throws Exception {
String region = key.getRegion() == null ? regions.iterator().next() : key.getRegion();
Optional<FloatingIPClient> ipClientOptional = client.getFloatingIPExtensionForRegion(region);
if (ipClientOptional.isPresent()) {
try {
return Iterables.find(ipClientOptional.get().listFloatingIPs(),
return Iterables.transform(Iterables.filter(ipClientOptional.get().listFloatingIPs(),
new Predicate<FloatingIP>() {
@Override
public boolean apply(FloatingIP input) {
return key.getName().equals(input.getInstanceId());
}
}).getIp();
} catch (NoSuchElementException e) {
return null;
}),
new Function<FloatingIP, String>() {
@Override
public String apply(FloatingIP input) {
return input.getInstanceId();
}
});
}
return ImmutableSet.of();
}
}
return null;
}
}