query across all keystone services of type, as opposed to chosing the first one

This commit is contained in:
Adrian Cole 2012-07-02 19:40:27 -07:00
parent fb98ce82f7
commit 3fd65f25ce
4 changed files with 57 additions and 44 deletions

View File

@ -22,9 +22,12 @@ import static com.google.common.base.Objects.equal;
import static com.google.common.base.Objects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import java.beans.ConstructorProperties;
import java.util.Set;
import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.ImmutableSet;
/**
@ -36,7 +39,7 @@ import com.google.common.collect.ImmutableSet;
* @see <a href="http://docs.openstack.org/api/openstack-typeentity-service/2.0/content/Identity-Service-Concepts-e1362.html"
* />
*/
public class Service implements Comparable<Service> {
public class Service extends ForwardingSet<Endpoint> implements Comparable<Service> {
public static Builder builder() {
return new Builder();
@ -91,16 +94,11 @@ public class Service implements Comparable<Service> {
}
}
protected Service() {
// we want serializers like Gson to work w/o using sun.misc.Unsafe,
// prohibited in GAE. This also implies fields are not final.
// see http://code.google.com/p/jclouds/issues/detail?id=925
}
protected String type;
protected String name;
protected Set<Endpoint> endpoints = ImmutableSet.of();
protected final String type;
protected final String name;
protected final Set<Endpoint> endpoints;
@ConstructorProperties({ "type", "name", "endpoints" })
public Service(String type, String name, Set<Endpoint> endpoints) {
this.type = checkNotNull(type, "type");
this.name = checkNotNull(name, "name");
@ -155,11 +153,15 @@ public class Service implements Comparable<Service> {
@Override
public int compareTo(Service that) {
if (that == null)
return 1;
if (this == that)
return 0;
return this.type.compareTo(that.type);
return ComparisonChain.start()
.compare(this.type, that.type)
.compare(this.name, that.name)
.result();
}
@Override
protected Set<Endpoint> delegate() {
return endpoints;
}
}

View File

@ -21,6 +21,7 @@ package org.jclouds.openstack.keystone.v2_0.suppliers;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -35,6 +36,7 @@ import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.inject.assistedinject.Assisted;
@Singleton
@ -59,32 +61,36 @@ public class LocationIdToURIFromAccessForTypeAndVersion implements Supplier<Map<
@Override
public Map<String, Supplier<URI>> get() {
Access accessResponse = access.get();
Service service = null;
try {
service = Iterables.find(accessResponse.getServiceCatalog(), new Predicate<Service>() {
Set<Service> services = Sets.filter(accessResponse.getServiceCatalog(), new Predicate<Service>() {
@Override
public boolean apply(Service input) {
return input.getType().equals(apiType);
@Override
public boolean apply(Service input) {
return input.getType().equals(apiType);
}
});
if (services.size() == 0)
throw new NoSuchElementException(String.format("apiType %s not found in catalog %s", apiType,
accessResponse.getServiceCatalog()));
Iterable<Endpoint> endpoints = Iterables.filter(Iterables.concat(services), new Predicate<Endpoint>() {
@Override
public boolean apply(Endpoint input) {
if (input.getVersionId() == null) {
return true;
}
return input.getVersionId().equals(apiVersion);
}
});
} catch (NoSuchElementException e) {
throw new NoSuchElementException(String.format("apiType %s not found in catalog %s", apiType, accessResponse
.getServiceCatalog()));
}
Map<String, Endpoint> locationIdToEndpoint = Maps.uniqueIndex(Iterables.filter(service.getEndpoints(),
new Predicate<Endpoint>() {
});
@Override
public boolean apply(Endpoint input) {
if (input.getVersionId() == null) {
return true;
}
return input.getVersionId().equals(apiVersion);
}
if (Iterables.size(endpoints) == 0)
throw new NoSuchElementException(String.format(
"no endpoints for apiType %s are of version %s, or version agnostic: %s", apiType, apiVersion,
services));
}), endpointToLocationId);
Map<String, Endpoint> locationIdToEndpoint = Maps.uniqueIndex(endpoints, endpointToLocationId);
return Maps.transformValues(locationIdToEndpoint, endpointToSupplierURI);
}

View File

@ -59,7 +59,6 @@ public class RegionIdToURIFromAccessForTypeAndVersionTest {
RegionIdToURISupplier.Factory.class));
}
@SuppressWarnings("unused")
@Provides
@Singleton
public Supplier<Access> provide() {
@ -67,9 +66,13 @@ public class RegionIdToURIFromAccessForTypeAndVersionTest {
}
}).getInstance(RegionIdToURISupplier.Factory.class);
@Test(expectedExceptions = NoSuchElementException.class)
public void testRegionUnmatches() {
Maps.transformValues(factory.createForApiTypeAndVersion("compute", "1.0").get(),
Suppliers.<URI> supplierFunction());
}
public void testRegionMatches() {
assertEquals(Maps.transformValues(factory.createForApiTypeAndVersion("compute", "1.0").get(), Suppliers
.<URI> supplierFunction()), ImmutableMap.of());
assertEquals(Maps.transformValues(factory.createForApiTypeAndVersion("compute", "1.1").get(), Suppliers
.<URI> supplierFunction()), ImmutableMap.of("az-1.region-a.geo-1", URI
.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456"), "az-2.region-a.geo-1", URI
@ -89,7 +92,6 @@ public class RegionIdToURIFromAccessForTypeAndVersionTest {
RegionIdToURISupplier.Factory.class));
}
@SuppressWarnings("unused")
@Provides
@Singleton
public Supplier<Access> provide() {

View File

@ -58,7 +58,6 @@ public class ZoneIdToURIFromAccessForTypeAndVersionTest {
ZoneIdToURIFromAccessForTypeAndVersion.class).build(ZoneIdToURISupplier.Factory.class));
}
@SuppressWarnings("unused")
@Provides
@Singleton
public Supplier<Access> provide() {
@ -66,9 +65,14 @@ public class ZoneIdToURIFromAccessForTypeAndVersionTest {
}
}).getInstance(ZoneIdToURISupplier.Factory.class);
@Test(expectedExceptions = NoSuchElementException.class)
public void testZoneUnmatches() {
Maps.transformValues(factory.createForApiTypeAndVersion("compute", "1.0").get(),
Suppliers.<URI> supplierFunction());
}
public void testZoneMatches() {
assertEquals(Maps.transformValues(factory.createForApiTypeAndVersion("compute", "1.0").get(), Suppliers
.<URI> supplierFunction()), ImmutableMap.of());
assertEquals(Maps.transformValues(factory.createForApiTypeAndVersion("compute", "1.1").get(), Suppliers
.<URI> supplierFunction()), ImmutableMap.of("az-1.region-a.geo-1", URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456"),
"az-2.region-a.geo-1", URI.create("https://az-2.region-a.geo-1.compute.hpcloudsvc.com/v1.1/3456"),
@ -86,7 +90,6 @@ public class ZoneIdToURIFromAccessForTypeAndVersionTest {
ZoneIdToURIFromAccessForTypeAndVersion.class).build(ZoneIdToURISupplier.Factory.class));
}
@SuppressWarnings("unused")
@Provides
@Singleton
public Supplier<Access> provide() {