diff --git a/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/compute/functions/LoadFloatingIpForInstanceOrNull.java b/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/compute/functions/LoadFloatingIpsForInstance.java similarity index 57% rename from labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/compute/functions/LoadFloatingIpForInstanceOrNull.java rename to labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/compute/functions/LoadFloatingIpsForInstance.java index 1b2753223a..3fe3c2d629 100644 --- a/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/compute/functions/LoadFloatingIpForInstanceOrNull.java +++ b/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/compute/functions/LoadFloatingIpsForInstance.java @@ -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 { +public class LoadFloatingIpsForInstance extends CacheLoader> { private final NovaClient client; - + private final Iterable 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 ipClientOptional = client.getFloatingIPExtensionForRegion(key.getRegion()); + public Iterable load(final RegionAndName key) throws Exception { + String region = key.getRegion() == null ? regions.iterator().next() : key.getRegion(); + Optional ipClientOptional = client.getFloatingIPExtensionForRegion(region); if (ipClientOptional.isPresent()) { - try { - return Iterables.find(ipClientOptional.get().listFloatingIPs(), - new Predicate() { - @Override - public boolean apply(FloatingIP input) { - return key.getName().equals(input.getInstanceId()); - } + return Iterables.transform(Iterables.filter(ipClientOptional.get().listFloatingIPs(), + new Predicate() { + @Override + public boolean apply(FloatingIP input) { + return key.getName().equals(input.getInstanceId()); + } - }).getIp(); - } catch (NoSuchElementException e) { - return null; - } + }), + new Function() { + @Override + public String apply(FloatingIP input) { + return input.getInstanceId(); + } + }); } - return null; + return ImmutableSet.of(); } - } \ No newline at end of file