Finished partial fix in 91f405c9fe.

This commit is contained in:
Adrian Cole 2014-11-16 10:15:06 -08:00
parent 91f405c9fe
commit 90a6bb19f4
8 changed files with 92 additions and 97 deletions

View File

@ -16,14 +16,34 @@
*/
package org.jclouds.openstack.keystone.v2_0.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import java.net.URI;
import java.util.Map;
import javax.inject.Inject;
import org.jclouds.location.functions.RegionToEndpoint;
import org.jclouds.openstack.keystone.v2_0.suppliers.RegionIdToAdminURISupplier;
public class RegionToAdminEndpointURI extends RegionToEndpoint {
import com.google.common.base.Function;
import com.google.common.base.Supplier;
public final class RegionToAdminEndpointURI implements Function<Object, URI> {
private final RegionIdToAdminURISupplier regionToAdminEndpoints;
@Inject
public RegionToAdminEndpointURI(RegionIdToAdminURISupplier regionToEndpointSupplier) {
super(regionToEndpointSupplier);
RegionToAdminEndpointURI(RegionIdToAdminURISupplier regionToAdminEndpoints) {
this.regionToAdminEndpoints = regionToAdminEndpoints;
}
@Override
public URI apply(Object from) {
Map<String, Supplier<URI>> regionToAdminEndpoint = regionToAdminEndpoints.get();
checkState(!regionToAdminEndpoint.isEmpty(), "no region name to admin endpoint mappings in keystone!");
checkArgument(regionToAdminEndpoint.containsKey(from),
"requested location %s, which is not in the keystone admin endpoints: %s", from, regionToAdminEndpoint);
return regionToAdminEndpoint.get(from).get();
}
}

View File

@ -18,41 +18,33 @@ package org.jclouds.s3.functions;
import java.net.URI;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.logging.Logger;
import org.jclouds.s3.Bucket;
import com.google.common.base.Function;
import com.google.common.base.Optional;
@Singleton
public class AssignCorrectHostnameForBucket implements Function<Object, URI> {
@Resource
protected Logger logger = Logger.NULL;
public final class AssignCorrectHostnameForBucket implements Function<Object, URI> {
protected final RegionToEndpointOrProviderIfNull r2;
protected final Function<String, Optional<String>> bucketToRegion;
private final RegionToEndpointOrProviderIfNull delegate;
private final Function<String, Optional<String>> bucketToRegion;
@Inject
public AssignCorrectHostnameForBucket(RegionToEndpointOrProviderIfNull r2,
AssignCorrectHostnameForBucket(RegionToEndpointOrProviderIfNull delegate,
@Bucket Function<String, Optional<String>> bucketToRegion) {
this.bucketToRegion = bucketToRegion;
this.r2 = r2;
this.delegate = delegate;
}
@Override
public URI apply(@Nullable Object from) {
public URI apply(Object from) {
String bucket = from.toString();
Optional<String> region = bucketToRegion.apply(bucket);
if (region.isPresent()) {
return r2.apply(region.get());
return delegate.apply(region.get());
}
return r2.apply(null);
return delegate.apply(null);
}
}

View File

@ -19,7 +19,6 @@ package org.jclouds.s3.functions;
import java.net.URI;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.s3.Bucket;
@ -28,23 +27,22 @@ import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.cache.LoadingCache;
@Singleton
public class DefaultEndpointThenInvalidateRegion implements Function<Object, URI> {
public final class DefaultEndpointThenInvalidateRegion implements Function<Object, URI> {
private final Function<Object, URI> delegate;
private final LoadingCache<String, Optional<String>> bucketToRegionCache;
private final AssignCorrectHostnameForBucket r2;
@Inject
public DefaultEndpointThenInvalidateRegion(AssignCorrectHostnameForBucket r2,
@Bucket LoadingCache<String, Optional<String>> bucketToRegionCache) {
this.r2 = r2;
DefaultEndpointThenInvalidateRegion(AssignCorrectHostnameForBucket delegate,
@Bucket LoadingCache<String, Optional<String>> bucketToRegionCache) {
this.delegate = delegate;
this.bucketToRegionCache = bucketToRegionCache;
}
@Override
public URI apply(@Nullable Object from) {
try {
return r2.apply(from);
return delegate.apply(from);
} finally {
bucketToRegionCache.invalidate(from.toString());
}

View File

@ -21,6 +21,8 @@ import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Map;
import org.jclouds.location.Provider;
import org.jclouds.location.Region;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.testng.annotations.Test;
@ -29,42 +31,41 @@ import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Module;
import com.google.inject.Provides;
/**
* Tests behavior of {@code AssignCorrectHostnameForBucket}
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during
// surefire
@Test(groups = "unit", testName = "AssignCorrectHostnameForBucketTest")
@Test
public class AssignCorrectHostnameForBucketTest {
static final RegionToEndpointOrProviderIfNull REGION_TO_ENDPOINT = Guice.createInjector(new Module() {
@Override public void configure(Binder binder) {
binder.bindConstant().annotatedWith(Provider.class).to("s3");
}
@Provides @Provider Supplier<URI> defaultUri() {
return Suppliers.ofInstance(URI.create("https://s3.amazonaws.com"));
}
@Provides @Region Supplier<Map<String, Supplier<URI>>> regionToEndpoints() {
Map<String, Supplier<URI>> regionToEndpoint = ImmutableMap.of( //
"us-standard", defaultUri(), //
"us-west-1", Suppliers.ofInstance(URI.create("https://s3-us-west-1.amazonaws.com")));
return Suppliers.ofInstance(regionToEndpoint);
}
}).getInstance(RegionToEndpointOrProviderIfNull.class);
public void testWhenNoBucketRegionMappingInCache() {
AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(new RegionToEndpointOrProviderIfNull(
"aws-s3", Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")),
Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("us-standard",
Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")), "us-west-1",
Suppliers.ofInstance(URI.create("https://s3-us-west-1.amazonaws.com"))))),
Functions.forMap(ImmutableMap.<String, Optional<String>> of("bucket", Optional.<String> absent())));
AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(REGION_TO_ENDPOINT,
Functions.forMap(ImmutableMap.of("bucket", Optional.<String>absent())));
assertEquals(fn.apply("bucket"), URI.create("https://s3.amazonaws.com"));
}
public void testWhenBucketRegionMappingInCache() {
AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(new RegionToEndpointOrProviderIfNull(
"aws-s3", Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")),
Suppliers.<Map<String, Supplier<URI>>> ofInstance(ImmutableMap.of("us-standard",
Suppliers.ofInstance(URI.create("https://s3.amazonaws.com")), "us-west-1",
Suppliers.ofInstance(URI.create("https://s3-us-west-1.amazonaws.com"))))),
Functions.forMap(ImmutableMap.<String, Optional<String>> of("bucket", Optional.of("us-west-1"))));
AssignCorrectHostnameForBucket fn = new AssignCorrectHostnameForBucket(REGION_TO_ENDPOINT,
Functions.forMap(ImmutableMap.of("bucket", Optional.of("us-west-1"))));
assertEquals(fn.apply("bucket"), URI.create("https://s3-us-west-1.amazonaws.com"));
}
}

View File

@ -17,34 +17,30 @@
package org.jclouds.s3.functions;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import java.net.URI;
import static org.jclouds.s3.functions.AssignCorrectHostnameForBucketTest.REGION_TO_ENDPOINT;
import org.testng.annotations.Test;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
@Test(testName = "DefaultEndpointThenInvalidateRegionTest")
public class DefaultEndpointThenInvalidateRegionTest {
@SuppressWarnings("unchecked")
@Test
void testInvalidate() throws Exception {
AssignCorrectHostnameForBucket r2 = createMock(AssignCorrectHostnameForBucket.class);
public void testInvalidate() throws Exception {
LoadingCache<String, Optional<String>> bucketToRegionCache = createMock(LoadingCache.class);
expect(r2.apply("mybucket")).andReturn(URI.create("http://east-url"));
bucketToRegionCache.invalidate("mybucket");
replay(bucketToRegionCache);
replay(r2, bucketToRegionCache);
new DefaultEndpointThenInvalidateRegion(r2, bucketToRegionCache).apply("mybucket");
verify(r2, bucketToRegionCache);
AssignCorrectHostnameForBucket delegate = new AssignCorrectHostnameForBucket(REGION_TO_ENDPOINT,
Functions.forMap(ImmutableMap.of("mybucket", Optional.of("us-west-1"))));
new DefaultEndpointThenInvalidateRegion(delegate, bucketToRegionCache).apply("mybucket");
verify(bucketToRegionCache);
}
}

View File

@ -17,36 +17,33 @@
package org.jclouds.location.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.net.URI;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.location.Region;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
@Singleton
public class RegionToEndpoint implements Function<Object, URI> {
public final class RegionToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, Supplier<URI>>> regionToEndpointSupplier;
private final Supplier<Map<String, Supplier<URI>>> regionToEndpoints;
@Inject
public RegionToEndpoint(@Region Supplier<Map<String, Supplier<URI>>> regionToEndpointSupplier) {
this.regionToEndpointSupplier = checkNotNull(regionToEndpointSupplier, "regionToEndpointSupplier");
RegionToEndpoint(@Region Supplier<Map<String, Supplier<URI>>> regionToEndpoints) {
this.regionToEndpoints = regionToEndpoints;
}
@Override
public URI apply(Object from) {
Map<String, Supplier<URI>> regionToEndpoint = regionToEndpointSupplier.get();
Map<String, Supplier<URI>> regionToEndpoint = regionToEndpoints.get();
checkState(!regionToEndpoint.isEmpty(), "no region name to endpoint mappings configured!");
checkArgument(regionToEndpoint.containsKey(from),
"requested location %s, which is not in the configured locations: %s", from, regionToEndpoint);
"requested location %s, which is not a configured region: %s", from, regionToEndpoint);
return regionToEndpoint.get(from).get();
}
}

View File

@ -17,13 +17,11 @@
package org.jclouds.location.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Provider;
@ -36,25 +34,23 @@ import com.google.common.base.Supplier;
* Return a uri corresponding to the name of the region (passed argument).
* Otherwise, return the default location.
*/
@Singleton
public class RegionToEndpointOrProviderIfNull implements Function<Object, URI> {
public final class RegionToEndpointOrProviderIfNull implements Function<Object, URI> {
private final Supplier<URI> defaultUri;
private final String defaultProvider;
private final Supplier<Map<String, Supplier<URI>>> regionToEndpointSupplier;
@Inject
public RegionToEndpointOrProviderIfNull(@Provider String defaultProvider, @Provider Supplier<URI> defaultUri,
RegionToEndpointOrProviderIfNull(@Provider String defaultProvider, @Provider Supplier<URI> defaultUri,
@Region Supplier<Map<String, Supplier<URI>>> regionToEndpointSupplier) {
this.defaultProvider = checkNotNull(defaultProvider, "defaultProvider");
this.defaultUri = checkNotNull(defaultUri, "defaultUri");
this.regionToEndpointSupplier = checkNotNull(regionToEndpointSupplier, "regionToEndpointSupplier");
this.defaultProvider = defaultProvider;
this.defaultUri = defaultUri;
this.regionToEndpointSupplier = regionToEndpointSupplier;
}
@Override
public URI apply(@Nullable Object from) {
if (from == null)
return defaultUri.get();
checkArgument(from instanceof String, "region is a String argument");
Map<String, Supplier<URI>> regionToEndpoint = regionToEndpointSupplier.get();
if (from.equals(defaultProvider)) {
if (regionToEndpoint.containsKey(from))

View File

@ -17,38 +17,33 @@
package org.jclouds.location.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.net.URI;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Zone;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
@Singleton
public class ZoneToEndpoint implements Function<Object, URI> {
public final class ZoneToEndpoint implements Function<Object, URI> {
private final Supplier<Map<String, Supplier<URI>>> zoneToEndpointSupplier;
private final Supplier<Map<String, Supplier<URI>>> zoneToEndpoints;
@Inject
public ZoneToEndpoint(@Zone Supplier<Map<String, Supplier<URI>>> zoneToEndpointSupplier) {
this.zoneToEndpointSupplier = checkNotNull(zoneToEndpointSupplier, "zoneToEndpointSupplier");
ZoneToEndpoint(@Zone Supplier<Map<String, Supplier<URI>>> zoneToEndpoints) {
this.zoneToEndpoints = zoneToEndpoints;
}
@Override
public URI apply(@Nullable Object from) {
checkArgument(from != null && from instanceof String, "you must specify a zone, as a String argument");
Map<String, Supplier<URI>> zoneToEndpoint = zoneToEndpointSupplier.get();
public URI apply(Object from) {
Map<String, Supplier<URI>> zoneToEndpoint = zoneToEndpoints.get();
checkState(!zoneToEndpoint.isEmpty(), "no zone name to endpoint mappings configured!");
checkArgument(zoneToEndpoint.containsKey(from),
"requested location %s, which is not in the configured locations: %s", from, zoneToEndpoint);
"requested location %s, which is not a configured zone: %s", from, zoneToEndpoint);
return zoneToEndpoint.get(from).get();
}
}