Issue 550: Added support to find providers "collocated" with a given provider.

[in core/src/main/java/org/jclouds]

* providers/ProviderPredicates.java
  (intersectingIso3166Code, providerContainsIso3166Code): Added.

* providers/Providers.java
  (collocatedWith): Added.

[in core/src/test/java/org/jclouds]

* providers/JcloudsTestYetAnotherComputeProviderMetadata.java: Added.

* providers/ProvidersTest.java
  (testIntersectingIso3166Code): Added.

[in core/src/test/resources]

* META-INF/services/org.jclouds.providers.ProviderMetadata: Updated for the
   new test ProviderMetadata implementation.
This commit is contained in:
Jeremy Whitlock 2011-05-26 09:47:04 -06:00
parent cbf14a5397
commit d320641734
5 changed files with 231 additions and 28 deletions

View File

@ -109,31 +109,84 @@ public class ProviderPredicates {
Preconditions.checkNotNull(iso3166Code, "iso3166Code must not be null"); Preconditions.checkNotNull(iso3166Code, "iso3166Code must not be null");
return new Predicate<ProviderMetadata>() { return new Predicate<ProviderMetadata>() {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean apply(ProviderMetadata providerMetadata) { public boolean apply(ProviderMetadata providerMetadata) {
for (String availCode : providerMetadata.getIso3166Codes()) { return providerContainsIso3166Code(providerMetadata, iso3166Code);
if(iso3166Code.indexOf('-') == -1) { }
if (availCode.startsWith(iso3166Code + "-")) {
return true;
}
} else if (availCode.equals(iso3166Code)) {
return true;
}
}
return false; /**
} * {@inheritDoc}
*/
/** @Override
* {@inheritDoc} public String toString() {
*/ return "inIso3166Code(" + iso3166Code + ")";
@Override }
public String toString() {
return "iso3166Code(" + iso3166Code + ")";
}
}; };
} }
/**
* Return all providers that have at least one ISO 3166 code in common with the given provider metadata.
*
* @param refProviderMetadata
* the provider metadata to use to filter providers by
*
* @return the providers that have at least one ISO 3166 code in common
*/
public static Predicate<ProviderMetadata> intersectingIso3166Code(final ProviderMetadata refProviderMetadata) {
Preconditions.checkNotNull(refProviderMetadata, "refProviderMetadata must not be null");
return new Predicate<ProviderMetadata>() {
/**
* {@inheritDoc}
*/
@Override
public boolean apply(ProviderMetadata providerMetadata) {
for (String refIso3166Code : refProviderMetadata.getIso3166Codes()) {
// Return only if the potential provider contains the same ISO 3166 code and the provider and
// reference provider are not the same.
if (providerContainsIso3166Code(providerMetadata, refIso3166Code) &&
!refProviderMetadata.equals(providerMetadata)) {
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "intersectingIso3166Code(" + refProviderMetadata + ")";
}
};
}
/**
* Returns whether or not the provided provider contains the ISO 3166 code provider or is within the same
* "global" region, like "US" would contain "US-*".
*
* @param providerMetadata
* the provider metadata to search
* @param iso3166Code
* the ISO 3166 code to search the provider metadata for
*
* @return the result
*/
private static boolean providerContainsIso3166Code(ProviderMetadata providerMetadata, String iso3166Code) {
for (String availCode : providerMetadata.getIso3166Codes()) {
if(iso3166Code.indexOf('-') == -1) {
if (availCode.startsWith(iso3166Code + "-")) {
return true;
}
} else if (availCode.equals(iso3166Code)) {
return true;
}
}
return false;
}
} }

View File

@ -135,6 +135,19 @@ public class Providers {
* @return the providers with the given ISO 3166 code * @return the providers with the given ISO 3166 code
*/ */
public static Iterable<ProviderMetadata> withIso3166Code(String iso3166Code) { public static Iterable<ProviderMetadata> withIso3166Code(String iso3166Code) {
return filter(all(), ProviderPredicates.inIso3166Code(iso3166Code)); return filter(all(), ProviderPredicates.inIso3166Code(iso3166Code));
}
/**
* Returns the providers that have at least one common ISO 3166 code in common
* regardless of type.
*
* @param providerMetadata
* the provider metadata to use to filter providers by
*
* @return the providers that share at least one common ISO 3166 code
*/
public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata) {
return filter(all(), ProviderPredicates.intersectingIso3166Code(providerMetadata));
} }
} }

View File

@ -0,0 +1,105 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.providers;
import com.google.common.collect.ImmutableSet;
import java.net.URI;
import java.util.Set;
/**
* Implementation of @ link org.jclouds.types.ProviderMetadata} for testing.
*
* @author Jeremy Whitlock <jwhitlock@apache.org>
*/
public class JcloudsTestYetAnotherComputeProviderMetadata extends BaseProviderMetadata {
/**
* {@ see org.jclouds.types.ProviderMetadata#getId()}
*/
@Override
public String getId() {
return "test-yet-another-compute-provider";
}
/**
* {@ see org.jclouds.types.ProviderMetadata#getType()}
*/
@Override
public String getType() {
return ProviderMetadata.COMPUTE_TYPE;
}
/**
* {@ see org.jclouds.types.ProviderMetadata#getName()}
*/
@Override
public String getName() {
return "Test Yet Another Compute Provider";
}
/**
* {@inheritDoc}
*/
@Override
public String getCredentialName() {
return "user";
}
/**
* {@inheritDoc}
*/
@Override
public String getIdentityName() {
return "password";
}
/**
* {@ see org.jclouds.types.ProviderMetadata#getHomepage()}
*/
@Override
public URI getHomepage() {
return URI.create("http://jclouds.org");
}
/**
* {@ see org.jclouds.types.ProviderMetadata#getConsole()}
*/
@Override
public URI getConsole() {
return URI.create("http://jclouds.org/console");
}
/**
* {@inheritDoc}
*/
@Override
public URI getApiDocumentation() {
return URI.create("http://jclouds.org/documentation");
}
/**
* {@inheritDoc}
*/
@Override
public Set<String> getIso3166Codes() {
return ImmutableSet.of("JP-13");
}
}

View File

@ -37,6 +37,7 @@ public class ProvidersTest {
private final ProviderMetadata testBlobstoreProvider = new JcloudsTestBlobStoreProviderMetadata(); private final ProviderMetadata testBlobstoreProvider = new JcloudsTestBlobStoreProviderMetadata();
private final ProviderMetadata testComputeProvider = new JcloudsTestComputeProviderMetadata(); private final ProviderMetadata testComputeProvider = new JcloudsTestComputeProviderMetadata();
private final ProviderMetadata testYetAnotherComputeProvider = new JcloudsTestYetAnotherComputeProviderMetadata();
@Test @Test
public void testWithId() { public void testWithId() {
@ -66,7 +67,11 @@ public class ProvidersTest {
providersMetadata = Providers.ofType(ProviderMetadata.COMPUTE_TYPE); providersMetadata = Providers.ofType(ProviderMetadata.COMPUTE_TYPE);
for (ProviderMetadata providerMetadata : providersMetadata) { for (ProviderMetadata providerMetadata : providersMetadata) {
assertEquals(testComputeProvider, providerMetadata); if (providerMetadata.getName().equals(testComputeProvider.getName())) {
assertEquals(testComputeProvider, providerMetadata);
} else {
assertEquals(testYetAnotherComputeProvider, providerMetadata);
}
} }
providersMetadata = Providers.ofType("fake-type"); providersMetadata = Providers.ofType("fake-type");
@ -79,10 +84,12 @@ public class ProvidersTest {
Iterable<ProviderMetadata> providersMetadata = Providers.all(); Iterable<ProviderMetadata> providersMetadata = Providers.all();
for (ProviderMetadata providerMetadata : providersMetadata) { for (ProviderMetadata providerMetadata : providersMetadata) {
if (providerMetadata.getName().equals("Test Blobstore Provider")) { if (providerMetadata.getName().equals(testBlobstoreProvider.getName())) {
assertEquals(testBlobstoreProvider, providerMetadata); assertEquals(testBlobstoreProvider, providerMetadata);
} else { } else if (providerMetadata.getName().equals(testComputeProvider.getName())){
assertEquals(testComputeProvider, providerMetadata); assertEquals(testComputeProvider, providerMetadata);
} else {
assertEquals(testYetAnotherComputeProvider, providerMetadata);
} }
} }
} }
@ -94,6 +101,8 @@ public class ProvidersTest {
put("US-CA", 2); put("US-CA", 2);
put("US-FL", 1); put("US-FL", 1);
put("US", 2); put("US", 2);
put("JP-13", 1);
put("JP", 1);
put("SOME-FAKE-CODE", 0); put("SOME-FAKE-CODE", 0);
}}; }};
@ -111,4 +120,26 @@ public class ProvidersTest {
} }
} }
@Test
public void testIntersectingIso3166Code() {
@SuppressWarnings("serial")
Map<ProviderMetadata, Integer> expectedResults = new HashMap<ProviderMetadata, Integer>() {{
put(testBlobstoreProvider, 1);
put(testComputeProvider, 1);
put(testYetAnotherComputeProvider, 0);
}};
for (Map.Entry<ProviderMetadata, Integer> result : expectedResults.entrySet()) {
Iterable<ProviderMetadata> providersMetadata = Providers.collocatedWith(result.getKey());
int providersFound = 0;
for (ProviderMetadata providerMetadata : providersMetadata) {
if (providerMetadata != null) {
providersFound++;
}
}
assertEquals(providersFound, result.getValue().intValue());
}
}
} }

View File

@ -1,2 +1,3 @@
org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata org.jclouds.providers.JcloudsTestBlobStoreProviderMetadata
org.jclouds.providers.JcloudsTestComputeProviderMetadata org.jclouds.providers.JcloudsTestComputeProviderMetadata
org.jclouds.providers.JcloudsTestYetAnotherComputeProviderMetadata