From 3e9c566edc5beafd024d42c4fccce5013e2757eb Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Thu, 2 Feb 2012 07:48:26 -0800 Subject: [PATCH] aws related changes for issue 826 --- .../cloudwatch/CloudWatchAsyncClientTest.java | 30 +++-- .../EC2BindComputeSuppliersByClass.java | 17 +-- .../EC2ComputeServiceContextModule.java | 3 +- .../strategy/EC2ListNodesStrategy.java | 8 +- .../compute/suppliers/EC2ImageSupplier.java | 10 +- .../ec2/config/EC2RestClientModule.java | 116 +++++------------- .../DescribeAvailabilityZonesInRegion.java | 94 ++++++++++++++ .../DescribeRegionsForConfiguredRegions.java | 59 +++++++++ .../jclouds/ec2/xml/AttachmentHandler.java | 10 +- .../ec2/xml/BaseReservationHandler.java | 9 +- .../jclouds/ec2/xml/BundleTaskHandler.java | 8 +- .../ec2/xml/CreateVolumeResponseHandler.java | 30 +++-- .../xml/DescribeAddressesResponseHandler.java | 5 +- ...cribeAvailabilityZonesResponseHandler.java | 14 ++- .../xml/DescribeImagesResponseHandler.java | 7 +- .../xml/DescribeInstancesResponseHandler.java | 3 +- .../xml/DescribeKeyPairsResponseHandler.java | 13 +- ...DescribeSecurityGroupsResponseHandler.java | 7 +- .../ec2/xml/InstanceStateChangeHandler.java | 5 +- .../ec2/xml/KeyPairResponseHandler.java | 14 ++- .../ec2/xml/RunInstancesResponseHandler.java | 5 +- .../org/jclouds/ec2/xml/SnapshotHandler.java | 12 +- .../ec2/services/BaseEC2AsyncClientTest.java | 50 +++++--- ...escribeAvailabilityZonesInRegionTest.java} | 74 ++++++----- .../jclouds/ec2/xml/BaseEC2HandlerTest.java | 25 +++- ...eAvailabilityZonesResponseHandlerTest.java | 17 ++- .../DescribeImagesResponseHandlerTest.java | 6 +- .../DescribeInstancesResponseHandlerTest.java | 6 +- .../config/S3BlobStoreContextModule.java | 2 - .../jclouds/s3/config/S3RestClientModule.java | 4 +- .../s3/functions/BindRegionToXmlPayload.java | 23 ++-- .../aws/config/AWSRestClientModule.java | 50 -------- .../WithZonesFormSigningRestClientModule.java | 45 +------ ...hZonesFormSigningRestClientModuleTest.java | 60 --------- .../AWSEC2ComputeServiceContextModule.java | 8 +- .../strategy/AWSEC2ListNodesStrategy.java | 5 +- .../suppliers/AWSEC2ImageSupplier.java | 6 +- .../AWSDescribeInstancesResponseHandler.java | 4 +- .../xml/AWSRunInstancesResponseHandler.java | 3 +- .../ec2/xml/BaseAWSReservationHandler.java | 9 +- .../ec2/xml/LaunchSpecificationHandler.java | 7 +- .../aws/ec2/xml/PlacementGroupHandler.java | 6 +- .../xml/ReservedInstancesOfferingHandler.java | 6 +- .../org/jclouds/aws/ec2/xml/SpotHandler.java | 8 +- .../aws/ec2/xml/SpotInstanceHandler.java | 11 +- .../AWSEC2TemplateBuilderLiveTest.java | 2 + ...GroupsAsNeededAndReturnRunOptionsTest.java | 6 +- .../ec2/services/AWSAMIClientLiveTest.java | 1 - .../services/BaseAWSEC2AsyncClientTest.java | 54 ++++---- ...SDescribeInstancesResponseHandlerTest.java | 5 +- .../AWSRunInstancesResponseHandlerTest.java | 11 +- ...beSpotPriceHistoryResponseHandlerTest.java | 5 +- .../aws/ec2/xml/SpotInstanceHandlerTest.java | 9 +- .../aws/ec2/xml/SpotInstancesHandlerTest.java | 9 +- .../aws/s3/config/AWSS3RestClientModule.java | 22 ++-- .../jclouds/aws/s3/AWSS3AsyncClientTest.java | 22 ++-- ...meAndBindAsHostPrefixIfConfiguredTest.java | 32 +++-- 57 files changed, 587 insertions(+), 505 deletions(-) create mode 100644 apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java create mode 100644 apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForConfiguredRegions.java rename apis/ec2/src/test/java/org/jclouds/ec2/{config/EC2RestClientModuleTest.java => suppliers/DescribeAvailabilityZonesInRegionTest.java} (64%) delete mode 100644 common/aws/src/test/java/org/jclouds/aws/config/AWSWithZonesFormSigningRestClientModuleTest.java diff --git a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java index ae815f778e..33d405718b 100644 --- a/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java +++ b/apis/cloudwatch/src/test/java/org/jclouds/cloudwatch/CloudWatchAsyncClientTest.java @@ -18,6 +18,7 @@ */ package org.jclouds.cloudwatch; +import static com.google.common.collect.Maps.transformValues; import static org.testng.Assert.assertEquals; import java.io.IOException; @@ -40,13 +41,17 @@ import org.jclouds.date.DateService; import org.jclouds.http.HttpRequest; import org.jclouds.http.RequiresHttp; import org.jclouds.http.functions.ParseSax; +import org.jclouds.location.config.LocationModule; +import org.jclouds.location.suppliers.RegionIdToURISupplier; import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.RestClientTest; import org.jclouds.rest.RestContextFactory; import org.jclouds.rest.RestContextSpec; import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.jclouds.util.Suppliers2; import org.testng.annotations.Test; +import com.google.common.base.Supplier; import com.google.common.collect.ImmutableMap; import com.google.inject.Module; import com.google.inject.TypeLiteral; @@ -91,22 +96,21 @@ public class CloudWatchAsyncClientTest extends RestClientTest> get() { + return transformValues(ImmutableMap. 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")), Suppliers2. ofInstanceFunction()); + } - static class Regions implements javax.inject.Provider> { - @Override - public Map get() { - return ImmutableMap. 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 diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2BindComputeSuppliersByClass.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2BindComputeSuppliersByClass.java index 7591ef88ff..b66c8e8bb6 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2BindComputeSuppliersByClass.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2BindComputeSuppliersByClass.java @@ -20,14 +20,11 @@ package org.jclouds.ec2.compute.config; import java.util.Set; -import org.jclouds.ec2.compute.suppliers.EC2HardwareSupplier; -import org.jclouds.ec2.compute.suppliers.EC2ImageSupplier; 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.OnlyLocationOrFirstRegionOptionallyMatchingRegionId; -import org.jclouds.location.suppliers.ZoneToRegionToProviderOrJustProvider; +import org.jclouds.ec2.compute.suppliers.EC2HardwareSupplier; +import org.jclouds.ec2.compute.suppliers.EC2ImageSupplier; import com.google.common.base.Supplier; /** @@ -43,14 +40,4 @@ public class EC2BindComputeSuppliersByClass extends BindComputeSuppliersByClass protected Class>> defineImageSupplier() { return EC2ImageSupplier.class; } - - @Override - protected Class> defineDefaultLocationSupplier() { - return OnlyLocationOrFirstRegionOptionallyMatchingRegionId.class; - } - - @Override - protected Class>> defineLocationSupplier() { - return ZoneToRegionToProviderOrJustProvider.class; - } } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceContextModule.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceContextModule.java index f8728c1f83..687cb846f0 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceContextModule.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceContextModule.java @@ -79,7 +79,8 @@ public class EC2ComputeServiceContextModule extends BaseComputeServiceContextMod } @Override - protected Supplier> supplyNonParsingImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, + protected Supplier> supplyNonParsingImageCache( + AtomicReference authException, @Named(PROPERTY_SESSION_INTERVAL) long seconds, final Supplier> imageSupplier, Injector injector) { final Supplier> cache = injector.getInstance(Key.get(new TypeLiteral>>() {})); return new Supplier>() { diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java index 88842af628..0ac45bd538 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java @@ -49,6 +49,7 @@ import org.jclouds.logging.Logger; import com.google.common.base.Function; import com.google.common.base.Predicate; +import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; /** @@ -62,12 +63,12 @@ public class EC2ListNodesStrategy implements ListNodesStrategy { protected Logger logger = Logger.NULL; protected final EC2AsyncClient client; - protected final Set regions; + protected final Supplier> regions; protected final Function runningInstanceToNodeMetadata; protected final ExecutorService executor; @Inject - protected EC2ListNodesStrategy(EC2AsyncClient client, @Region Set regions, + protected EC2ListNodesStrategy(EC2AsyncClient client, @Region Supplier> regions, Function runningInstanceToNodeMetadata, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) { this.client = checkNotNull(client, "client"); @@ -91,9 +92,8 @@ public class EC2ListNodesStrategy implements ListNodesStrategy { protected Iterable pollRunningInstances() { Iterable>> reservations = transformParallel( - regions, new Function>>>() { + regions.get(), new Function>>>() { - @SuppressWarnings("unchecked") @Override public Future>> apply(String from) { // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7126754 diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/suppliers/EC2ImageSupplier.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/suppliers/EC2ImageSupplier.java index a84520dfa5..124075b714 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/suppliers/EC2ImageSupplier.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/suppliers/EC2ImageSupplier.java @@ -25,8 +25,8 @@ import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS; import java.util.Collections; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; +import java.util.Map.Entry; import javax.annotation.Resource; import javax.inject.Inject; @@ -47,9 +47,9 @@ import com.google.common.base.Predicates; import com.google.common.base.Supplier; import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableMap.Builder; /** * @@ -61,14 +61,14 @@ public class EC2ImageSupplier implements Supplier> { @Named(ComputeServiceConstants.COMPUTE_LOGGER) protected Logger logger = Logger.NULL; - private final Set regions; + private final Supplier> regions; private final DescribeImagesParallel describer; private final String[] amiOwners; private final EC2ImageParser parser; private final Supplier> cache; @Inject - protected EC2ImageSupplier(@Region Set regions, DescribeImagesParallel describer, + protected EC2ImageSupplier(@Region Supplier> regions, DescribeImagesParallel describer, @Named(PROPERTY_EC2_AMI_OWNERS) String[] amiOwners, Supplier> cache, EC2ImageParser parser) { this.regions = regions; @@ -88,7 +88,7 @@ public class EC2ImageSupplier implements Supplier> { } else { logger.debug(">> providing images"); - Iterable> queries = getDescribeQueriesForOwnersInRegions(regions, + Iterable> queries = getDescribeQueriesForOwnersInRegions(regions.get(), amiOwners); Iterable parsedImages = ImmutableSet.copyOf(filter(transform(describer.apply(queries), parser), Predicates diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/config/EC2RestClientModule.java b/apis/ec2/src/main/java/org/jclouds/ec2/config/EC2RestClientModule.java index 8de8b8125b..6a65c0bb0a 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/config/EC2RestClientModule.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/config/EC2RestClientModule.java @@ -18,21 +18,16 @@ */ package org.jclouds.ec2.config; -import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS; - -import java.net.URI; +import java.util.Collection; import java.util.Map; import java.util.Set; -import java.util.Map.Entry; -import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Singleton; import org.jclouds.aws.config.WithZonesFormSigningRestClientModule; import org.jclouds.ec2.EC2AsyncClient; import org.jclouds.ec2.EC2Client; -import org.jclouds.ec2.domain.AvailabilityZoneInfo; import org.jclouds.ec2.services.AMIAsyncClient; import org.jclouds.ec2.services.AMIClient; import org.jclouds.ec2.services.AvailabilityZoneAndRegionAsyncClient; @@ -49,23 +44,22 @@ import org.jclouds.ec2.services.SecurityGroupAsyncClient; import org.jclouds.ec2.services.SecurityGroupClient; import org.jclouds.ec2.services.WindowsAsyncClient; import org.jclouds.ec2.services.WindowsClient; -import org.jclouds.http.HttpResponseException; +import org.jclouds.ec2.suppliers.DescribeAvailabilityZonesInRegion; +import org.jclouds.ec2.suppliers.DescribeRegionsForConfiguredRegions; import org.jclouds.http.RequiresHttp; -import org.jclouds.location.Region; import org.jclouds.location.Zone; -import org.jclouds.logging.Logger; +import org.jclouds.location.config.LocationModule; +import org.jclouds.location.suppliers.RegionIdToURISupplier; +import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier; +import org.jclouds.location.suppliers.ZoneIdsSupplier; import org.jclouds.rest.ConfiguresRestClient; -import com.google.common.base.Predicates; -import com.google.common.base.Splitter; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -import com.google.common.collect.ImmutableMap.Builder; -import com.google.inject.ConfigurationException; -import com.google.inject.Injector; -import com.google.inject.Key; -import com.google.inject.name.Names; +import com.google.common.collect.Iterables; +import com.google.inject.Scopes; /** * Configures the EC2 connection. @@ -97,85 +91,33 @@ public class EC2RestClientModule } @Override - protected void bindRegionsToProvider() { - bindRegionsToProvider(RegionIdsToURI.class); - } - - @Override - protected void bindZonesToProvider() { - bindZonesToProvider(RegionIdToZoneId.class); + protected void installLocations() { + install(new LocationModule()); + bind(RegionIdToZoneIdsSupplier.class).to(DescribeAvailabilityZonesInRegion.class).in(Scopes.SINGLETON); + bind(RegionIdToURISupplier.class).to(DescribeRegionsForConfiguredRegions.class).in(Scopes.SINGLETON); + bind(ZoneIdsSupplier.class).to(ZoneIdsFromRegionIdToZoneIdsValues.class).in(Scopes.SINGLETON); } + /** + * as opposed to via properties, lets look up zones via api, as they are more likely to change + */ @Singleton - public static class RegionIdsToURI implements javax.inject.Provider> { - private final AvailabilityZoneAndRegionClient client; - private final Injector injector; + public static class ZoneIdsFromRegionIdToZoneIdsValues implements ZoneIdsSupplier { + + private final Supplier>>> regionIdToZoneIdsSupplier; @Inject - public RegionIdsToURI(EC2Client client, Injector injector) { - this.client = client.getAvailabilityZoneAndRegionServices(); - this.injector = injector; + protected ZoneIdsFromRegionIdToZoneIdsValues( + @Zone Supplier>>> regionIdToZoneIdsSupplier) { + this.regionIdToZoneIdsSupplier = regionIdToZoneIdsSupplier; } - @Singleton - @Region @Override - public Map get() { - try { - String regionString = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGIONS))); - Set regions = ImmutableSet.copyOf(Splitter.on(',').split(regionString)); - if (regions.size() > 0) - return Maps.filterKeys(client.describeRegions(), Predicates.in(regions)); - } catch (ConfigurationException e) { - // this happens if regions property isn't set - // services not run by AWS may not have regions, so this is ok. - } - return client.describeRegions(); - - } - } - - @Singleton - public static class RegionIdToZoneId implements javax.inject.Provider> { - @Resource - protected Logger logger = Logger.NULL; - - private final AvailabilityZoneAndRegionClient client; - private final Map regions; - - @Inject - public RegionIdToZoneId(EC2Client client, @Region Map regions) { - this.client = client.getAvailabilityZoneAndRegionServices(); - this.regions = regions; - } - - @Singleton - @Zone - @Override - public Map get() { - Builder map = ImmutableMap.builder(); - HttpResponseException exception = null; - for (Entry region : regions.entrySet()) { - try { - for (AvailabilityZoneInfo zoneInfo : client.describeAvailabilityZonesInRegion(region.getKey())) { - map.put(zoneInfo.getZone(), region.getKey()); - } - } catch (HttpResponseException e) { - if (e.getMessage().contains("Unable to tunnel through proxy")) { - exception = e; - logger.error(e, "Could not describe availability zones in Region: %s", region.getKey()); - } else { - throw e; - } - } - } - ImmutableMap result = map.build(); - if (result.isEmpty() && exception != null) { - throw exception; - } - return result; + public Set get() { + Collection>> zones = regionIdToZoneIdsSupplier.get().values(); + return ImmutableSet.copyOf(Iterables.concat(Iterables.transform(zones, Suppliers + .> supplierFunction()))); } } - } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java b/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java new file mode 100644 index 0000000000..63eab5cb6e --- /dev/null +++ b/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegion.java @@ -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.ec2.suppliers; + +import java.util.Map; +import java.util.Set; + +import javax.annotation.Resource; +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.ec2.EC2Client; +import org.jclouds.ec2.domain.AvailabilityZoneInfo; +import org.jclouds.ec2.services.AvailabilityZoneAndRegionClient; +import org.jclouds.http.HttpResponseException; +import org.jclouds.location.Region; +import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier; +import org.jclouds.logging.Logger; +import org.jclouds.util.Suppliers2; + +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import com.google.common.collect.ImmutableMap.Builder; + +@Singleton +public class DescribeAvailabilityZonesInRegion implements RegionIdToZoneIdsSupplier { + @Resource + protected Logger logger = Logger.NULL; + + private final AvailabilityZoneAndRegionClient client; + private final Supplier> regions; + + @Inject + public DescribeAvailabilityZonesInRegion(EC2Client client, @Region Supplier> regions) { + this.client = client.getAvailabilityZoneAndRegionServices(); + this.regions = regions; + } + + @Override + public Map>> get() { + Builder> map = ImmutableMap.builder(); + HttpResponseException exception = null; + // TODO: this should be parallel + for (String region : regions.get()) { + try { + ImmutableSet zones = ImmutableSet.copyOf(Iterables.transform(client + .describeAvailabilityZonesInRegion(region), new Function() { + + @Override + public String apply(AvailabilityZoneInfo arg0) { + return arg0.getZone(); + } + + })); + if (zones.size() > 0) + map.put(region, zones); + } catch (HttpResponseException e) { + // TODO: this should be in retry handler, not here. + if (e.getMessage().contains("Unable to tunnel through proxy")) { + exception = e; + logger.error(e, "Could not describe availability zones in Region: %s", region); + } else { + throw e; + } + } + } + ImmutableMap> result = map.build(); + if (result.isEmpty() && exception != null) { + throw exception; + } + return Maps.transformValues(result, Suppliers2.> ofInstanceFunction()); + } + +} \ No newline at end of file diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForConfiguredRegions.java b/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForConfiguredRegions.java new file mode 100644 index 0000000000..6e3d7ab9e7 --- /dev/null +++ b/apis/ec2/src/main/java/org/jclouds/ec2/suppliers/DescribeRegionsForConfiguredRegions.java @@ -0,0 +1,59 @@ +/** + * 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.ec2.suppliers; + +import java.net.URI; +import java.util.Map; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.jclouds.ec2.EC2Client; +import org.jclouds.ec2.services.AvailabilityZoneAndRegionClient; +import org.jclouds.location.Region; +import org.jclouds.location.suppliers.RegionIdToURISupplier; +import org.jclouds.util.Suppliers2; + +import com.google.common.base.Predicates; +import com.google.common.base.Supplier; +import com.google.common.collect.Maps; + +@Singleton +public class DescribeRegionsForConfiguredRegions implements RegionIdToURISupplier { + private final AvailabilityZoneAndRegionClient client; + private final Supplier> regions; + + @Inject + public DescribeRegionsForConfiguredRegions(EC2Client client, @Region Supplier> regions) { + this.client = client.getAvailabilityZoneAndRegionServices(); + this.regions = regions; + } + + @Singleton + @Region + @Override + public Map> get() { + Set regionWhiteList = regions.get(); + Map regionToUris = client.describeRegions(); + if (regionWhiteList.size() > 0) + regionToUris = Maps.filterKeys(regionToUris, Predicates.in(regionWhiteList)); + return Maps.transformValues(regionToUris, Suppliers2. ofInstanceFunction()); + } +} \ No newline at end of file diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java index d9761653d5..030aa79e92 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/AttachmentHandler.java @@ -23,13 +23,15 @@ import java.util.Date; import javax.annotation.Resource; import javax.inject.Inject; -import org.jclouds.ec2.domain.Attachment; import org.jclouds.aws.util.AWSUtils; import org.jclouds.date.DateService; +import org.jclouds.ec2.domain.Attachment; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; import org.jclouds.logging.Logger; +import com.google.common.base.Supplier; + /** * * @author Adrian Cole @@ -40,10 +42,10 @@ public class AttachmentHandler extends ParseSax.HandlerForGeneratedRequestWithRe protected Logger logger = Logger.NULL; protected final DateService dateService; - protected final String defaultRegion; + protected final Supplier defaultRegion; @Inject - AttachmentHandler(DateService dateService, @Region String defaultRegion) { + AttachmentHandler(DateService dateService, @Region Supplier defaultRegion) { this.dateService = dateService; this.defaultRegion = defaultRegion; } @@ -58,7 +60,7 @@ public class AttachmentHandler extends ParseSax.HandlerForGeneratedRequestWithRe public Attachment getResult() { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); return new Attachment(region, volumeId, instanceId, device, attachmentStatus, attachTime); } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java index 38da0d53a6..2e8cd5578e 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/BaseReservationHandler.java @@ -41,6 +41,7 @@ import org.jclouds.location.Region; import org.jclouds.logging.Logger; import org.xml.sax.Attributes; +import com.google.common.base.Supplier; import com.google.common.collect.Sets; import com.google.inject.Provider; @@ -54,11 +55,11 @@ public abstract class BaseReservationHandler extends HandlerForGeneratedReque protected Logger logger = Logger.NULL; protected final DateService dateService; - protected final String defaultRegion; + protected final Supplier defaultRegion; protected final Provider builderProvider; @Inject - public BaseReservationHandler(DateService dateService, @Region String defaultRegion, + public BaseReservationHandler(DateService dateService, @Region Supplier defaultRegion, Provider builderProvider) { this.dateService = dateService; this.defaultRegion = defaultRegion; @@ -205,7 +206,7 @@ public abstract class BaseReservationHandler extends HandlerForGeneratedReque builder.privateDnsName(null); } - builder.region((region == null) ? defaultRegion : region); + builder.region((region == null) ? defaultRegion.get() : region); builder.groupIds(groupIds); } @@ -224,7 +225,7 @@ public abstract class BaseReservationHandler extends HandlerForGeneratedReque protected Reservation newReservation() { String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; if (region == null) - region = defaultRegion; + region = defaultRegion.get(); Reservation info = new Reservation(region, groupIds, instances, ownerId, requesterId, reservationId); this.groupIds = Sets.newLinkedHashSet(); diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java index cac2f98d14..81c7f0d320 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/BundleTaskHandler.java @@ -22,12 +22,14 @@ import java.util.Date; import javax.inject.Inject; -import org.jclouds.ec2.domain.BundleTask; import org.jclouds.aws.util.AWSUtils; import org.jclouds.date.DateService; +import org.jclouds.ec2.domain.BundleTask; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; +import com.google.common.base.Supplier; + /** * * @author Adrian Cole @@ -39,7 +41,7 @@ public class BundleTaskHandler extends ParseSax.HandlerForGeneratedRequestWithRe protected DateService dateService; @Inject @Region - String defaultRegion; + Supplier defaultRegion; private String bundleId; private String code; @@ -55,7 +57,7 @@ public class BundleTaskHandler extends ParseSax.HandlerForGeneratedRequestWithRe public BundleTask getResult() { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); BundleTask.Error error = null; if (code != null) error = new BundleTask.Error(code, message); diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java index 8fdcf3ff31..2e11c937eb 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/CreateVolumeResponseHandler.java @@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Date; import java.util.Map; import java.util.Set; +import java.util.Map.Entry; import javax.annotation.Resource; import javax.inject.Inject; @@ -39,6 +40,9 @@ import org.jclouds.logging.Logger; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.xml.sax.Attributes; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.google.common.collect.Maps; import com.google.common.collect.Sets; /** @@ -54,11 +58,14 @@ public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedReq protected DateService dateService; @Inject @Region - String defaultRegion; + Supplier defaultRegion; @Inject @Zone - protected Map availabilityZoneToRegion; - + protected Supplier>>> regionToZonesSupplier; + @Inject + @Zone + protected Supplier> zonesSupplier; + private String id; private int size; private String snapshotId; @@ -159,12 +166,21 @@ public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedReq super.setContext(request); region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) { - String zone = findAvailabilityZoneInArgsOrNull(getRequest(), availabilityZoneToRegion.keySet()); + Set zones = zonesSupplier.get(); + String zone = findAvailabilityZoneInArgsOrNull(getRequest(), zones); if (zone != null) { - region = checkNotNull(availabilityZoneToRegion.get(zone), - String.format("zone %s not in %s", zone, availabilityZoneToRegion)); + Map> regionToZones = Maps.transformValues(regionToZonesSupplier.get(), Suppliers + .> supplierFunction()); + for (Entry> entry : regionToZones.entrySet()) { + if (entry.getValue().contains(zone)) { + region = entry.getKey(); + break; + } + + } + checkNotNull(regionToZones, String.format("zone %s not in %s", zone, regionToZones)); } else { - region = defaultRegion; + region = defaultRegion.get(); } } return this; diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java index 8b8cd37d7f..d42dec1982 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java @@ -29,6 +29,7 @@ import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult; import org.jclouds.location.Region; import org.jclouds.logging.Logger; +import com.google.common.base.Supplier; import com.google.common.collect.Sets; /** @@ -45,7 +46,7 @@ public class DescribeAddressesResponseHandler extends private StringBuilder currentText = new StringBuilder(); @Inject @Region - String defaultRegion; + Supplier defaultRegion; private String instanceId; protected String currentOrNull() { @@ -61,7 +62,7 @@ public class DescribeAddressesResponseHandler extends } else if (qName.equals("item")) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId)); ipAddress = null; instanceId = null; diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java index fff5a93a39..b19dcd224f 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandler.java @@ -29,6 +29,7 @@ import org.jclouds.location.Region; import org.jclouds.logging.Logger; import org.xml.sax.Attributes; +import com.google.common.base.Supplier; import com.google.common.collect.Sets; /** @@ -37,7 +38,7 @@ import com.google.common.collect.Sets; */ public class DescribeAvailabilityZonesResponseHandler extends ParseSax.HandlerWithResult> { private StringBuilder currentText = new StringBuilder(); - private final String defaultRegion; + private final Supplier defaultRegion; private Set availablilityZones = Sets.newLinkedHashSet(); private String zone; @@ -52,9 +53,13 @@ public class DescribeAvailabilityZonesResponseHandler extends ParseSax.HandlerWi * Eucalyptus 1.6 doesn't return region in the XML output */ @Inject - DescribeAvailabilityZonesResponseHandler(@Region String defaultRegion) { + DescribeAvailabilityZonesResponseHandler(@Region Supplier defaultRegion) { this.defaultRegion = defaultRegion; - region = defaultRegion; + } + + @Override + public void startDocument() { + region = defaultRegion.get(); } public Set getResult() { @@ -86,7 +91,7 @@ public class DescribeAvailabilityZonesResponseHandler extends ParseSax.HandlerWi } else if (qName.equals("item") && !inMessageSet) { availablilityZones.add(new AvailabilityZoneInfo(zone, zoneState, region, messages)); this.zone = null; - this.region = defaultRegion; + this.region = defaultRegion.get(); this.zoneState = null; this.messages = Sets.newHashSet(); } @@ -96,4 +101,5 @@ public class DescribeAvailabilityZonesResponseHandler extends ParseSax.HandlerWi public void characters(char ch[], int start, int length) { currentText.append(ch, start, length); } + } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeImagesResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeImagesResponseHandler.java index 1d12a9178e..9624a77623 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeImagesResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeImagesResponseHandler.java @@ -38,6 +38,7 @@ import org.jclouds.location.Region; import org.jclouds.logging.Logger; import org.xml.sax.Attributes; +import com.google.common.base.Supplier; import com.google.common.collect.Maps; import com.google.common.collect.Sets; @@ -53,7 +54,7 @@ import com.google.common.collect.Sets; public class DescribeImagesResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult> { @Inject - public DescribeImagesResponseHandler(@Region String defaultRegion) { + public DescribeImagesResponseHandler(@Region Supplier defaultRegion) { this.defaultRegion = defaultRegion; } @@ -62,7 +63,7 @@ public class DescribeImagesResponseHandler extends ParseSax.HandlerForGeneratedR private Set contents = Sets.newLinkedHashSet(); private StringBuilder currentText = new StringBuilder(); - private final String defaultRegion; + private final Supplier defaultRegion; private Architecture architecture; private String name; @@ -165,7 +166,7 @@ public class DescribeImagesResponseHandler extends ParseSax.HandlerForGeneratedR try { String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; if (region == null) - region = defaultRegion; + region = defaultRegion.get(); contents.add(new Image(region, architecture, this.name, description, imageId, imageLocation, imageOwnerId, imageState, imageType, isPublic, productCodes, kernelId, platform, ramdiskId, rootDeviceType, rootDeviceName, ebsBlockDevices, virtualizationType, hypervisor)); diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandler.java index 99c2ccf750..ef96a0a774 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandler.java @@ -27,6 +27,7 @@ import org.jclouds.ec2.domain.Reservation; import org.jclouds.ec2.domain.RunningInstance; import org.jclouds.location.Region; +import com.google.common.base.Supplier; import com.google.common.collect.Sets; import com.google.inject.Provider; @@ -43,7 +44,7 @@ public class DescribeInstancesResponseHandler extends private Set> reservations = Sets.newLinkedHashSet(); @Inject - DescribeInstancesResponseHandler(DateService dateService, @Region String defaultRegion, + DescribeInstancesResponseHandler(DateService dateService, @Region Supplier defaultRegion, Provider builderProvider) { super(dateService, defaultRegion, builderProvider); } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandler.java index a4ab1ac359..0ba7a51090 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeKeyPairsResponseHandler.java @@ -30,6 +30,7 @@ import org.jclouds.ec2.domain.KeyPair.Builder; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; +import com.google.common.base.Supplier; import com.google.common.collect.Sets; /** @@ -40,13 +41,17 @@ import com.google.common.collect.Sets; * @author Adrian Cole */ public class DescribeKeyPairsResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult> { - private final String defaultRegion; + private final Supplier defaultRegion; private Builder builder; @Inject - public DescribeKeyPairsResponseHandler(@Region String defaultRegion) { + public DescribeKeyPairsResponseHandler(@Region Supplier defaultRegion) { this.defaultRegion = defaultRegion; - builder = KeyPair.builder().region(defaultRegion); + } + + @Override + public void startDocument() { + builder = KeyPair.builder().region(defaultRegion.get()); } private StringBuilder currentText = new StringBuilder(); @@ -67,7 +72,7 @@ public class DescribeKeyPairsResponseHandler extends ParseSax.HandlerForGenerate try { keyPairs.add(builder.build()); } finally { - builder = KeyPair.builder().region(defaultRegion); + builder = KeyPair.builder().region(defaultRegion.get()); } } else if (qName.equals("keyName")) { builder.keyName(currentOrNull(currentText)); diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandler.java index 7ce9eb18d5..9b1b53ddf9 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeSecurityGroupsResponseHandler.java @@ -18,8 +18,8 @@ */ package org.jclouds.ec2.xml; -import static org.jclouds.util.SaxUtils.currentOrNull; import static org.jclouds.util.SaxUtils.currentOrNegative; +import static org.jclouds.util.SaxUtils.currentOrNull; import static org.jclouds.util.SaxUtils.equalsOrSuffix; import java.util.Set; @@ -34,6 +34,7 @@ import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; import org.xml.sax.Attributes; +import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; @@ -52,7 +53,7 @@ public class DescribeSecurityGroupsResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult> { @Inject @Region - String defaultRegion; + Supplier defaultRegion; private StringBuilder currentText = new StringBuilder(); private Set securtyGroups = Sets.newLinkedHashSet(); @@ -135,7 +136,7 @@ public class DescribeSecurityGroupsResponseHandler extends } else if (!inIpPermissions && !inIpRanges && !inGroups) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); securtyGroups.add(new SecurityGroup(region, groupId, groupName, ownerId, groupDescription, ipPermissions)); this.groupName = null; this.groupId = null; diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/InstanceStateChangeHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/InstanceStateChangeHandler.java index df44f8f889..c2fdd157dd 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/InstanceStateChangeHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/InstanceStateChangeHandler.java @@ -29,6 +29,7 @@ import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult; import org.jclouds.location.Region; import org.xml.sax.Attributes; +import com.google.common.base.Supplier; import com.google.common.collect.Sets; /** @@ -51,7 +52,7 @@ public class InstanceStateChangeHandler extends private StringBuilder currentText = new StringBuilder(); @Inject @Region - String defaultRegion; + Supplier defaultRegion; Set instances = Sets.newLinkedHashSet(); private InstanceState shutdownState; @@ -92,7 +93,7 @@ public class InstanceStateChangeHandler extends } else if (qName.equals("item")) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); instances.add(new InstanceStateChange(region, instanceId, shutdownState, previousState)); this.instanceId = null; this.shutdownState = null; diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/KeyPairResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/KeyPairResponseHandler.java index de44b37da9..c0bfa809ba 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/KeyPairResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/KeyPairResponseHandler.java @@ -28,6 +28,8 @@ import org.jclouds.ec2.domain.KeyPair.Builder; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; +import com.google.common.base.Supplier; + /** * * @see { - private final String defaultRegion; + private final Supplier defaultRegion; private Builder builder; @Inject - public KeyPairResponseHandler(@Region String defaultRegion) { + public KeyPairResponseHandler(@Region Supplier defaultRegion) { this.defaultRegion = defaultRegion; - builder = KeyPair.builder().region(defaultRegion); + } + + @Override + public void startDocument() { + builder = KeyPair.builder().region(defaultRegion.get()); } private StringBuilder currentText = new StringBuilder(); @@ -54,7 +60,7 @@ public class KeyPairResponseHandler extends ParseSax.HandlerForGeneratedRequestW try { return builder.build(); } finally { - builder = KeyPair.builder().region(defaultRegion); + builder = KeyPair.builder().region(defaultRegion.get()); } } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/RunInstancesResponseHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/RunInstancesResponseHandler.java index 1919242dea..c7fe32718a 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/RunInstancesResponseHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/RunInstancesResponseHandler.java @@ -1,5 +1,5 @@ /** - * Licensed to jclouds, Inc. (jclouds) under one or more +s * 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 @@ -25,6 +25,7 @@ import org.jclouds.ec2.domain.Reservation; import org.jclouds.ec2.domain.RunningInstance; import org.jclouds.location.Region; +import com.google.common.base.Supplier; import com.google.inject.Provider; /** @@ -38,7 +39,7 @@ import com.google.inject.Provider; public class RunInstancesResponseHandler extends BaseReservationHandler> { @Inject - RunInstancesResponseHandler(DateService dateService, @Region String defaultRegion, + RunInstancesResponseHandler(DateService dateService, @Region Supplier defaultRegion, Provider builderProvider) { super(dateService, defaultRegion, builderProvider); } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/xml/SnapshotHandler.java b/apis/ec2/src/main/java/org/jclouds/ec2/xml/SnapshotHandler.java index dac9883560..fb1c7c5b82 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/xml/SnapshotHandler.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/xml/SnapshotHandler.java @@ -22,13 +22,15 @@ import java.util.Date; import javax.inject.Inject; -import org.jclouds.ec2.domain.Snapshot; -import org.jclouds.ec2.domain.Snapshot.Status; import org.jclouds.aws.util.AWSUtils; import org.jclouds.date.DateService; +import org.jclouds.ec2.domain.Snapshot; +import org.jclouds.ec2.domain.Snapshot.Status; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; +import com.google.common.base.Supplier; + /** * * @author Adrian Cole @@ -37,7 +39,7 @@ public class SnapshotHandler extends ParseSax.HandlerForGeneratedRequestWithResu private StringBuilder currentText = new StringBuilder(); protected final DateService dateService; - protected final String defaultRegion; + protected final Supplier defaultRegion; private String id; private String volumeId; @@ -50,7 +52,7 @@ public class SnapshotHandler extends ParseSax.HandlerForGeneratedRequestWithResu private String ownerAlias; @Inject - public SnapshotHandler(DateService dateService, @Region String defaultRegion) { + public SnapshotHandler(DateService dateService, @Region Supplier defaultRegion) { this.dateService = dateService; this.defaultRegion = defaultRegion; } @@ -58,7 +60,7 @@ public class SnapshotHandler extends ParseSax.HandlerForGeneratedRequestWithResu public Snapshot getResult() { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); Snapshot snapshot = new Snapshot(region, id, volumeId, volumeSize, status, startTime, progress, ownerId, description, ownerAlias); this.id = null; diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/services/BaseEC2AsyncClientTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/services/BaseEC2AsyncClientTest.java index dff62b2aba..3e1ae2ab85 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/services/BaseEC2AsyncClientTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/services/BaseEC2AsyncClientTest.java @@ -18,11 +18,13 @@ */ package org.jclouds.ec2.services; +import static com.google.common.collect.Maps.transformValues; import static org.testng.Assert.assertEquals; import java.io.IOException; import java.net.URI; import java.util.Map; +import java.util.Set; import javax.inject.Singleton; @@ -38,13 +40,18 @@ import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.ec2.config.EC2RestClientModule; import org.jclouds.http.HttpRequest; import org.jclouds.http.RequiresHttp; +import org.jclouds.location.config.LocationModule; +import org.jclouds.location.suppliers.RegionIdToURISupplier; +import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier; import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.RestClientTest; import org.jclouds.rest.RestContextFactory; import org.jclouds.rest.RestContextSpec; +import org.jclouds.util.Suppliers2; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -84,29 +91,38 @@ public abstract class BaseEC2AsyncClientTest extends RestClientTest { return "2009-11-08T15:54:08.897Z"; } - protected void bindRegionsToProvider() { - bindRegionsToProvider(Regions.class); - } - - static class Regions implements javax.inject.Provider> { - @Override - public Map get() { - return ImmutableMap. 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")); - } - } - - protected void bindZonesToProvider() { - bindZonesToProvider(Zones.class); - } - static class Zones implements javax.inject.Provider> { @Override public Map get() { return ImmutableMap. of("us-east-1a", "us-east-1"); } } + + @Override + protected void installLocations() { + install(new LocationModule()); + bind(RegionIdToURISupplier.class).toInstance(new RegionIdToURISupplier() { + + @Override + public Map> get() { + return transformValues(ImmutableMap. 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")), Suppliers2. ofInstanceFunction()); + } + + }); + bind(RegionIdToZoneIdsSupplier.class).toInstance(new RegionIdToZoneIdsSupplier() { + + @Override + public Map>> get() { + return transformValues(ImmutableMap.> of("us-east-1", ImmutableSet.of( + "us-east-1a", "us-east-1b", "us-east-1c", "us-east-1b")), Suppliers2 + .> ofInstanceFunction()); + } + + }); + } } protected FormSigner filter; diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/config/EC2RestClientModuleTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionTest.java similarity index 64% rename from apis/ec2/src/test/java/org/jclouds/ec2/config/EC2RestClientModuleTest.java rename to apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionTest.java index cb223eb89f..d9b83f4edd 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/config/EC2RestClientModuleTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/suppliers/DescribeAvailabilityZonesInRegionTest.java @@ -16,31 +16,38 @@ * specific language governing permissions and limitations * under the License. */ -package org.jclouds.ec2.config; +package org.jclouds.ec2.suppliers; -import static org.easymock.classextension.EasyMock.*; +import static org.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.createControl; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; import org.easymock.classextension.IMocksControl; import org.jclouds.ec2.EC2Client; import org.jclouds.ec2.domain.AvailabilityZoneInfo; import org.jclouds.ec2.services.AvailabilityZoneAndRegionClient; +import org.jclouds.ec2.suppliers.DescribeAvailabilityZonesInRegion; import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpResponseException; import org.testng.annotations.Test; -import static org.testng.Assert.*; -import java.net.URI; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; /** - * A test for {@link EC2RestClientModule}. - * + * A test for {@link DescribeAvailabilityZonesInRegion}. + * * @author Eric Pabst (pabstec@familysearch.org) */ -public class EC2RestClientModuleTest { +public class DescribeAvailabilityZonesInRegionTest { @Test public void testDescribeAvailabilityZonesInRegion_BestEffort() { IMocksControl control = createControl(); @@ -49,27 +56,28 @@ public class EC2RestClientModuleTest { AvailabilityZoneInfo info1 = control.createMock(AvailabilityZoneInfo.class); AvailabilityZoneInfo info2 = control.createMock(AvailabilityZoneInfo.class); HttpCommand command = control.createMock(HttpCommand.class); - HttpResponseException exception = new HttpResponseException("Error: Unable to tunnel through proxy: ...", command, null); + HttpResponseException exception = new HttpResponseException("Error: Unable to tunnel through proxy: ...", + command, null); expect(client.getAvailabilityZoneAndRegionServices()).andStubReturn(regionClient); - expect(regionClient.describeAvailabilityZonesInRegion("accessibleRegion1")).andReturn(Collections.singleton(info1)); + expect(regionClient.describeAvailabilityZonesInRegion("accessibleRegion1")).andReturn( + Collections.singleton(info1)); expect(regionClient.describeAvailabilityZonesInRegion("inaccessibleRegion")).andThrow(exception); - expect(regionClient.describeAvailabilityZonesInRegion("accessibleRegion2")).andReturn(Collections.singleton(info2)); + expect(regionClient.describeAvailabilityZonesInRegion("accessibleRegion2")).andReturn( + Collections.singleton(info2)); expect(info1.getZone()).andStubReturn("zone1"); expect(info2.getZone()).andStubReturn("zone2"); - Map regions = new LinkedHashMap(); - regions.put("accessibleRegion1", null); - regions.put("inaccessibleRegion", null); - regions.put("accessibleRegion2", null); + Set regions = ImmutableSet.of("accessibleRegion1", "inaccessibleRegion", "accessibleRegion2"); control.replay(); - Map expectedResult = new HashMap(); - expectedResult.put("zone1", "accessibleRegion1"); - expectedResult.put("zone2", "accessibleRegion2"); + Map> expectedResult = ImmutableMap.> builder().put("accessibleRegion1", + ImmutableSet.of("zone1")).put("accessibleRegion2", ImmutableSet.of("zone2")).build(); - EC2RestClientModule.RegionIdToZoneId regionIdToZoneId = new EC2RestClientModule.RegionIdToZoneId(client, regions); - assertEquals(regionIdToZoneId.get(), expectedResult); + DescribeAvailabilityZonesInRegion regionIdToZoneId = new DescribeAvailabilityZonesInRegion(client, Suppliers + .ofInstance(regions)); + assertEquals(Maps.transformValues(regionIdToZoneId.get(), Suppliers.> supplierFunction()), + expectedResult); control.verify(); } @@ -79,16 +87,17 @@ public class EC2RestClientModuleTest { EC2Client client = control.createMock(EC2Client.class); AvailabilityZoneAndRegionClient regionClient = control.createMock(AvailabilityZoneAndRegionClient.class); HttpCommand command = control.createMock(HttpCommand.class); - HttpResponseException exception = new HttpResponseException("Error: Unable to tunnel through proxy: ...", command, null); + HttpResponseException exception = new HttpResponseException("Error: Unable to tunnel through proxy: ...", + command, null); expect(client.getAvailabilityZoneAndRegionServices()).andStubReturn(regionClient); expect(regionClient.describeAvailabilityZonesInRegion("inaccessibleRegion")).andThrow(exception); - Map regions = new LinkedHashMap(); - regions.put("inaccessibleRegion", null); + Set regions = ImmutableSet.of("inaccessibleRegion"); control.replay(); - EC2RestClientModule.RegionIdToZoneId regionIdToZoneId = new EC2RestClientModule.RegionIdToZoneId(client, regions); + DescribeAvailabilityZonesInRegion regionIdToZoneId = new DescribeAvailabilityZonesInRegion(client, Suppliers + .ofInstance(regions)); try { regionIdToZoneId.get(); fail("expected exception"); @@ -105,14 +114,15 @@ public class EC2RestClientModuleTest { AvailabilityZoneAndRegionClient regionClient = control.createMock(AvailabilityZoneAndRegionClient.class); expect(client.getAvailabilityZoneAndRegionServices()).andStubReturn(regionClient); - expect(regionClient.describeAvailabilityZonesInRegion("emptyRegion")).andReturn(Collections.emptySet()); + expect(regionClient.describeAvailabilityZonesInRegion("emptyRegion")).andReturn( + Collections. emptySet()); - Map regions = new LinkedHashMap(); - regions.put("emptyRegion", null); + Set regions = ImmutableSet.of("emptyRegion"); control.replay(); - EC2RestClientModule.RegionIdToZoneId regionIdToZoneId = new EC2RestClientModule.RegionIdToZoneId(client, regions); - assertEquals(regionIdToZoneId.get(), Collections.emptyMap()); + DescribeAvailabilityZonesInRegion regionIdToZoneId = new DescribeAvailabilityZonesInRegion(client, Suppliers + .ofInstance(regions)); + assertEquals(regionIdToZoneId.get(), Collections. emptyMap()); control.verify(); } } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/BaseEC2HandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/BaseEC2HandlerTest.java index f1dbf902f6..aafc0057cc 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/BaseEC2HandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/BaseEC2HandlerTest.java @@ -19,6 +19,7 @@ package org.jclouds.ec2.xml; import java.util.Map; +import java.util.Set; import javax.inject.Singleton; @@ -27,10 +28,15 @@ import org.jclouds.http.functions.BaseHandlerTest; import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.location.Zone; +import org.jclouds.util.Suppliers2; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Provides; @@ -57,20 +63,29 @@ public abstract class BaseEC2HandlerTest extends BaseHandlerTest { @Singleton @Provides @org.jclouds.location.Region - String provideDefaultRegion() { - return defaultRegion; + Supplier provideDefaultRegion() { + return Suppliers.ofInstance(defaultRegion); } @SuppressWarnings("unused") @Singleton @Provides @Zone - Map provideAvailabilityZoneRegionMap() { - return ImmutableMap. of("us-east-1a", "us-east-1"); + Supplier>>> provideRegionToAvailabilityZoneMap() { + return Suppliers.>>> ofInstance(Maps.transformValues(ImmutableMap + .> of("us-east-1", ImmutableSet.of("us-east-1a")), Suppliers2 + .> ofInstanceFunction())); + } + + @SuppressWarnings("unused") + @Singleton + @Provides + @Zone + Supplier> provideZones() { + return Suppliers.> ofInstance(ImmutableSet.of("us-east-1a")); } }); factory = injector.getInstance(ParseSax.Factory.class); assert factory != null; } - } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandlerTest.java index f1109cfae7..5fcb878bdf 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAvailabilityZonesResponseHandlerTest.java @@ -23,6 +23,8 @@ import static org.testng.Assert.assertEquals; import java.io.InputStream; import java.util.Set; +import javax.inject.Singleton; + import org.jclouds.ec2.domain.AvailabilityZoneInfo; import org.jclouds.http.functions.BaseHandlerTest; import org.jclouds.http.functions.ParseSax; @@ -30,8 +32,11 @@ import org.jclouds.http.functions.config.SaxParserModule; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import com.google.inject.Guice; +import com.google.inject.Provides; /** * Tests behavior of {@code DescribeAvailabilityZonesResponseHandler} @@ -45,11 +50,13 @@ public class DescribeAvailabilityZonesResponseHandlerTest extends BaseHandlerTes @BeforeTest protected void setUpInjector() { injector = Guice.createInjector(new SaxParserModule() { - - @Override - protected void configure() { - bindConstant().annotatedWith(org.jclouds.location.Region.class).to("SHOULDNTSEETHISASXMLHASREGIONDATA"); - super.configure(); + + @SuppressWarnings("unused") + @Singleton + @Provides + @org.jclouds.location.Region + Supplier provideDefaultRegion() { + return Suppliers.ofInstance("SHOULDNTSEETHISASXMLHASREGIONDATA"); } }); diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeImagesResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeImagesResponseHandlerTest.java index 9c78386647..d1fbeb39c8 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeImagesResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeImagesResponseHandlerTest.java @@ -37,12 +37,15 @@ import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.location.Region; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code DescribeImagesResponseHandler} @@ -95,7 +98,8 @@ public class DescribeImagesResponseHandlerTest { @Override protected void configure() { - bind(String.class).annotatedWith(Region.class).toInstance("us-east-1"); + bind(new TypeLiteral>() { + }).annotatedWith(Region.class).toInstance(Suppliers.ofInstance("us-east-1")); } }); diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandlerTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandlerTest.java index 9bc2ac4b74..e0e3aaae0d 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandlerTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeInstancesResponseHandlerTest.java @@ -38,10 +38,13 @@ import org.jclouds.location.Region; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code DescribeInstancesResponseHandler} @@ -142,7 +145,8 @@ public class DescribeInstancesResponseHandlerTest extends BaseEC2HandlerTest { @Override protected void configure() { - bind(String.class).annotatedWith(Region.class).toInstance("us-east-1"); + bind(new TypeLiteral>() { + }).annotatedWith(Region.class).toInstance(Suppliers.ofInstance("us-east-1")); } }); diff --git a/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java b/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java index c38c47a3df..709333e7df 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java +++ b/apis/s3/src/main/java/org/jclouds/s3/blobstore/config/S3BlobStoreContextModule.java @@ -30,7 +30,6 @@ import org.jclouds.blobstore.attr.ConsistencyModel; import org.jclouds.blobstore.config.BlobStoreMapModule; import org.jclouds.blobstore.internal.BlobStoreContextImpl; import org.jclouds.domain.Location; -import org.jclouds.location.config.RegionsLocationModule; import org.jclouds.s3.S3AsyncClient; import org.jclouds.s3.S3Client; import org.jclouds.s3.blobstore.S3AsyncBlobStore; @@ -59,7 +58,6 @@ public class S3BlobStoreContextModule extends AbstractModule { @Override protected void configure() { install(new BlobStoreMapModule()); - install(new RegionsLocationModule()); bind(ConsistencyModel.class).toInstance(ConsistencyModel.EVENTUAL); bind(AsyncBlobStore.class).to(S3AsyncBlobStore.class).in(Scopes.SINGLETON); bind(BlobStore.class).to(S3BlobStore.class).in(Scopes.SINGLETON); diff --git a/apis/s3/src/main/java/org/jclouds/s3/config/S3RestClientModule.java b/apis/s3/src/main/java/org/jclouds/s3/config/S3RestClientModule.java index 5b784c92bc..9fcfb355df 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/config/S3RestClientModule.java +++ b/apis/s3/src/main/java/org/jclouds/s3/config/S3RestClientModule.java @@ -34,7 +34,6 @@ 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.location.Region; import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.RequestSigner; @@ -77,8 +76,7 @@ public class S3RestClientModule ext @Provides @Bucket @Singleton - @Nullable - protected String defaultRegionForBucket(@Nullable @Region String defaultRegion) { + protected Supplier defaultRegionForBucket(@Region Supplier defaultRegion) { return defaultRegion; } diff --git a/apis/s3/src/main/java/org/jclouds/s3/functions/BindRegionToXmlPayload.java b/apis/s3/src/main/java/org/jclouds/s3/functions/BindRegionToXmlPayload.java index 0f18d412d7..88ab1e85a8 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/functions/BindRegionToXmlPayload.java +++ b/apis/s3/src/main/java/org/jclouds/s3/functions/BindRegionToXmlPayload.java @@ -23,17 +23,19 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Set; -import org.jclouds.javax.annotation.Nullable; import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Singleton; import javax.ws.rs.core.MediaType; import org.jclouds.http.HttpRequest; +import org.jclouds.location.Region; import org.jclouds.logging.Logger; import org.jclouds.rest.binders.BindToStringPayload; import org.jclouds.s3.Bucket; +import com.google.common.base.Supplier; + /** * * Depending on your latency and legal requirements, you can specify a location @@ -47,26 +49,29 @@ public class BindRegionToXmlPayload extends BindToStringPayload { @Resource protected Logger logger = Logger.NULL; - private final String defaultRegionForEndpoint; - private final String defaultRegionForService; - private final Set regions; + private final Supplier defaultRegionForEndpointSupplier; + private final Supplier defaultRegionForServiceSupplier; + private final Supplier> regionsSupplier; @Inject - public BindRegionToXmlPayload(@org.jclouds.location.Region @Nullable String defaultRegionForEndpoint, - @Nullable @Bucket String defaultRegionForService, @org.jclouds.location.Region Set regions) { - this.defaultRegionForEndpoint = defaultRegionForEndpoint; - this.defaultRegionForService = defaultRegionForService; - this.regions = checkNotNull(regions, "regions"); + public BindRegionToXmlPayload(@Region Supplier defaultRegionForEndpointSupplier, + @Bucket Supplier defaultRegionForServiceSupplier, @Region Supplier> regionsSupplier) { + this.defaultRegionForEndpointSupplier = defaultRegionForEndpointSupplier; + this.defaultRegionForServiceSupplier = defaultRegionForServiceSupplier; + this.regionsSupplier = checkNotNull(regionsSupplier, "regions"); } @Override public R bindToRequest(R request, Object input) { + String defaultRegionForEndpoint = defaultRegionForEndpointSupplier.get(); if (defaultRegionForEndpoint == null) return request; input = input == null ? defaultRegionForEndpoint : input; checkArgument(input instanceof String, "this binder is only valid for Region!"); String constraint = (String) input; String value = null; + String defaultRegionForService = defaultRegionForServiceSupplier.get(); + Set regions = regionsSupplier.get(); if ((defaultRegionForService == null && constraint == null) || (defaultRegionForService != null && defaultRegionForService.equals(constraint))) { // nothing to bind as this is default. diff --git a/common/aws/src/main/java/org/jclouds/aws/config/AWSRestClientModule.java b/common/aws/src/main/java/org/jclouds/aws/config/AWSRestClientModule.java index b4440ef4ef..42f5929b52 100644 --- a/common/aws/src/main/java/org/jclouds/aws/config/AWSRestClientModule.java +++ b/common/aws/src/main/java/org/jclouds/aws/config/AWSRestClientModule.java @@ -18,13 +18,8 @@ */ package org.jclouds.aws.config; -import static com.google.common.collect.Iterables.get; -import java.net.URI; import java.util.Map; -import java.util.Set; - -import javax.inject.Singleton; import org.jclouds.aws.handlers.AWSClientErrorRetryHandler; import org.jclouds.aws.handlers.ParseAWSErrorFromXmlContent; @@ -34,18 +29,9 @@ 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.location.Provider; -import org.jclouds.location.Region; -import org.jclouds.location.config.ProvideRegionToURIViaProperties; -import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.config.RestClientModule; -import com.google.common.collect.ImmutableBiMap; -import com.google.inject.Provides; -import com.google.inject.Scopes; -import com.google.inject.TypeLiteral; /** * @@ -75,40 +61,4 @@ public class AWSRestClientModule extends RestClientModule { bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(AWSClientErrorRetryHandler.class); } - @Provides - @Singleton - @Nullable - @Region - protected String getDefaultRegion(@Provider URI uri, @Region Map 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; - } - - protected void bindRegionsToProvider() { - bindRegionsToProvider(ProvideRegionToURIViaProperties.class); - } - - @Override - protected void configure() { - super.configure(); - bindRegionsToProvider(); - } - - protected void bindRegionsToProvider(Class>> providerClass) { - bind(new TypeLiteral>() { - }).annotatedWith(Region.class).toProvider(providerClass).in(Scopes.SINGLETON); - } - - @Provides - @Singleton - @Region - protected Set provideRegions(@Region Map map) { - return map.keySet(); - } - } \ No newline at end of file diff --git a/common/aws/src/main/java/org/jclouds/aws/config/WithZonesFormSigningRestClientModule.java b/common/aws/src/main/java/org/jclouds/aws/config/WithZonesFormSigningRestClientModule.java index f8f69b1435..0d2d8f21ad 100644 --- a/common/aws/src/main/java/org/jclouds/aws/config/WithZonesFormSigningRestClientModule.java +++ b/common/aws/src/main/java/org/jclouds/aws/config/WithZonesFormSigningRestClientModule.java @@ -18,22 +18,12 @@ */ package org.jclouds.aws.config; -import java.net.URI; + import java.util.Map; -import javax.inject.Singleton; - import org.jclouds.http.RequiresHttp; -import org.jclouds.location.Region; -import org.jclouds.location.Zone; -import org.jclouds.location.config.ProvideZonesViaProperties; import org.jclouds.rest.ConfiguresRestClient; -import com.google.common.base.Function; -import com.google.common.collect.Maps; -import com.google.inject.Provides; -import com.google.inject.Scopes; -import com.google.inject.TypeLiteral; /** * @@ -44,7 +34,7 @@ import com.google.inject.TypeLiteral; public class WithZonesFormSigningRestClientModule extends FormSigningRestClientModule { public WithZonesFormSigningRestClientModule(Class syncClientType, Class asyncClientType, - Map, Class> delegates) { + Map, Class> delegates) { super(syncClientType, asyncClientType, delegates); } @@ -52,35 +42,4 @@ public class WithZonesFormSigningRestClientModule extends FormSigningRestC super(syncClientType, asyncClientType); } - protected void bindZonesToProvider() { - bindZonesToProvider(ProvideZonesViaProperties.class); - } - - protected void bindZonesToProvider(Class>> providerClass) { - bind(new TypeLiteral>() { - }).annotatedWith(Zone.class).toProvider(providerClass).in(Scopes.SINGLETON); - } - - @Override - protected void configure() { - super.configure(); - bindZonesToProvider(); - } - - @Provides - @Singleton - @Zone - protected Map provideZones(@Region final Map regionToEndpoint, - @Zone Map availabilityZoneToRegion) { - return Maps.transformValues(availabilityZoneToRegion, new Function() { - - @Override - public URI apply(String from) { - - return regionToEndpoint.get(from); - } - - }); - } - } \ No newline at end of file diff --git a/common/aws/src/test/java/org/jclouds/aws/config/AWSWithZonesFormSigningRestClientModuleTest.java b/common/aws/src/test/java/org/jclouds/aws/config/AWSWithZonesFormSigningRestClientModuleTest.java deleted file mode 100644 index bac6f8d3d7..0000000000 --- a/common/aws/src/test/java/org/jclouds/aws/config/AWSWithZonesFormSigningRestClientModuleTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * 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.config; - -import static org.testng.Assert.assertEquals; - -import java.net.URI; -import java.util.Map; - -import org.jclouds.logging.jdk.JDKLogger; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableMap; - -/** - * - * @author Adrian Cole - */ -@Test(sequential = true, groups = { "unit" }) -public class AWSWithZonesFormSigningRestClientModuleTest { - - @Test - public void testDefaultRegionWhenThereIsAMatch() { - AWSRestClientModule module = new WithZonesFormSigningRestClientModule( - String.class, String.class); - - URI currentEndpoint = URI.create("http://region1"); - Map map = ImmutableMap.of("region1", currentEndpoint, "region2", URI.create("http://region2")); - - assertEquals("region1", module.getDefaultRegion(currentEndpoint, map, new JDKLogger.JDKLoggerFactory())); - } - - @Test - public void testDefaultRegionWhenThereIsNoMatch() { - AWSRestClientModule module = new WithZonesFormSigningRestClientModule( - String.class, String.class); - - URI currentEndpoint = URI.create("http://region3"); - Map map = ImmutableMap.of("region1", currentEndpoint, "region2", URI.create("http://region2")); - - assertEquals("region1", module.getDefaultRegion(currentEndpoint, map, new JDKLogger.JDKLoggerFactory())); - } - -} diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceContextModule.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceContextModule.java index 53f93a2e49..f67e0c2e46 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceContextModule.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/config/AWSEC2ComputeServiceContextModule.java @@ -113,10 +113,14 @@ public class AWSEC2ComputeServiceContextModule extends BaseComputeServiceContext } // duplicates EC2ComputeServiceContextModule; but that's easiest thing to do with guice; could extract to common util + // TODO: have a another look at this (Adrian) @Override - protected Supplier> supplyNonParsingImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, + protected Supplier> supplyNonParsingImageCache( + AtomicReference authException, @Named(PROPERTY_SESSION_INTERVAL) long seconds, final Supplier> imageSupplier, Injector injector) { - final Supplier> cache = injector.getInstance(Key.get(new TypeLiteral>>() {})); + final Supplier> cache = injector.getInstance(Key + .get(new TypeLiteral>>() { + })); return new Supplier>() { @Override public Set get() { diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java index d94e193243..8f098247ed 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java @@ -44,6 +44,7 @@ import org.jclouds.ec2.domain.RunningInstance; import org.jclouds.location.Region; import com.google.common.base.Function; +import com.google.common.base.Supplier; /** * @@ -56,7 +57,7 @@ public class AWSEC2ListNodesStrategy extends EC2ListNodesStrategy { protected final SpotInstanceRequestToAWSRunningInstance spotConverter; @Inject - protected AWSEC2ListNodesStrategy(AWSEC2AsyncClient client, @Region Set regions, + protected AWSEC2ListNodesStrategy(AWSEC2AsyncClient client, @Region Supplier> regions, Function runningInstanceToNodeMetadata, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, SpotInstanceRequestToAWSRunningInstance spotConverter) { @@ -67,7 +68,7 @@ public class AWSEC2ListNodesStrategy extends EC2ListNodesStrategy { @Override protected Iterable pollRunningInstances() { - Iterable spots = filter(transform(concat(transformParallel(regions, + Iterable spots = filter(transform(concat(transformParallel(regions.get(), new Function>>() { @SuppressWarnings("unchecked") diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/suppliers/AWSEC2ImageSupplier.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/suppliers/AWSEC2ImageSupplier.java index b1dd1d16fe..49a23b4a15 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/suppliers/AWSEC2ImageSupplier.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/suppliers/AWSEC2ImageSupplier.java @@ -72,14 +72,14 @@ public class AWSEC2ImageSupplier implements Supplier> { private final CallForImages.Factory factory; private final ExecutorService executor; - private final Iterable regions; + private final Supplier> regions; private final String amiQuery; private final Iterable clusterRegions; private final String ccAmiQuery; private final Supplier> cache; @Inject - protected AWSEC2ImageSupplier(@Region Set regions, + protected AWSEC2ImageSupplier(@Region Supplier> regions, @Named(PROPERTY_EC2_AMI_QUERY) String amiQuery, @Named(PROPERTY_EC2_CC_REGIONS) String clusterRegions, @Named(PROPERTY_EC2_CC_AMI_QUERY) String ccAmiQuery, Supplier> cache, @@ -98,7 +98,7 @@ public class AWSEC2ImageSupplier implements Supplier> { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Set get() { - Future> normalImages = images(regions, amiQuery, PROPERTY_EC2_AMI_QUERY); + Future> normalImages = images(regions.get(), amiQuery, PROPERTY_EC2_AMI_QUERY); ImmutableSet clusterImages; try { clusterImages = ImmutableSet.copyOf(images(clusterRegions, ccAmiQuery, PROPERTY_EC2_CC_AMI_QUERY).get()); diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandler.java index e55b5f41da..d98c04ea5c 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandler.java @@ -27,13 +27,13 @@ import javax.inject.Inject; import org.jclouds.aws.ec2.domain.AWSRunningInstance; import org.jclouds.date.DateService; -import org.jclouds.ec2.domain.Hypervisor; import org.jclouds.ec2.domain.Reservation; import org.jclouds.ec2.domain.RunningInstance; import org.jclouds.location.Region; import org.xml.sax.Attributes; import org.xml.sax.SAXException; +import com.google.common.base.Supplier; import com.google.common.collect.Sets; import com.google.inject.Provider; @@ -53,7 +53,7 @@ public class AWSDescribeInstancesResponseHandler extends private String value; @Inject - AWSDescribeInstancesResponseHandler(DateService dateService, @Region String defaultRegion, + AWSDescribeInstancesResponseHandler(DateService dateService, @Region Supplier defaultRegion, Provider builderProvider, TagSetHandler tagSetHandler) { super(dateService, defaultRegion, builderProvider); } diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandler.java index 8ebe583c88..ec835a1e0f 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandler.java @@ -26,6 +26,7 @@ import org.jclouds.ec2.domain.Reservation; import org.jclouds.ec2.domain.RunningInstance; import org.jclouds.location.Region; +import com.google.common.base.Supplier; import com.google.inject.Provider; /** @@ -39,7 +40,7 @@ import com.google.inject.Provider; public class AWSRunInstancesResponseHandler extends BaseAWSReservationHandler> { @Inject - AWSRunInstancesResponseHandler(DateService dateService, @Region String defaultRegion, + AWSRunInstancesResponseHandler(DateService dateService, @Region Supplier defaultRegion, Provider builderProvider) { super(dateService, defaultRegion, builderProvider); } diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java index 8d2c2c23a4..7a25ed57c9 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/BaseAWSReservationHandler.java @@ -45,6 +45,7 @@ import org.jclouds.logging.Logger; import org.xml.sax.Attributes; import org.xml.sax.SAXException; +import com.google.common.base.Supplier; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.inject.Provider; @@ -59,11 +60,11 @@ public abstract class BaseAWSReservationHandler extends HandlerForGeneratedRe protected Logger logger = Logger.NULL; protected final DateService dateService; - protected final String defaultRegion; + protected final Supplier defaultRegion; protected final Provider builderProvider; @Inject - public BaseAWSReservationHandler(DateService dateService, @Region String defaultRegion, + public BaseAWSReservationHandler(DateService dateService, @Region Supplier defaultRegion, Provider builderProvider) { this.dateService = dateService; this.defaultRegion = defaultRegion; @@ -221,7 +222,7 @@ public abstract class BaseAWSReservationHandler extends HandlerForGeneratedRe protected void refineBuilderBeforeAddingInstance() { String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; - builder.region((region == null) ? defaultRegion : region); + builder.region((region == null) ? defaultRegion.get() : region); } protected abstract boolean endOfInstanceItem(); @@ -233,7 +234,7 @@ public abstract class BaseAWSReservationHandler extends HandlerForGeneratedRe protected Reservation newReservation() { String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; if (region == null) - region = defaultRegion; + region = defaultRegion.get(); Reservation info = new Reservation(region, reservationGroupIdToNames.values(), instances, ownerId, requesterId, reservationId); this.reservationGroupIdToNames = Maps.newLinkedHashMap(); diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/LaunchSpecificationHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/LaunchSpecificationHandler.java index 15d16670a7..9737010c86 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/LaunchSpecificationHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/LaunchSpecificationHandler.java @@ -26,7 +26,6 @@ import org.jclouds.aws.ec2.domain.LaunchSpecification.Builder; import org.jclouds.date.DateService; import org.jclouds.ec2.domain.BlockDeviceMapping; import org.jclouds.http.functions.ParseSax.HandlerForGeneratedRequestWithResult; -import org.jclouds.location.Region; import org.jclouds.logging.Logger; import org.xml.sax.Attributes; @@ -40,15 +39,13 @@ public class LaunchSpecificationHandler extends HandlerForGeneratedRequestWithRe protected Logger logger = Logger.NULL; protected final DateService dateService; - protected final String defaultRegion; protected final Builder builder; protected final BlockDeviceMapping.Builder blockDeviceMappingBuilder; @Inject - public LaunchSpecificationHandler(DateService dateService, @Region String defaultRegion, - LaunchSpecification.Builder builder, BlockDeviceMapping.Builder blockDeviceMappingBuilder) { + public LaunchSpecificationHandler(DateService dateService, LaunchSpecification.Builder builder, + BlockDeviceMapping.Builder blockDeviceMappingBuilder) { this.dateService = dateService; - this.defaultRegion = defaultRegion; this.builder = builder; this.blockDeviceMappingBuilder = blockDeviceMappingBuilder; } diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/PlacementGroupHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/PlacementGroupHandler.java index df343d9f46..b8171d29fa 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/PlacementGroupHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/PlacementGroupHandler.java @@ -26,6 +26,8 @@ import org.jclouds.aws.util.AWSUtils; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; +import com.google.common.base.Supplier; + /** * * @author Adrian Cole @@ -36,7 +38,7 @@ public class PlacementGroupHandler extends @Inject @Region - String defaultRegion; + Supplier defaultRegion; private String name; private String strategy = "cluster"; @@ -45,7 +47,7 @@ public class PlacementGroupHandler extends public PlacementGroup getResult() { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); PlacementGroup returnVal = new PlacementGroup(region, name, strategy, state); this.name = null; this.strategy = "cluster"; diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/ReservedInstancesOfferingHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/ReservedInstancesOfferingHandler.java index 56d70069e1..fcf4378d12 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/ReservedInstancesOfferingHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/ReservedInstancesOfferingHandler.java @@ -25,6 +25,8 @@ import org.jclouds.ec2.domain.ReservedInstancesOffering; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; +import com.google.common.base.Supplier; + /** * * @see { @Inject @Region - String defaultRegion; + Supplier defaultRegion; private StringBuilder currentText = new StringBuilder(); @@ -50,7 +52,7 @@ public class ReservedInstancesOfferingHandler extends public ReservedInstancesOffering getResult() { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region == null) - region = defaultRegion; + region = defaultRegion.get(); ReservedInstancesOffering returnVal = new ReservedInstancesOffering(region, availabilityZone, duration, fixedPrice, instanceType, productDescription, id, usagePrice); diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotHandler.java index 84f2c04551..950ccafe0e 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotHandler.java @@ -26,6 +26,8 @@ import org.jclouds.date.DateService; import org.jclouds.http.functions.ParseSax; import org.jclouds.location.Region; +import com.google.common.base.Supplier; + /** * * @author Adrian Cole @@ -34,10 +36,10 @@ public class SpotHandler extends ParseSax.HandlerForGeneratedRequestWithResult defaultRegion; @Inject - public SpotHandler(DateService dateService, @Region String defaultRegion) { + public SpotHandler(DateService dateService, @Region Supplier defaultRegion) { this.dateService = dateService; this.defaultRegion = defaultRegion; } @@ -48,7 +50,7 @@ public class SpotHandler extends ParseSax.HandlerForGeneratedRequestWithResult defaultRegion; protected final Builder builder; protected boolean inLaunchSpecification; protected final LaunchSpecificationHandler launchSpecificationHandler; @@ -47,7 +50,7 @@ public class SpotInstanceHandler extends ParseSax.HandlerForGeneratedRequestWith protected final TagSetHandler tagSetHandler; @Inject - public SpotInstanceHandler(DateService dateService, @Region String defaultRegion, + public SpotInstanceHandler(DateService dateService, @Region Supplier defaultRegion, LaunchSpecificationHandler launchSpecificationHandler, TagSetHandler tagSetHandler, SpotInstanceRequest.Builder builder) { this.dateService = dateService; @@ -61,7 +64,7 @@ public class SpotInstanceHandler extends ParseSax.HandlerForGeneratedRequestWith try { String region = getRequest() != null ? AWSUtils.findRegionInArgsOrNull(getRequest()) : null; if (region == null) - region = defaultRegion; + region = defaultRegion.get(); return builder.region(region).build(); } finally { builder.clear(); diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java index ddea167a92..ea0b7fcb70 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java @@ -323,6 +323,8 @@ public class AWSEC2TemplateBuilderLiveTest extends EC2TemplateBuilderLiveTest { new Log4JLoggingModule(), TrackingJavaUrlHttpCommandExecutorService.newTrackingModule(commandsInvoked)), overrides); + assert context.getComputeService().listAssignableLocations().size() < this.context.getComputeService().listAssignableLocations().size(); + assertOnlyOneRegionQueriedForAvailabilityZone(commandsInvoked); assert context.getComputeService().listImages().size() < this.context.getComputeService().listImages().size(); diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java index 2be19c7070..9d9cf1394c 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java @@ -18,10 +18,10 @@ */ package org.jclouds.aws.ec2.compute.strategy; +import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; -import static org.easymock.classextension.EasyMock.createMock; -import static org.easymock.classextension.EasyMock.replay; -import static org.easymock.classextension.EasyMock.verify; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; import static org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions.Builder.keyPair; import static org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest.CREDENTIALS; import static org.jclouds.ec2.compute.strategy.CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest.KEYPAIR; diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/AWSAMIClientLiveTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/AWSAMIClientLiveTest.java index 630b2d5ba3..689f0f44c5 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/AWSAMIClientLiveTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/AWSAMIClientLiveTest.java @@ -36,7 +36,6 @@ import org.jclouds.ec2.domain.Image; import org.jclouds.ec2.domain.RootDeviceType; import org.jclouds.ec2.domain.Image.ImageType; import org.jclouds.logging.log4j.config.Log4JLoggingModule; -import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.RestContext; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeGroups; diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/BaseAWSEC2AsyncClientTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/BaseAWSEC2AsyncClientTest.java index 556c630b36..50295fe64b 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/BaseAWSEC2AsyncClientTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/services/BaseAWSEC2AsyncClientTest.java @@ -18,12 +18,14 @@ */ package org.jclouds.aws.ec2.services; +import static com.google.common.collect.Maps.transformValues; import static org.testng.Assert.assertEquals; import java.io.IOException; import java.net.URI; import java.util.Map; import java.util.Properties; +import java.util.Set; import javax.inject.Singleton; @@ -35,17 +37,23 @@ import org.jclouds.date.DateService; import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.http.HttpRequest; import org.jclouds.http.RequiresHttp; +import org.jclouds.location.config.LocationModule; +import org.jclouds.location.suppliers.RegionIdToURISupplier; +import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier; import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.RestClientTest; import org.jclouds.rest.RestContextFactory; import org.jclouds.rest.RestContextSpec; +import org.jclouds.util.Suppliers2; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; -import com.google.common.cache.LoadingCache; +import com.google.common.base.Supplier; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.inject.Module; import com.google.inject.Provides; @@ -64,13 +72,9 @@ public abstract class BaseAWSEC2AsyncClientTest extends RestClientTest { return "2009-11-08T15:54:08.897Z"; } - protected void bindRegionsToProvider() { - bindRegionsToProvider(Regions.class); - } - @Provides @Singleton - LoadingCache provide(){ + LoadingCache provide() { return CacheBuilder.newBuilder().build(new CacheLoader() { @Override @@ -80,24 +84,30 @@ public abstract class BaseAWSEC2AsyncClientTest extends RestClientTest { }); } - static class Regions implements javax.inject.Provider> { - @Override - public Map get() { - return ImmutableMap. 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")); - } - } - protected void bindZonesToProvider() { - bindZonesToProvider(Zones.class); - } + @Override + protected void installLocations() { + install(new LocationModule()); + bind(RegionIdToURISupplier.class).toInstance(new RegionIdToURISupplier() { - static class Zones implements javax.inject.Provider> { - @Override - public Map get() { - return ImmutableMap. of("us-east-1a", "us-east-1"); - } + @Override + public Map> get() { + return transformValues(ImmutableMap. 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")), Suppliers2. ofInstanceFunction()); + } + + }); + bind(RegionIdToZoneIdsSupplier.class).toInstance(new RegionIdToZoneIdsSupplier() { + + @Override + public Map>> get() { + return transformValues(ImmutableMap.> of("us-east-1", ImmutableSet.of("us-east-1a", + "us-east-1b", "us-east-1c", "us-east-1b")), Suppliers2.> ofInstanceFunction()); + } + + }); } } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java index 9af948b628..de8bf6a7e3 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSDescribeInstancesResponseHandlerTest.java @@ -42,11 +42,14 @@ import org.jclouds.location.Region; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code AWSDescribeInstancesResponseHandler} @@ -243,7 +246,7 @@ public class AWSDescribeInstancesResponseHandlerTest extends BaseEC2HandlerTest @Override protected void configure() { - bind(String.class).annotatedWith(Region.class).toInstance("us-east-1"); + bind(new TypeLiteral>(){}).annotatedWith(Region.class).toInstance(Suppliers.ofInstance("us-east-1")); bind(RunningInstance.Builder.class).to(AWSRunningInstance.Builder.class); } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java index bd9783e3c5..16d591d9ce 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/AWSRunInstancesResponseHandlerTest.java @@ -1,5 +1,5 @@ /** - * Licensed to jclouds, Inc. (jclouds) under one or more +s * 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 @@ -18,9 +18,9 @@ */ package org.jclouds.aws.ec2.xml; +import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; -import static org.easymock.classextension.EasyMock.createMock; -import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -41,10 +41,13 @@ import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code RunInstancesResponseHandler} @@ -65,7 +68,7 @@ public class AWSRunInstancesResponseHandlerTest extends BaseEC2HandlerTest { @Override protected void configure() { - bind(String.class).annotatedWith(Region.class).toInstance("us-east-1"); + bind(new TypeLiteral>(){}).annotatedWith(Region.class).toInstance(Suppliers.ofInstance("us-east-1")); bind(RunningInstance.Builder.class).to(AWSRunningInstance.Builder.class); } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeSpotPriceHistoryResponseHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeSpotPriceHistoryResponseHandlerTest.java index 5cea44c9fc..905d423e94 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeSpotPriceHistoryResponseHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/DescribeSpotPriceHistoryResponseHandlerTest.java @@ -29,8 +29,11 @@ import org.jclouds.http.functions.BaseHandlerTest; import org.jclouds.location.Region; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import com.google.inject.AbstractModule; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code DescribeSpotPriceHistoryResponseHandler} @@ -58,7 +61,7 @@ public class DescribeSpotPriceHistoryResponseHandlerTest extends BaseHandlerTest @Override protected void configure() { - bindConstant().annotatedWith(Region.class).to("us-west-1"); + bind(new TypeLiteral>(){}).annotatedWith(Region.class).toInstance(Suppliers.ofInstance("us-west-1")); } }).getInstance(DescribeSpotPriceHistoryResponseHandler.class)).parse(is); diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java index 9dc607bbab..8f15313392 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstanceHandlerTest.java @@ -18,9 +18,9 @@ */ package org.jclouds.aws.ec2.xml; +import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; -import static org.easymock.classextension.EasyMock.createMock; -import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -37,9 +37,12 @@ import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code SpotInstanceHandler} @@ -60,7 +63,7 @@ public class SpotInstanceHandlerTest extends BaseEC2HandlerTest { @Override protected void configure() { - bind(String.class).annotatedWith(Region.class).toInstance("us-east-1"); + bind(new TypeLiteral>(){}).annotatedWith(Region.class).toInstance(Suppliers.ofInstance("us-east-1")); } }); diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java index 9477abfbf4..5db3fcdb45 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java @@ -18,9 +18,9 @@ */ package org.jclouds.aws.ec2.xml; +import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; -import static org.easymock.classextension.EasyMock.createMock; -import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.EasyMock.replay; import static org.testng.Assert.assertEquals; import java.io.InputStream; @@ -38,11 +38,14 @@ import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code SpotInstancesHandler} @@ -63,7 +66,7 @@ public class SpotInstancesHandlerTest extends BaseEC2HandlerTest { @Override protected void configure() { - bind(String.class).annotatedWith(Region.class).toInstance("us-east-1"); + bind(new TypeLiteral>(){}).annotatedWith(Region.class).toInstance(Suppliers.ofInstance("us-east-1")); } }); diff --git a/providers/aws-s3/src/main/java/org/jclouds/aws/s3/config/AWSS3RestClientModule.java b/providers/aws-s3/src/main/java/org/jclouds/aws/s3/config/AWSS3RestClientModule.java index ccaf4b097b..a56a0f2c6f 100644 --- a/providers/aws-s3/src/main/java/org/jclouds/aws/s3/config/AWSS3RestClientModule.java +++ b/providers/aws-s3/src/main/java/org/jclouds/aws/s3/config/AWSS3RestClientModule.java @@ -40,6 +40,8 @@ import org.jclouds.s3.S3Client; import org.jclouds.s3.binders.BindAsHostPrefixIfConfigured; import org.jclouds.s3.config.S3RestClientModule; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.inject.Provides; /** @@ -51,16 +53,9 @@ import com.google.inject.Provides; @ConfiguresRestClient public class AWSS3RestClientModule extends S3RestClientModule { - @Provides - @Singleton - @Bucket - protected URI provideBucketURI(@Named(PROPERTY_REGION + "." + US_STANDARD + "." + ENDPOINT) String endpoint) { - return URI.create(endpoint); - } - @Override - protected String defaultRegionForBucket(@Region String defaultRegion) { - return US_STANDARD; + protected Supplier defaultRegionForBucket(@Region Supplier defaultRegion) { + return Suppliers.ofInstance(US_STANDARD); } @Override @@ -68,7 +63,14 @@ public class AWSS3RestClientModule extends S3RestClientModule provideBucketURI(@Named(PROPERTY_REGION + "." + US_STANDARD + "." + ENDPOINT) String endpoint){ + return Suppliers.ofInstance(URI.create(endpoint)); + } + public AWSS3RestClientModule() { super(AWSS3Client.class, AWSS3AsyncClient.class); } diff --git a/providers/aws-s3/src/test/java/org/jclouds/aws/s3/AWSS3AsyncClientTest.java b/providers/aws-s3/src/test/java/org/jclouds/aws/s3/AWSS3AsyncClientTest.java index d102c254f5..da375f3532 100644 --- a/providers/aws-s3/src/test/java/org/jclouds/aws/s3/AWSS3AsyncClientTest.java +++ b/providers/aws-s3/src/test/java/org/jclouds/aws/s3/AWSS3AsyncClientTest.java @@ -20,7 +20,6 @@ package org.jclouds.aws.s3; import java.io.IOException; import java.lang.reflect.Method; -import java.net.URI; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentMap; @@ -111,16 +110,16 @@ public class AWSS3AsyncClientTest extends org.jclouds.s3.S3AsyncClientTestEU", "text/xml", false); @@ -273,11 +272,6 @@ public class AWSS3AsyncClientTest extends org.jclouds.s3.S3AsyncClientTest cache) { return "2009-11-08T15:54:08.897Z"; diff --git a/providers/aws-s3/src/test/java/org/jclouds/aws/s3/binders/AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest.java b/providers/aws-s3/src/test/java/org/jclouds/aws/s3/binders/AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest.java index 68a5b1c055..03441f200b 100644 --- a/providers/aws-s3/src/test/java/org/jclouds/aws/s3/binders/AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest.java +++ b/providers/aws-s3/src/test/java/org/jclouds/aws/s3/binders/AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest.java @@ -21,6 +21,7 @@ package org.jclouds.aws.s3.binders; import static org.testng.Assert.assertEquals; import java.net.URI; +import java.util.Map; import javax.inject.Provider; import javax.ws.rs.core.UriBuilder; @@ -30,12 +31,13 @@ import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull; import org.jclouds.rest.binders.BindAsHostPrefix; import org.testng.annotations.Test; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableMap; import com.sun.jersey.api.uri.UriBuilderImpl; /** - * Tests behavior of - * {@code AssignCorrectHostnameAndBindAsHostPrefixIfConfigured} + * Tests behavior of {@code AssignCorrectHostnameAndBindAsHostPrefixIfConfigured} * * @author Adrian Cole */ @@ -56,11 +58,14 @@ public class AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest { 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. of()); + new BindAsHostPrefix(uriBuilderProvider), new RegionToEndpointOrProviderIfNull("aws-s3", Suppliers + .ofInstance(URI.create("https://s3.amazonaws.com")), + + Suppliers.>> 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"))))), + + uriBuilderProvider, ImmutableMap. of()); request = binder.bindToRequest(request, "bucket"); assertEquals(request.getRequestLine(), "GET https://bucket.s3.amazonaws.com HTTP/1.1"); @@ -71,11 +76,14 @@ public class AssignCorrectHostnameAndBindAsHostPrefixIfConfiguredTest { 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. of("bucket", "us-west-1")); + new BindAsHostPrefix(uriBuilderProvider), new RegionToEndpointOrProviderIfNull("aws-s3", Suppliers + .ofInstance(URI.create("https://s3.amazonaws.com")), + + Suppliers.>> 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"))))), + + uriBuilderProvider, ImmutableMap. 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");