backfilled tests and refactored most of org.jclouds.location.suppliers package

This commit is contained in:
Adrian Cole 2011-10-23 04:38:25 +02:00
parent cf4acc8000
commit dd89ad3068
23 changed files with 590 additions and 106 deletions

View File

@ -22,12 +22,15 @@ import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import javax.inject.Named;
import org.jclouds.Constants;
import org.jclouds.aws.domain.Region;
import org.jclouds.aws.filters.FormSigner;
import org.jclouds.cloudwatch.config.CloudWatchRestClientModule;
import org.jclouds.cloudwatch.xml.GetMetricStatisticsResponseHandler;
@ -42,6 +45,7 @@ import org.jclouds.rest.RestContextSpec;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
@ -50,7 +54,8 @@ import com.google.inject.TypeLiteral;
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
// NOTE:without testName, this will not call @Before* and fail w/NPE during
// surefire
@Test(groups = "unit", testName = "CloudWatchAsyncClientTest")
public class CloudWatchAsyncClientTest extends RestClientTest<CloudWatchAsyncClient> {
@ -95,6 +100,19 @@ public class CloudWatchAsyncClientTest extends RestClientTest<CloudWatchAsyncCli
super.configure();
}
protected void bindRegionsToProvider() {
bindRegionsToProvider(Regions.class);
}
static class Regions implements javax.inject.Provider<Map<String, URI>> {
@Override
public Map<String, URI> get() {
return ImmutableMap.<String, URI> of(Region.EU_WEST_1, URI.create("https://ec2.eu-west-1.amazonaws.com"),
Region.US_EAST_1, URI.create("https://ec2.us-east-1.amazonaws.com"), Region.US_WEST_1,
URI.create("https://ec2.us-west-1.amazonaws.com"));
}
}
@Override
protected String provideTimeStamp(final DateService dateService,
@Named(Constants.PROPERTY_SESSION_INTERVAL) int expiration) {

View File

@ -26,7 +26,7 @@ import org.jclouds.compute.config.BindComputeSuppliersByClass;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.domain.Location;
import org.jclouds.location.suppliers.OnlyLocationOrFirstZoneOrRegionMatchingRegionId;
import org.jclouds.location.suppliers.OnlyLocationOrFirstRegionOptionallyMatchingRegionId;
import org.jclouds.location.suppliers.ZoneToRegionToProviderOrJustProvider;
import com.google.common.base.Supplier;
@ -46,7 +46,7 @@ public class EC2BindComputeSuppliersByClass extends BindComputeSuppliersByClass
@Override
protected Class<? extends Supplier<Location>> defineDefaultLocationSupplier() {
return OnlyLocationOrFirstZoneOrRegionMatchingRegionId.class;
return OnlyLocationOrFirstRegionOptionallyMatchingRegionId.class;
}
@Override

View File

@ -55,7 +55,7 @@ public class OrgAndVDCToLocationSupplier extends JustProvider {
OrgAndVDCToLocationSupplier(@Iso3166 Set<String> isoCodes, @Provider String providerName, @Provider URI endpoint,
@org.jclouds.vcloud.endpoints.Org Supplier<Map<String, ReferenceType>> orgNameToResource,
Supplier<Map<String, ? extends Org>> orgNameToVDCResource, @Iso3166 Map<String, Set<String>> isoCodesById) {
super(isoCodes, providerName, endpoint);
super(providerName, endpoint, isoCodes);
this.orgNameToResource = checkNotNull(orgNameToResource, "orgNameToResource");
this.orgNameToVDCResource = checkNotNull(orgNameToVDCResource, "orgNameToVDCResource");
this.isoCodesById = checkNotNull(isoCodesById, "isoCodesById");

View File

@ -55,7 +55,7 @@ public class OrgAndVDCToLocationSupplier extends JustProvider {
OrgAndVDCToLocationSupplier(@Iso3166 Set<String> isoCodes, @Provider String providerName, @Provider URI endpoint,
@org.jclouds.trmk.vcloud_0_8.endpoints.Org Supplier<Map<String, ReferenceType>> orgNameToResource,
Supplier<Map<String, ? extends Org>> orgNameToVDCResource, @Iso3166 Map<String, Set<String>> isoCodesById) {
super(isoCodes, providerName, endpoint);
super(providerName, endpoint, isoCodes);
this.orgNameToResource = checkNotNull(orgNameToResource, "orgNameToResource");
this.orgNameToVDCResource = checkNotNull(orgNameToVDCResource, "orgNameToVDCResource");
this.isoCodesById = checkNotNull(isoCodesById, "isoCodesById");

View File

@ -21,7 +21,7 @@ package org.jclouds.location.config;
import java.util.Set;
import org.jclouds.domain.Location;
import org.jclouds.location.suppliers.OnlyLocationOrFirstZoneOrRegionMatchingRegionId;
import org.jclouds.location.suppliers.OnlyLocationOrFirstRegionOptionallyMatchingRegionId;
import org.jclouds.location.suppliers.RegionToProviderOrJustProvider;
import com.google.common.base.Supplier;
@ -39,7 +39,7 @@ public class RegionsLocationModule extends LocationModule {
bind(new TypeLiteral<Supplier<Set<? extends Location>>>() {
}).to(RegionToProviderOrJustProvider.class);
bind(new TypeLiteral<Supplier<Location>>() {
}).to(OnlyLocationOrFirstZoneOrRegionMatchingRegionId.class);
}).to(OnlyLocationOrFirstRegionOptionallyMatchingRegionId.class);
super.configure();
}

View File

@ -0,0 +1,41 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.functions;
import org.jclouds.domain.Location;
import com.google.common.base.Function;
/**
*
* @author Adrian Cole
*
*/
public enum ToIdAndScope implements Function<Location, String> {
INSTANCE;
@Override
public String apply(Location input) {
return input.getId() + ":" + input.getScope();
}
@Override
public String toString() {
return "toIdAndScope()";
}
}

View File

@ -0,0 +1,101 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.predicates;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Singleton;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope;
import com.google.common.base.Predicate;
/**
*
* @author Adrian Cole
*/
public class LocationPredicates {
public static Predicate<Location> isZone() {
return IsZone.INSTANCE;
}
@Singleton
static enum IsZone implements Predicate<Location> {
INSTANCE;
@Override
public boolean apply(Location input) {
return input.getScope() == LocationScope.ZONE;
}
@Override
public String toString() {
return "isZone()";
}
}
public static Predicate<Location> isRegion() {
return IsRegion.INSTANCE;
}
static enum IsRegion implements Predicate<Location> {
INSTANCE;
@Override
public boolean apply(Location input) {
return input.getScope() == LocationScope.REGION;
}
@Override
public String toString() {
return "isRegion()";
}
}
public static Predicate<Location> isZoneOrRegionWhereRegionIdEquals(String region) {
return new IsZoneOrRegionWhereRegionIdEquals(region);
}
static class IsZoneOrRegionWhereRegionIdEquals implements Predicate<Location> {
private final String region;
IsZoneOrRegionWhereRegionIdEquals(String region) {
this.region = checkNotNull(region, "region");
}
@Override
public boolean apply(Location input) {
switch (input.getScope()) {
case ZONE:
return input.getParent().getId().equals(region);
case REGION:
return input.getId().equals(region);
default:
return false;
}
}
@Override
public String toString() {
return "isRegionAndIdEqualsOrIsZoneParentIdEquals(" + region + ")";
}
}
}

View File

@ -46,7 +46,7 @@ public class JustProvider implements Supplier<Set<? extends Location>> {
private final Set<String> isoCodes;
@Inject
public JustProvider(@Iso3166 Set<String> isoCodes, @Provider String providerName, @Provider URI endpoint) {
public JustProvider(@Provider String providerName, @Provider URI endpoint, @Iso3166 Set<String> isoCodes) {
this.providerName = checkNotNull(providerName, "providerName");
this.endpoint = checkNotNull(endpoint, "endpoint");
this.isoCodes = checkNotNull(isoCodes, "isoCodes");
@ -54,8 +54,8 @@ public class JustProvider implements Supplier<Set<? extends Location>> {
@Override
public Set<? extends Location> get() {
return ImmutableSet.of(new LocationBuilder().scope(LocationScope.PROVIDER).id(providerName).description(
endpoint.toASCIIString()).iso3166Codes(isoCodes).build());
return ImmutableSet.of(new LocationBuilder().scope(LocationScope.PROVIDER).id(providerName)
.description(endpoint.toASCIIString()).iso3166Codes(isoCodes).build());
}
}

View File

@ -20,10 +20,15 @@ package org.jclouds.location.suppliers;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.location.predicates.LocationPredicates.isRegion;
import static org.jclouds.location.predicates.LocationPredicates.isZone;
import static org.jclouds.location.predicates.LocationPredicates.isZoneOrRegionWhereRegionIdEquals;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -31,53 +36,27 @@ import org.jclouds.collect.Memoized;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope;
import org.jclouds.location.Region;
import org.jclouds.location.functions.ToIdAndScope;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.inject.Injector;
import com.google.inject.Key;
/**
* @author Adrian Cole
*/
@Singleton
public class OnlyLocationOrFirstZoneOrRegionMatchingRegionId implements Supplier<Location> {
@Singleton
public static final class IsRegionAndIdEqualsOrIsZoneParentIdEquals implements Predicate<Location> {
public class OnlyLocationOrFirstRegionOptionallyMatchingRegionId implements Supplier<Location> {
private final String region;
@Inject
IsRegionAndIdEqualsOrIsZoneParentIdEquals(@Region String region) {
this.region = checkNotNull(region, "region");
}
@Override
public boolean apply(Location input) {
switch (input.getScope()) {
case ZONE:
return input.getParent().getId().equals(region);
case REGION:
return input.getId().equals(region);
default:
return false;
}
}
@Override
public String toString() {
return "isRegionAndIdEqualsOrIsZoneParentIdEquals(" + region + ")";
}
}
private final Injector injector;
private final Predicate<Location> locationPredicate;
private final Supplier<Set<? extends Location>> locationsSupplier;
@Inject
OnlyLocationOrFirstZoneOrRegionMatchingRegionId(Injector injector,
OnlyLocationOrFirstRegionOptionallyMatchingRegionId(@Nullable @Region String region,
@Memoized Supplier<Set<? extends Location>> locationsSupplier) {
this.injector = checkNotNull(injector, "injector");
this.locationPredicate = region == null ? Predicates.<Location>or(isZone(), isRegion())
: isZoneOrRegionWhereRegionIdEquals(region);
this.locationsSupplier = checkNotNull(locationsSupplier, "locationsSupplier");
}
@ -87,16 +66,12 @@ public class OnlyLocationOrFirstZoneOrRegionMatchingRegionId implements Supplier
Set<? extends Location> locations = locationsSupplier.get();
if (locationsSupplier.get().size() == 1)
return getOnlyElement(locationsSupplier.get());
IsRegionAndIdEqualsOrIsZoneParentIdEquals matcher = null;
try {
String region = injector.getInstance(Key.get(String.class, Region.class));
if (region == null)
return Iterables.get(locationsSupplier.get(), 0);
matcher = injector.getInstance(IsRegionAndIdEqualsOrIsZoneParentIdEquals.class);
Location toReturn = Iterables.find(locations, matcher);
Location toReturn = Iterables.find(locations, locationPredicate);
return toReturn.getScope() == LocationScope.REGION ? toReturn : toReturn.getParent();
} catch (NoSuchElementException e) {
throw new IllegalStateException(String.format("region %s not found in %s", matcher, locations));
throw new NoSuchElementException(String.format("couldn't find region matching %s in %s", locationPredicate,
transform(locations, ToIdAndScope.INSTANCE)));
}
}
}

View File

@ -21,7 +21,10 @@ package org.jclouds.location.suppliers;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.location.predicates.LocationPredicates.isZone;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.inject.Inject;
@ -29,9 +32,8 @@ import javax.inject.Singleton;
import org.jclouds.collect.Memoized;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationScope;
import org.jclouds.location.functions.ToIdAndScope;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
/**
@ -41,33 +43,25 @@ import com.google.common.base.Supplier;
*/
@Singleton
public class OnlyLocationOrFirstZone implements Supplier<Location> {
@Singleton
public static final class IsZone implements Predicate<Location> {
@Override
public boolean apply(Location input) {
return input.getScope() == LocationScope.ZONE;
}
@Override
public String toString() {
return "isZone()";
}
}
private final Supplier<Set<? extends Location>> locationsSupplier;
private final IsZone isZone;
@Inject
OnlyLocationOrFirstZone(@Memoized Supplier<Set<? extends Location>> locationsSupplier, IsZone isZone) {
OnlyLocationOrFirstZone(@Memoized Supplier<Set<? extends Location>> locationsSupplier) {
this.locationsSupplier = checkNotNull(locationsSupplier, "locationsSupplierSupplier");
this.isZone = checkNotNull(isZone, "isZone");
}
@Override
public Location get() {
if (locationsSupplier.get().size() == 1)
return getOnlyElement(locationsSupplier.get());
return find(locationsSupplier.get(), isZone);
Set<? extends Location> locations = locationsSupplier.get();
if (locations.size() == 1)
return getOnlyElement(locations);
try {
return find(locations, isZone());
} catch (NoSuchElementException e) {
throw new NoSuchElementException("none to of the locations are scope ZONE: "
+ transform(locations, ToIdAndScope.INSTANCE));
}
}
}

View File

@ -50,7 +50,7 @@ public class RegionToProviderOrJustProvider extends JustProvider {
@Inject
public RegionToProviderOrJustProvider(@Iso3166 Set<String> isoCodes, @Provider String providerName,
@Provider URI endpoint, @Region Set<String> regions, @Iso3166 Map<String, Set<String>> isoCodesById) {
super(isoCodes, providerName, endpoint);
super(providerName, endpoint, isoCodes);
this.regions = checkNotNull(regions, "regions");
this.isoCodesById = checkNotNull(isoCodesById, "isoCodesById");
}

View File

@ -0,0 +1,48 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
/**
* Tests behavior of {@code JustProvider}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "JustProviderTest")
public class JustProviderTest {
@Test
public void test() throws SecurityException, NoSuchMethodException {
JustProvider fn = new JustProvider("servo", URI.create("http://servo"), ImmutableSet.of("US"));
assertEquals(
fn.get(),
ImmutableSet.of(new LocationBuilder().scope(LocationScope.PROVIDER).id("servo").description("http://servo")
.iso3166Codes(ImmutableSet.of("US")).build()));
}
}

View File

@ -0,0 +1,94 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers;
import static org.testng.Assert.assertEquals;
import java.util.NoSuchElementException;
import java.util.Set;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableSet;
/**
* Tests behavior of {@code OnlyLocationOrFirstRegionOptionallyMatchingRegionId}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "OnlyLocationOrFirstRegionOptionallyMatchingRegionIdTest")
public class OnlyLocationOrFirstRegionOptionallyMatchingRegionIdTest {
Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("servo").description("http://servo")
.build();
Location region = new LocationBuilder().scope(LocationScope.REGION).id("servo-r1").description("http://r1.servo")
.parent(provider).build();
Location region2 = new LocationBuilder().scope(LocationScope.REGION).id("servo-r2").description("http://r2.servo")
.parent(provider).build();
Location zone = new LocationBuilder().scope(LocationScope.ZONE).id("servo-z").description("http://z.r.servo")
.parent(region).build();
@Test
public void testDidntMatchRegionIdThrowsNSEEWithReasonableMessage() {
Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(provider, region, region2, zone));
OnlyLocationOrFirstRegionOptionallyMatchingRegionId fn = new OnlyLocationOrFirstRegionOptionallyMatchingRegionId(
"foo", supplier);
try {
fn.get();
assert false;
} catch (NoSuchElementException e) {
assertEquals(
e.getMessage(),
"couldn't find region matching isRegionAndIdEqualsOrIsZoneParentIdEquals(foo) in [servo:PROVIDER, servo-r1:REGION, servo-r2:REGION, servo-z:ZONE]");
}
}
@Test
public void testNoRegionUsesProvider() {
Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(provider));
OnlyLocationOrFirstRegionOptionallyMatchingRegionId fn = new OnlyLocationOrFirstRegionOptionallyMatchingRegionId(
null, supplier);
assertEquals(fn.get(), provider);
}
@Test
public void testFirstRegion() {
Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(provider, region, region2, zone));
OnlyLocationOrFirstRegionOptionallyMatchingRegionId fn = new OnlyLocationOrFirstRegionOptionallyMatchingRegionId(
null, supplier);
assertEquals(fn.get(), region);
}
@Test
public void testFindRegion() {
Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(provider, region, region2, zone));
OnlyLocationOrFirstRegionOptionallyMatchingRegionId fn = new OnlyLocationOrFirstRegionOptionallyMatchingRegionId(
region2.getId(), supplier);
assertEquals(fn.get(), region2);
}
}

View File

@ -0,0 +1,78 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers;
import static org.testng.Assert.assertEquals;
import java.util.NoSuchElementException;
import java.util.Set;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableSet;
/**
* Tests behavior of {@code OnlyLocationOrFirstZone}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "OnlyLocationOrFirstZoneTest")
public class OnlyLocationOrFirstZoneTest {
Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("servo").description("http://servo")
.build();
Location region = new LocationBuilder().scope(LocationScope.REGION).id("servo-r").description("http://r.servo")
.parent(provider).build();
Location zone = new LocationBuilder().scope(LocationScope.ZONE).id("servo-z").description("http://z.r.servo")
.parent(region).build();
@Test
public void testDidntFindZoneThrowsNSEEWithReasonableMessage() {
Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(provider, region));
OnlyLocationOrFirstZone fn = new OnlyLocationOrFirstZone(supplier);
try {
fn.get();
assert false;
} catch (NoSuchElementException e) {
assertEquals(e.getMessage(), "none to of the locations are scope ZONE: [servo:PROVIDER, servo-r:REGION]");
}
}
@Test
public void testNoZoneUsesProvider() {
Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(provider));
OnlyLocationOrFirstZone fn = new OnlyLocationOrFirstZone(supplier);
assertEquals(fn.get(), provider);
}
@Test
public void testFirstZone() {
Supplier<Set<? extends Location>> supplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(provider, region, zone));
OnlyLocationOrFirstZone fn = new OnlyLocationOrFirstZone(supplier);
assertEquals(fn.get(), zone);
}
}

View File

@ -26,7 +26,7 @@ import javax.inject.Singleton;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.domain.Location;
import org.jclouds.location.suppliers.OnlyLocationOrFirstZoneOrRegionMatchingRegionId;
import org.jclouds.location.suppliers.OnlyLocationOrFirstRegionOptionallyMatchingRegionId;
import com.google.common.base.Supplier;
import com.google.inject.AbstractModule;
@ -66,7 +66,7 @@ public abstract class BindLoadBalancerSuppliersByClass extends AbstractModule {
}
protected Class<? extends Supplier<Location>> defineDefaultLocationSupplier() {
return OnlyLocationOrFirstZoneOrRegionMatchingRegionId.class;
return OnlyLocationOrFirstRegionOptionallyMatchingRegionId.class;
}
protected void bindImageSupplier(Class<? extends Supplier<Set<? extends Image>>> clazz) {

View File

@ -18,20 +18,17 @@
*/
package org.jclouds.aws.s3.binders;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
import static org.jclouds.http.utils.ModifyRequest.endpoint;
import java.net.URI;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.utils.ModifyRequest;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.rest.binders.BindAsHostPrefix;
import org.jclouds.s3.Bucket;
@ -48,10 +45,9 @@ public class AssignCorrectHostnameAndBindAsHostPrefixIfConfigured extends BindAs
@Inject
public AssignCorrectHostnameAndBindAsHostPrefixIfConfigured(BindAsHostPrefix bindAsHostPrefix,
@Named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS) boolean isVhostStyle,
@Named(PROPERTY_S3_SERVICE_PATH) String servicePath, RegionToEndpointOrProviderIfNull r2,
Provider<UriBuilder> uriBuilderProvider, @Bucket Map<String, String> bucketToRegion) {
super(bindAsHostPrefix, isVhostStyle, servicePath, uriBuilderProvider);
RegionToEndpointOrProviderIfNull r2, Provider<UriBuilder> uriBuilderProvider,
@Bucket Map<String, String> bucketToRegion) {
super(bindAsHostPrefix, true, "/", uriBuilderProvider);
this.bucketToRegion = bucketToRegion;
this.r2 = r2;
}
@ -62,9 +58,7 @@ public class AssignCorrectHostnameAndBindAsHostPrefixIfConfigured extends BindAs
String region = bucketToRegion.get(bucket);
if (region != null) {
URI endpoint = r2.apply(region);
request = ModifyRequest.endpoint(
request,
uriBuilderProvider.get().uri(endpoint).path(request.getEndpoint().getPath())
request = endpoint(request, uriBuilderProvider.get().uri(endpoint).path(request.getEndpoint().getPath())
.replaceQuery(request.getEndpoint().getQuery()).build());
}
return super.bindToRequest(request, payload);

View File

@ -19,7 +19,6 @@
package org.jclouds.aws.s3;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Map;
@ -29,6 +28,7 @@ import java.util.concurrent.ConcurrentMap;
import org.jclouds.aws.s3.config.AWSS3RestClientModule;
import org.jclouds.aws.s3.functions.ETagFromHttpResponseViaRegex;
import org.jclouds.aws.s3.functions.UploadIdFromHttpResponseViaRegex;
import org.jclouds.blobstore.binders.BindBlobToMultipartFormTest;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.RequiresHttp;
@ -45,6 +45,7 @@ import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.s3.domain.ObjectMetadata;
import org.jclouds.s3.domain.ObjectMetadataBuilder;
import org.jclouds.s3.domain.S3Object;
import org.jclouds.s3.functions.ReturnFalseIfBucketAlreadyOwnedByYouOrIllegalState;
import org.jclouds.s3.options.PutBucketOptions;
import org.jclouds.s3.options.PutObjectOptions;
@ -84,6 +85,26 @@ public class AWSS3AsyncClientTest extends org.jclouds.s3.S3AsyncClientTest<AWSS3
checkFilters(request);
}
@Override
public void testPutObject() throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
NoSuchMethodException, IOException {
Method method = AWSS3AsyncClient.class
.getMethod("putObject", String.class, S3Object.class, PutObjectOptions[].class);
HttpRequest request = processor.createRequest(method, "bucket",
blobToS3Object.apply(BindBlobToMultipartFormTest.TEST_BLOB));
assertRequestLineEquals(request, "PUT https://bucket." + url + "/hello HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Host: bucket." + url + "\n");
assertPayloadEquals(request, "hello", "text/plain", false);
assertResponseParserClassEquals(method, request, ParseETagHeader.class);
assertSaxResponseParserClassEquals(method, null);
assertExceptionParserClassEquals(method, null);
checkFilters(request);
}
@Override
public void testGetBucketLocation() throws SecurityException, NoSuchMethodException, IOException {
Method method = AWSS3AsyncClient.class.getMethod("getBucketLocation", String.class);
@ -111,8 +132,8 @@ public class AWSS3AsyncClientTest extends org.jclouds.s3.S3AsyncClientTest<AWSS3
@Override
public void testPutBucketDefault() throws ArrayIndexOutOfBoundsException, SecurityException,
IllegalArgumentException, NoSuchMethodException, IOException {
Method method = AWSS3AsyncClient.class.getMethod("putBucketInRegion", String.class, String.class, Array
.newInstance(PutBucketOptions.class, 0).getClass());
Method method = AWSS3AsyncClient.class.getMethod("putBucketInRegion", String.class, String.class,
PutBucketOptions[].class);
HttpRequest request = processor.createRequest(method, (String) null, "bucket");
assertRequestLineEquals(request, "PUT https://bucket.bucketendpoint/ HTTP/1.1");
@ -224,8 +245,8 @@ public class AWSS3AsyncClientTest extends org.jclouds.s3.S3AsyncClientTest<AWSS3
public void testPutBucketEu() throws ArrayIndexOutOfBoundsException, SecurityException, IllegalArgumentException,
NoSuchMethodException, IOException {
Method method = AWSS3AsyncClient.class.getMethod("putBucketInRegion", String.class, String.class, Array
.newInstance(PutBucketOptions.class, 0).getClass());
Method method = AWSS3AsyncClient.class.getMethod("putBucketInRegion", String.class, String.class,
PutBucketOptions[].class);
HttpRequest request = processor.createRequest(method, "EU", "bucket");
assertRequestLineEquals(request, "PUT https://bucket.bucketendpoint/ HTTP/1.1");

View File

@ -0,0 +1,84 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.aws.s3.binders;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import javax.inject.Provider;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
import org.jclouds.rest.binders.BindAsHostPrefix;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.sun.jersey.api.uri.UriBuilderImpl;
/**
* Tests behavior of
* {@code AssignCorrectHostnameAndBindAsHostPrefixIfConfigured}
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during
// surefire
@Test(groups = "unit", testName = "AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest")
public class AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest {
Provider<UriBuilder> uriBuilderProvider = new Provider<UriBuilder>() {
@Override
public UriBuilder get() {
return new UriBuilderImpl();
}
};
public void testWhenNoBucketRegionMappingInCache() {
HttpRequest request = new HttpRequest("GET", URI.create("https://s3.amazonaws.com"));
AssignCorrectHostnameAndBindAsHostPrefixIfConfigured binder = new AssignCorrectHostnameAndBindAsHostPrefixIfConfigured(
new BindAsHostPrefix(uriBuilderProvider), new RegionToEndpointOrProviderIfNull("aws-s3",
URI.create("https://s3.amazonaws.com"), ImmutableMap.of("us-standard",
URI.create("https://s3.amazonaws.com"), "us-west-1",
URI.create("https://s3-us-west-1.amazonaws.com"))), uriBuilderProvider,
ImmutableMap.<String, String> of());
request = binder.bindToRequest(request, "bucket");
assertEquals(request.getRequestLine(), "GET https://bucket.s3.amazonaws.com HTTP/1.1");
}
public void testWhenBucketRegionMappingInCache() {
HttpRequest request = new HttpRequest("GET", URI.create("https://s3.amazonaws.com"));
AssignCorrectHostnameAndBindAsHostPrefixIfConfigured binder = new AssignCorrectHostnameAndBindAsHostPrefixIfConfigured(
new BindAsHostPrefix(uriBuilderProvider), new RegionToEndpointOrProviderIfNull("aws-s3",
URI.create("https://s3.amazonaws.com"), ImmutableMap.of("us-standard",
URI.create("https://s3.amazonaws.com"), "us-west-1",
URI.create("https://s3-us-west-1.amazonaws.com"))), uriBuilderProvider,
ImmutableMap.<String, String> of("bucket", "us-west-1"));
request = binder.bindToRequest(request, "bucket");
assertEquals(request.getRequestLine(), "GET https://bucket.s3-us-west-1.amazonaws.com HTTP/1.1");
}
}

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.cloudloadbalancers.config;
import static com.google.common.collect.Iterables.get;
import java.net.URI;
import java.util.Map;
import java.util.Set;
@ -37,10 +39,13 @@ import org.jclouds.http.RequiresHttp;
import org.jclouds.http.annotation.ClientError;
import org.jclouds.http.annotation.Redirection;
import org.jclouds.http.annotation.ServerError;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.config.GsonModule.DateAdapter;
import org.jclouds.json.config.GsonModule.Iso8601DateAdapter;
import org.jclouds.location.Provider;
import org.jclouds.location.Region;
import org.jclouds.location.config.ProvideRegionToURIViaProperties;
import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse;
import org.jclouds.openstack.config.OpenStackAuthenticationModule;
import org.jclouds.openstack.reference.AuthHeaders;
@ -48,6 +53,7 @@ import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.config.RestClientModule;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
@ -118,6 +124,20 @@ public class CloudLoadBalancersRestClientModule extends
return endpoints.keySet();
}
@Provides
@Singleton
@Nullable
@Region
protected String getDefaultRegion(@Provider URI uri, @Region Map<String, URI> map, LoggerFactory logFactory) {
String region = ImmutableBiMap.copyOf(map).inverse().get(uri);
if (region == null && map.size() > 0) {
logFactory.getLogger(getClass().getName()).warn(
"failed to find region for current endpoint %s in %s; choosing first: %s", uri, map, region);
region = get(map.keySet(), 0);
}
return region;
}
@Override
protected void bindErrorHandlers() {
bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(

View File

@ -21,6 +21,8 @@ package org.jclouds.cloudloadbalancers.features;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.Properties;
import org.jclouds.cloudloadbalancers.CloudLoadBalancersAsyncClient;
@ -40,6 +42,7 @@ import org.testng.annotations.BeforeClass;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Module;
/**
@ -50,6 +53,19 @@ public abstract class BaseCloudLoadBalancersAsyncClientTest<T> extends RestClien
@RequiresHttp
@ConfiguresRestClient
public static class CloudLoadBalancersRestClientModuleExtension extends CloudLoadBalancersRestClientModule {
protected void bindRegionsToProvider() {
bindRegionsToProvider(Regions.class);
}
static class Regions implements javax.inject.Provider<Map<String, URI>> {
@Override
public Map<String, URI> get() {
return ImmutableMap.<String, URI> of("DFW",
URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/1234"));
}
}
@Override
protected String accountID(Supplier<AuthenticationResponse> in) {
return "1234";

View File

@ -53,7 +53,7 @@ public class GoGridLocationSupplier extends JustProvider {
@Inject
GoGridLocationSupplier(@Iso3166 Set<String> isoCodes, @Provider String providerName, @Provider URI endpoint,
GoGridClient sync, @Iso3166 Map<String, Set<String>> isoCodesById) {
super(isoCodes, providerName, endpoint);
super(providerName, endpoint, isoCodes);
this.sync = checkNotNull(sync, "sync");
this.isoCodesById = checkNotNull(isoCodesById, "isoCodesById");
}

View File

@ -56,7 +56,7 @@ public class RimuHostingLocationSupplier extends JustProvider {
@Inject
RimuHostingLocationSupplier(@Iso3166 Set<String> isoCodes, @Provider String providerName, @Provider URI endpoint,
RimuHostingClient sync, @Iso3166 Map<String, Set<String>> isoCodesById) {
super(isoCodes, providerName, endpoint);
super(providerName, endpoint, isoCodes);
this.sync = checkNotNull(sync, "sync");
this.isoCodesById = checkNotNull(isoCodesById, "isoCodesById");
}

View File

@ -40,8 +40,8 @@ import com.google.common.collect.ImmutableSet;
@Test(singleThreaded = true, groups = "unit")
public class DatacenterToLocationTest {
static DatacenterToLocation function = new DatacenterToLocation(new JustProvider(ImmutableSet.<String> of(), "softlayer",
URI.create("foo")));;
static DatacenterToLocation function = new DatacenterToLocation(new JustProvider("softlayer", URI.create("foo"),
ImmutableSet.<String> of()));
@Test
public void testDatacenterToLocation() {