JCLOUDS-774 cleanup derived location suppliers and backfill test for inconsistent region and zone mappings.

This commit is contained in:
Adrian Cole 2014-11-16 10:27:50 -08:00
parent c18f3b3537
commit b59d7fd792
5 changed files with 110 additions and 54 deletions

View File

@ -16,39 +16,35 @@
*/ */
package org.jclouds.location.suppliers.derived; package org.jclouds.location.suppliers.derived;
import static com.google.common.base.Throwables.propagate;
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.inject.Singleton;
import org.jclouds.location.Region; import org.jclouds.location.Region;
import org.jclouds.location.suppliers.RegionIdsSupplier; import org.jclouds.location.suppliers.RegionIdsSupplier;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.inject.Inject; import com.google.inject.Inject;
/** /** As opposed to via properties, lets look up regions via api, as they are more likely to change. */
* as opposed to via properties, lets look up regions via api, as they are more likely to change public final class RegionIdsFromRegionIdToURIKeySet implements RegionIdsSupplier {
*/
@Singleton
public class RegionIdsFromRegionIdToURIKeySet implements RegionIdsSupplier {
private final Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier; private final Supplier<Map<String, Supplier<URI>>> regionIdToURIs;
@Inject @Inject
protected RegionIdsFromRegionIdToURIKeySet(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier) { RegionIdsFromRegionIdToURIKeySet(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURIs) {
this.regionIdToURISupplier = regionIdToURISupplier; this.regionIdToURIs = regionIdToURIs;
} }
@Override @Override
public Set<String> get() { public Set<String> get() {
try { try {
return regionIdToURISupplier.get().keySet(); return regionIdToURIs.get().keySet();
} catch (UndeclaredThrowableException e) { } catch (UndeclaredThrowableException e) {
throw Throwables.propagate(e.getCause()); throw propagate(e.getCause());
} }
} }
} }

View File

@ -16,13 +16,14 @@
*/ */
package org.jclouds.location.suppliers.derived; package org.jclouds.location.suppliers.derived;
import static com.google.common.base.Preconditions.checkState;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.location.Region; import org.jclouds.location.Region;
import org.jclouds.location.Zone; import org.jclouds.location.Zone;
@ -32,28 +33,30 @@ import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
@Singleton public final class ZoneIdToURIFromJoinOnRegionIdToURI implements ZoneIdToURISupplier {
public class ZoneIdToURIFromJoinOnRegionIdToURI implements ZoneIdToURISupplier {
private final Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier; private final Supplier<Map<String, Supplier<URI>>> regionIdToURIs;
private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier; private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds;
@Inject @Inject
protected ZoneIdToURIFromJoinOnRegionIdToURI(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURISupplier, ZoneIdToURIFromJoinOnRegionIdToURI(@Region Supplier<Map<String, Supplier<URI>>> regionIdToURIs,
@Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier) { @Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds) {
this.regionIdToURISupplier = regionIdToURISupplier; this.regionIdToURIs = regionIdToURIs;
this.regionIdToZoneIdsSupplier = regionIdToZoneIdsSupplier; this.regionIdToZoneIds = regionIdToZoneIds;
} }
@Override @Override
public Map<String, Supplier<URI>> get() { public Map<String, Supplier<URI>> get() {
Builder<String, Supplier<URI>> builder = ImmutableMap.<String, Supplier<URI>> builder(); Map<String, Supplier<Set<String>>> regionIdToZoneIds = this.regionIdToZoneIds.get();
for (Entry<String, Supplier<URI>> regionToURI : regionIdToURISupplier.get().entrySet()) { Builder<String, Supplier<URI>> builder = ImmutableMap.builder();
for (String zone : regionIdToZoneIdsSupplier.get().get(regionToURI.getKey()).get()) { for (Entry<String, Supplier<URI>> regionToURI : regionIdToURIs.get().entrySet()) {
Supplier<Set<String>> zoneIds = regionIdToZoneIds.get(regionToURI.getKey());
checkState(zoneIds != null, "region %s is not in the configured region to zone mappings: %s",
regionToURI.getKey(), regionIdToZoneIds);
for (String zone : zoneIds.get()) {
builder.put(zone, regionToURI.getValue()); builder.put(zone, regionToURI.getValue());
} }
} }
return builder.build(); return builder.build();
} }
} }

View File

@ -16,40 +16,33 @@
*/ */
package org.jclouds.location.suppliers.derived; package org.jclouds.location.suppliers.derived;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.location.Zone; import org.jclouds.location.Zone;
import org.jclouds.location.suppliers.ZoneIdsSupplier; import org.jclouds.location.suppliers.ZoneIdsSupplier;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
/** /** As opposed to via properties, lets look up zones via api, as they are more likely to change. */
* as opposed to via properties, lets look up zones via api, as they are more likely to change public final class ZoneIdsFromRegionIdToZoneIdsValues implements ZoneIdsSupplier {
*/
@Singleton
public class ZoneIdsFromRegionIdToZoneIdsValues implements ZoneIdsSupplier {
private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier; private final Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds;
@Inject @Inject
protected ZoneIdsFromRegionIdToZoneIdsValues( ZoneIdsFromRegionIdToZoneIdsValues(@Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIds) {
@Zone Supplier<Map<String, Supplier<Set<String>>>> regionIdToZoneIdsSupplier) { this.regionIdToZoneIds = regionIdToZoneIds;
this.regionIdToZoneIdsSupplier = regionIdToZoneIdsSupplier;
} }
@Override @Override
public Set<String> get() { public Set<String> get() {
Collection<Supplier<Set<String>>> zones = regionIdToZoneIdsSupplier.get().values(); ImmutableSet.Builder<String> result = ImmutableSet.builder();
return ImmutableSet.copyOf(Iterables.concat(Iterables.transform(zones, Suppliers for (Supplier<Set<String>> zone : regionIdToZoneIds.get().values()) {
.<Set<String>> supplierFunction()))); result.addAll(zone.get());
}
return result.build();
} }
} }

View File

@ -20,29 +20,24 @@ import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.inject.Singleton;
import org.jclouds.location.Zone; import org.jclouds.location.Zone;
import org.jclouds.location.suppliers.ZoneIdsSupplier; import org.jclouds.location.suppliers.ZoneIdsSupplier;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.inject.Inject; import com.google.inject.Inject;
/** /** As opposed to via properties, lets look up zones via api, as they are more likely to change. */
* as opposed to via properties, lets look up zones via api, as they are more likely to change public final class ZoneIdsFromZoneIdToURIKeySet implements ZoneIdsSupplier {
*/
@Singleton
public class ZoneIdsFromZoneIdToURIKeySet implements ZoneIdsSupplier {
private final Supplier<Map<String, Supplier<URI>>> zoneIdToURISupplier; private final Supplier<Map<String, Supplier<URI>>> zoneIdToURIs;
@Inject @Inject
protected ZoneIdsFromZoneIdToURIKeySet(@Zone Supplier<Map<String, Supplier<URI>>> zoneIdToURISupplier) { ZoneIdsFromZoneIdToURIKeySet(@Zone Supplier<Map<String, Supplier<URI>>> zoneIdToURIs) {
this.zoneIdToURISupplier = zoneIdToURISupplier; this.zoneIdToURIs = zoneIdToURIs;
} }
@Override @Override
public Set<String> get() { public Set<String> get() {
return zoneIdToURISupplier.get().keySet(); return zoneIdToURIs.get().keySet();
} }
} }

View File

@ -0,0 +1,69 @@
/*
* 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.location.suppliers.derived;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Map;
import java.util.Set;
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.common.collect.Maps;
@Test
public class ZoneIdToURIFromJoinOnRegionIdToURITest {
public void zoneToRegionMappingsValid() {
Map<String, Supplier<URI>> regionIdToURIs = Maps.newLinkedHashMap();
regionIdToURIs.put("us-east-1", Suppliers.ofInstance(URI.create("ec2.us-east-1.amazonaws.com")));
regionIdToURIs.put("eu-central-1", Suppliers.ofInstance(URI.create("ec2.eu-central-1.amazonaws.com")));
Map<String, Supplier<Set<String>>> regionIdToZoneIds = Maps.newLinkedHashMap();
regionIdToZoneIds.put("us-east-1", supplyZoneIds("us-east-1a", "us-east-1b"));
regionIdToZoneIds.put("eu-central-1", supplyZoneIds("eu-central-1a"));
Map<String, Supplier<URI>> result = new ZoneIdToURIFromJoinOnRegionIdToURI(Suppliers.ofInstance(regionIdToURIs),
Suppliers.ofInstance(regionIdToZoneIds)).get();
assertEquals(result.size(), 3);
assertEquals(result.get("us-east-1a"), regionIdToURIs.get("us-east-1"));
assertEquals(result.get("us-east-1b"), regionIdToURIs.get("us-east-1"));
assertEquals(result.get("eu-central-1a"), regionIdToURIs.get("eu-central-1"));
}
@Test(expectedExceptions = IllegalStateException.class,
expectedExceptionsMessageRegExp = "region eu-central-1 is not in the configured region to zone mappings: .*")
public void zoneToRegionMappingsInconsistentOnKeys() {
Map<String, Supplier<URI>> regionIdToURIs = Maps.newLinkedHashMap();
regionIdToURIs.put("us-east-1", Suppliers.ofInstance(URI.create("ec2.us-east-1.amazonaws.com")));
regionIdToURIs.put("eu-central-1", Suppliers.ofInstance(URI.create("ec2.eu-central-1.amazonaws.com")));
Map<String, Supplier<Set<String>>> regionIdToZoneIds = Maps.newLinkedHashMap();
regionIdToZoneIds.put("us-east-1", supplyZoneIds("us-east-1a", "us-east-1b"));
// missing regionIdToZoneIds mapping for eu-central-1
new ZoneIdToURIFromJoinOnRegionIdToURI(Suppliers.ofInstance(regionIdToURIs),
Suppliers.ofInstance(regionIdToZoneIds)).get();
}
private static Supplier<Set<String>> supplyZoneIds(String... zoneIds) {
return Suppliers.<Set<String>>ofInstance(ImmutableSet.copyOf(zoneIds));
}
}