JCLOUDS-774 ec2 api was ignoring the jclouds.region property, which made exceptions possible on all zone-scoped api calls.

This commit is contained in:
Adrian Cole 2014-11-16 14:17:16 -08:00 committed by Adrian Cole
parent 95141ff877
commit 4b813ce096
3 changed files with 94 additions and 21 deletions

View File

@ -18,33 +18,40 @@ package org.jclouds.ec2.suppliers;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.ec2.EC2Api; import org.jclouds.ec2.EC2Api;
import org.jclouds.ec2.features.AvailabilityZoneAndRegionApi;
import org.jclouds.location.Region;
import org.jclouds.location.suppliers.RegionIdToURISupplier; import org.jclouds.location.suppliers.RegionIdToURISupplier;
import org.jclouds.util.Suppliers2; import org.jclouds.location.suppliers.fromconfig.RegionIdsFromConfiguration;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.Maps; import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
@Singleton /**
public class DescribeRegionsForRegionURIs implements RegionIdToURISupplier { * Uses the {@code DescribeRegions} call to return the regions endpoints, subject to any whitelist present in the
private final AvailabilityZoneAndRegionApi client; * property {@link org.jclouds.location.reference.LocationConstants#PROPERTY_REGIONS}.
*/
public final class DescribeRegionsForRegionURIs implements RegionIdToURISupplier {
private final EC2Api api;
private final Set<String> whitelistedRegionIds;
@Inject @Inject DescribeRegionsForRegionURIs(EC2Api api, RegionIdsFromConfiguration regionIdsFromConfiguration) {
public DescribeRegionsForRegionURIs(EC2Api client) { this.api = api;
this.client = client.getAvailabilityZoneAndRegionApi().get(); this.whitelistedRegionIds = regionIdsFromConfiguration.get();
} }
@Singleton
@Region
@Override @Override
public Map<String, Supplier<URI>> get() { public Map<String, Supplier<URI>> get() {
Map<String, URI> regionToUris = client.describeRegions(); ImmutableMap.Builder<String, Supplier<URI>> result = ImmutableMap.builder();
return Maps.transformValues(regionToUris, Suppliers2.<URI> ofInstanceFunction()); for (Entry<String, URI> regionUrl : api.getAvailabilityZoneAndRegionApi().get().describeRegions().entrySet()) {
if (whitelistedRegionIds.isEmpty() || whitelistedRegionIds.contains(regionUrl.getKey())) {
result.put(regionUrl.getKey(), Suppliers.ofInstance(regionUrl.getValue()));
}
}
return result.build();
} }
} }

View File

@ -58,18 +58,20 @@ public class BaseEC2ApiMockTest {
private Map<String, MockWebServer> regionToServers = Maps.newLinkedHashMap(); private Map<String, MockWebServer> regionToServers = Maps.newLinkedHashMap();
protected EC2Api api() { protected EC2Api api() {
return builder().buildApi(EC2Api.class); return builder(new Properties()).buildApi(EC2Api.class);
} }
protected ContextBuilder builder() { protected ContextBuilder builder(Properties overrides) {
Properties overrides = new Properties();
overrides.setProperty(Constants.PROPERTY_MAX_RETRIES, "1"); overrides.setProperty(Constants.PROPERTY_MAX_RETRIES, "1");
return ContextBuilder.newBuilder(new EC2ApiMetadata()).credentials(ACCESS_KEY, SECRET_KEY) return ContextBuilder.newBuilder(new EC2ApiMetadata())
.endpoint("http://localhost:" + regionToServers.get(DEFAULT_REGION).getPort()).overrides(overrides) .credentials(ACCESS_KEY, SECRET_KEY)
.endpoint("http://localhost:" + regionToServers.get(DEFAULT_REGION).getPort())
.overrides(overrides)
.modules(modules); .modules(modules);
} }
private final Set<Module> modules = ImmutableSet.<Module>of(new ExecutorServiceModule(sameThreadExecutor())); private final Set<Module> modules = ImmutableSet.<Module>of(
new ExecutorServiceModule(sameThreadExecutor(), sameThreadExecutor()));
@BeforeMethod @BeforeMethod
public void start() throws IOException { public void start() throws IOException {

View File

@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import java.net.URI;
import java.util.Map;
import java.util.Properties;
import org.jclouds.ec2.internal.BaseEC2ApiMockTest;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
@Test(groups = "unit", testName = "DescribeRegionsForRegionURIsMockTest", singleThreaded = true)
public class DescribeRegionsForRegionURIsMockTest extends BaseEC2ApiMockTest {
public void buildsUrlsForEachRegion() throws Exception {
enqueueRegions("us-east-1", "eu-central-1");
Map<String, Supplier<URI>> result = supplier(new Properties()).get();
assertEquals(result.size(), 2);
assertNotNull(result.get("us-east-1").get());
assertNotNull(result.get("eu-central-1").get());
assertPosted("us-east-1", "Action=DescribeRegions");
}
public void honorsRegionWhitelist() throws Exception {
enqueueRegions("us-east-1", "eu-central-1");
Properties overrides = new Properties();
overrides.setProperty(PROPERTY_REGIONS, "us-east-1");
Map<String, Supplier<URI>> result = supplier(overrides).get();
assertEquals(result.size(), 1);
assertNotNull(result.get("us-east-1").get());
assertPosted("us-east-1", "Action=DescribeRegions");
}
private DescribeRegionsForRegionURIs supplier(Properties overrides) {
return builder(overrides).buildInjector().getInstance(DescribeRegionsForRegionURIs.class);
}
}