added strict suppliers of scoped locations

This commit is contained in:
Adrian Cole 2012-07-02 15:22:10 -07:00
parent 547c73ab00
commit ef9a69e7c3
8 changed files with 575 additions and 0 deletions

View File

@ -0,0 +1,77 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers.all;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.location.Iso3166;
import org.jclouds.location.Region;
import org.jclouds.location.suppliers.LocationsSupplier;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
@Singleton
public class RegionToProvider implements LocationsSupplier {
private final JustProvider justProvider;
private final Supplier<Set<String>> regionsSupplier;
private final Supplier<Map<String, Supplier<Set<String>>>> isoCodesByIdSupplier;
@Inject
public RegionToProvider(JustProvider justProvider, @Region Supplier<Set<String>> regionsSupplier,
@Iso3166 Supplier<Map<String, Supplier<Set<String>>>> isoCodesByIdSupplier) {
this.justProvider = checkNotNull(justProvider, "justProvider");
this.regionsSupplier = checkNotNull(regionsSupplier, "regionsSupplier");
this.isoCodesByIdSupplier = checkNotNull(isoCodesByIdSupplier, "isoCodesByIdSupplier");
}
@Override
public Set<? extends Location> get() {
Builder<Location> locations = ImmutableSet.builder();
Location provider = Iterables.getOnlyElement(justProvider.get());
Set<String> regions = regionsSupplier.get();
checkState(regions.size() > 0, "no regions found for provider %s, using supplier %s", provider, regionsSupplier);
Map<String, Supplier<Set<String>>> isoCodesById = isoCodesByIdSupplier.get();
for (String region : regions) {
LocationBuilder builder = new LocationBuilder().scope(LocationScope.REGION).id(region).description(region)
.parent(provider);
if (isoCodesById.containsKey(region))
builder.iso3166Codes(isoCodesById.get(region).get());
locations.add(builder.build());
}
return locations.build();
}
}

View File

@ -0,0 +1,84 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers.all;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.jclouds.location.Iso3166;
import org.jclouds.location.Zone;
import org.jclouds.location.suppliers.LocationsSupplier;
import org.jclouds.logging.Logger;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
@Singleton
public class ZoneToProvider implements LocationsSupplier {
@Resource
protected Logger logger = Logger.NULL;
private final JustProvider justProvider;
private final Supplier<Set<String>> zoneIdsSupplier;
private final Supplier<Map<String, Supplier<Set<String>>>> isoCodesByIdSupplier;
@Inject
ZoneToProvider(JustProvider justProvider, @Zone Supplier<Set<String>> zoneIdsSupplier,
@Iso3166 Supplier<Map<String, Supplier<Set<String>>>> isoCodesByIdSupplier) {
this.justProvider = checkNotNull(justProvider, "justProvider");
this.zoneIdsSupplier = checkNotNull(zoneIdsSupplier, "zoneIdsSupplier");
this.isoCodesByIdSupplier = checkNotNull(isoCodesByIdSupplier, "isoCodesByIdSupplier");
}
@Override
public Set<? extends Location> get() {
Location provider = Iterables.getOnlyElement(justProvider.get());
Set<String> zoneIds = zoneIdsSupplier.get();
checkState(zoneIds.size() > 0, "no zones found for provider %s, using supplier %s", provider, zoneIdsSupplier);
Map<String, Supplier<Set<String>>> isoCodesById = isoCodesByIdSupplier.get();
Builder<Location> locations = ImmutableSet.builder();
for (String zoneId : zoneIds) {
LocationBuilder builder = new LocationBuilder().scope(LocationScope.ZONE).id(zoneId).description(zoneId)
.parent(provider);
if (isoCodesById.containsKey(zoneId))
builder.iso3166Codes(isoCodesById.get(zoneId).get());
locations.add(builder.build());
}
return locations.build();
}
}

View File

@ -0,0 +1,65 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers.implicit;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.location.predicates.LocationPredicates.isRegion;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.collect.Memoized;
import org.jclouds.domain.Location;
import org.jclouds.location.functions.ToIdAndScope;
import org.jclouds.location.suppliers.ImplicitLocationSupplier;
import com.google.common.base.Supplier;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class FirstRegion implements ImplicitLocationSupplier {
private final Supplier<Set<? extends Location>> locationsSupplier;
@Inject
FirstRegion(@Memoized Supplier<Set<? extends Location>> locationsSupplier) {
this.locationsSupplier = checkNotNull(locationsSupplier, "locationsSupplierSupplier");
}
@Override
public Location get() {
Set<? extends Location> locations = locationsSupplier.get();
try {
return find(locations, isRegion());
} catch (NoSuchElementException e) {
throw new NoSuchElementException("none to of the locations are scope REGION: "
+ transform(locations, ToIdAndScope.INSTANCE));
}
}
}

View File

@ -0,0 +1,65 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers.implicit;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.location.predicates.LocationPredicates.isZone;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.collect.Memoized;
import org.jclouds.domain.Location;
import org.jclouds.location.functions.ToIdAndScope;
import org.jclouds.location.suppliers.ImplicitLocationSupplier;
import com.google.common.base.Supplier;
/**
*
* @author Adrian Cole
*
*/
@Singleton
public class FirstZone implements ImplicitLocationSupplier {
private final Supplier<Set<? extends Location>> locationsSupplier;
@Inject
FirstZone(@Memoized Supplier<Set<? extends Location>> locationsSupplier) {
this.locationsSupplier = checkNotNull(locationsSupplier, "locationsSupplierSupplier");
}
@Override
public Location get() {
Set<? extends Location> locations = locationsSupplier.get();
try {
return find(locations, isZone());
} catch (NoSuchElementException e) {
throw new NoSuchElementException("none to of the locations are scope ZONE: "
+ transform(locations, ToIdAndScope.INSTANCE));
}
}
}

View File

@ -0,0 +1,72 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers.all;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "RegionToProviderTest")
public class RegionToProviderTest {
Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("servo").description("http://servo")
.iso3166Codes(ImmutableSet.of("US")).build();
JustProvider justProvider = new JustProvider("servo", Suppliers.ofInstance(URI.create("http://servo")), ImmutableSet.of("US"));
@Test
public void test() {
Supplier<Set<String>> regionIdsSupplier = Suppliers.<Set<String>>ofInstance(ImmutableSet.of("region1", "region2"));
Supplier<Map<String, Supplier<Set<String>>>> locationToIsoCodes = Suppliers.<Map<String, Supplier<Set<String>>>>ofInstance(
ImmutableMap.of(
"servo", Suppliers.<Set<String>>ofInstance(ImmutableSet.of("US")),
"region1", Suppliers.<Set<String>>ofInstance(ImmutableSet.of("US-CA")),
"region2", Suppliers.<Set<String>>ofInstance(ImmutableSet.of("US-VA"))
));
RegionToProvider fn = new RegionToProvider(justProvider, regionIdsSupplier, locationToIsoCodes);
assertEquals(fn.get(), ImmutableSet.of(
new LocationBuilder().scope(LocationScope.REGION).id("region1").description("region1").iso3166Codes(ImmutableSet.of("US-CA")).parent(provider).build(),
new LocationBuilder().scope(LocationScope.REGION).id("region2").description("region2").iso3166Codes(ImmutableSet.of("US-VA")).parent(provider).build()
));
}
@Test(expectedExceptions = IllegalStateException.class)
public void testWhenNoRegions() {
Supplier<Set<String>> regionIdsSupplier = Suppliers.<Set<String>>ofInstance(ImmutableSet.<String>of());
Supplier<Map<String, Supplier<Set<String>>>> locationToIsoCodes = Suppliers.<Map<String, Supplier<Set<String>>>>ofInstance(
ImmutableMap.<String, Supplier<Set<String>>>of());
RegionToProvider fn = new RegionToProvider(justProvider, regionIdsSupplier, locationToIsoCodes);
fn.get();
}
}

View File

@ -0,0 +1,72 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.location.suppliers.all;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "ZoneToProviderTest")
public class ZoneToProviderTest {
Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("servo").description("http://servo")
.iso3166Codes(ImmutableSet.of("US")).build();
JustProvider justProvider = new JustProvider("servo", Suppliers.ofInstance(URI.create("http://servo")), ImmutableSet.of("US"));
@Test
public void test() {
Supplier<Set<String>> zoneIdsSupplier = Suppliers.<Set<String>>ofInstance(ImmutableSet.of("zone1", "zone2"));
Supplier<Map<String, Supplier<Set<String>>>> locationToIsoCodes = Suppliers.<Map<String, Supplier<Set<String>>>>ofInstance(
ImmutableMap.of(
"servo", Suppliers.<Set<String>>ofInstance(ImmutableSet.of("US")),
"zone1", Suppliers.<Set<String>>ofInstance(ImmutableSet.of("US-CA")),
"zone2", Suppliers.<Set<String>>ofInstance(ImmutableSet.of("US-VA"))
));
ZoneToProvider fn = new ZoneToProvider(justProvider, zoneIdsSupplier, locationToIsoCodes);
assertEquals(fn.get(), ImmutableSet.of(
new LocationBuilder().scope(LocationScope.ZONE).id("zone1").description("zone1").iso3166Codes(ImmutableSet.of("US-CA")).parent(provider).build(),
new LocationBuilder().scope(LocationScope.ZONE).id("zone2").description("zone2").iso3166Codes(ImmutableSet.of("US-VA")).parent(provider).build()
));
}
@Test(expectedExceptions = IllegalStateException.class)
public void testWhenNoZones() {
Supplier<Set<String>> zoneIdsSupplier = Suppliers.<Set<String>>ofInstance(ImmutableSet.<String>of());
Supplier<Map<String, Supplier<Set<String>>>> locationToIsoCodes = Suppliers.<Map<String, Supplier<Set<String>>>>ofInstance(
ImmutableMap.<String, Supplier<Set<String>>>of());
ZoneToProvider fn = new ZoneToProvider(justProvider, zoneIdsSupplier, locationToIsoCodes);
fn.get();
}
}

View File

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

View File

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