change to choose the first ec2 region when a match cannot be found

This commit is contained in:
Adrian Cole 2010-10-24 19:28:28 -05:00
parent ed6c6e2605
commit f5729cfb4e
3 changed files with 150 additions and 82 deletions

View File

@ -19,12 +19,16 @@
package org.jclouds.aws.config;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Maps.newLinkedHashMap;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_REGIONS;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.inject.Singleton;
@ -39,14 +43,13 @@ 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.logging.Logger.LoggerFactory;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.annotations.Provider;
import org.jclouds.rest.config.RestClientModule;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
@ -61,8 +64,7 @@ import com.google.inject.name.Names;
@RequiresHttp
public class AWSRestClientModule<S, A> extends RestClientModule<S, A> {
public AWSRestClientModule(Class<S> syncClientType, Class<A> asyncClientType,
Map<Class<?>, Class<?>> delegates) {
public AWSRestClientModule(Class<S> syncClientType, Class<A> asyncClientType, Map<Class<?>, Class<?>> delegates) {
super(syncClientType, asyncClientType, delegates);
}
@ -74,12 +76,13 @@ public class AWSRestClientModule<S, A> extends RestClientModule<S, A> {
@Singleton
@Region
protected Map<String, URI> provideRegions(Injector injector) {
String regionString = injector.getInstance(Key.get(String.class, Names
.named(PROPERTY_REGIONS)));
Map<String, URI> regions = Maps.newLinkedHashMap();
String regionString = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGIONS)));
Map<String, URI> regions = newLinkedHashMap();
for (String region : Splitter.on(',').split(regionString)) {
regions.put(region, URI.create(injector.getInstance(Key.get(String.class, Names
.named(Constants.PROPERTY_ENDPOINT + "." + region)))));
regions.put(
region,
URI.create(injector.getInstance(Key.get(String.class,
Names.named(Constants.PROPERTY_ENDPOINT + "." + region)))));
}
return regions;
}
@ -94,33 +97,35 @@ public class AWSRestClientModule<S, A> extends RestClientModule<S, A> {
@Provides
@Singleton
@Region
protected String getDefaultRegion(@Provider final URI uri, @Region Map<String, URI> map) {
return Iterables.find(map.entrySet(), new Predicate<Entry<String, URI>>() {
protected String getDefaultRegion(@Provider final URI uri, @Region Map<String, URI> map, LoggerFactory logFactory) {
try {
return find(map.entrySet(), new Predicate<Entry<String, URI>>() {
@Override
public boolean apply(Entry<String, URI> input) {
return input.getValue().equals(uri);
}
@Override
public boolean apply(Entry<String, URI> input) {
return input.getValue().equals(uri);
}
}).getKey();
}).getKey();
} catch (NoSuchElementException e) {
String region = get(map.keySet(), 0);
logFactory.getLogger("jclouds.compute").warn(
"failed to find region for current endpoint %s in %s; choosing first: %s", uri, map, region);
return region;
}
}
@Override
protected void bindErrorHandlers() {
bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(
ParseAWSErrorFromXmlContent.class);
bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(
ParseAWSErrorFromXmlContent.class);
bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(
ParseAWSErrorFromXmlContent.class);
bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(ParseAWSErrorFromXmlContent.class);
bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(ParseAWSErrorFromXmlContent.class);
bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(ParseAWSErrorFromXmlContent.class);
}
@Override
protected void bindRetryHandlers() {
bind(HttpRetryHandler.class).annotatedWith(Redirection.class).to(
AWSRedirectionRetryHandler.class);
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(
AWSClientErrorRetryHandler.class);
bind(HttpRetryHandler.class).annotatedWith(Redirection.class).to(AWSRedirectionRetryHandler.class);
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(AWSClientErrorRetryHandler.class);
}
}

View File

@ -0,0 +1,63 @@
/**
*
* Copyright (C) 2010 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.aws.config;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Map;
import org.jclouds.aws.ec2.EC2AsyncClient;
import org.jclouds.aws.ec2.EC2Client;
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" }, testName = "aws.AWSRestClientModuleTest")
public class AWSRestClientModuleTest {
@Test
public void testDefaultRegionWhenThereIsAMatch() {
AWSRestClientModule<EC2Client, EC2AsyncClient> module = new AWSRestClientModule<EC2Client, EC2AsyncClient>(
EC2Client.class, EC2AsyncClient.class);
URI currentEndpoint = URI.create("http://region1");
Map<String, URI> map = ImmutableMap.of("region1", currentEndpoint, "region2", URI.create("http://region2"));
assertEquals("region1", module.getDefaultRegion(currentEndpoint, map, new JDKLogger.JDKLoggerFactory()));
}
@Test
public void testDefaultRegionWhenThereIsNoMatch() {
AWSRestClientModule<EC2Client, EC2AsyncClient> module = new AWSRestClientModule<EC2Client, EC2AsyncClient>(
EC2Client.class, EC2AsyncClient.class);
URI currentEndpoint = URI.create("http://region3");
Map<String, URI> map = ImmutableMap.of("region1", currentEndpoint, "region2", URI.create("http://region2"));
assertEquals("region1", module.getDefaultRegion(currentEndpoint, map, new JDKLogger.JDKLoggerFactory()));
}
}

View File

@ -31,74 +31,74 @@ import org.jclouds.logging.Logger;
*
*/
public class JDKLogger extends BaseLogger {
private final java.util.logging.Logger logger;
private final java.util.logging.Logger logger;
public static class JDKLoggerFactory implements LoggerFactory {
public Logger getLogger(String category) {
return new JDKLogger(java.util.logging.Logger.getLogger(category));
}
}
public static class JDKLoggerFactory implements LoggerFactory {
public Logger getLogger(String category) {
return new JDKLogger(java.util.logging.Logger.getLogger(category));
}
}
public JDKLogger(java.util.logging.Logger logger) {
this.logger = logger;
}
public JDKLogger(java.util.logging.Logger logger) {
this.logger = logger;
}
@Override
protected void logTrace(String message) {
logger.finest(message);
}
@Override
protected void logTrace(String message) {
logger.finest(message);
}
public boolean isTraceEnabled() {
return logger.isLoggable(Level.FINEST);
}
public boolean isTraceEnabled() {
return logger.isLoggable(Level.FINEST);
}
@Override
protected void logDebug(String message) {
logger.fine(message);
}
@Override
protected void logDebug(String message) {
logger.fine(message);
}
public boolean isDebugEnabled() {
return logger.isLoggable(Level.FINE);
}
public boolean isDebugEnabled() {
return logger.isLoggable(Level.FINE);
}
@Override
protected void logInfo(String message) {
logger.info(message);
}
@Override
protected void logInfo(String message) {
logger.info(message);
}
public boolean isInfoEnabled() {
return logger.isLoggable(Level.INFO);
}
public boolean isInfoEnabled() {
return logger.isLoggable(Level.INFO);
}
@Override
protected void logWarn(String message) {
logger.warning(message);
}
@Override
protected void logWarn(String message) {
logger.warning(message);
}
@Override
protected void logWarn(String message, Throwable e) {
logger.log(Level.WARNING, message, e);
}
@Override
protected void logWarn(String message, Throwable e) {
logger.log(Level.WARNING, message, e);
}
public boolean isWarnEnabled() {
return logger.isLoggable(Level.WARNING);
}
public boolean isWarnEnabled() {
return logger.isLoggable(Level.WARNING);
}
@Override
protected void logError(String message) {
logger.severe(message);
}
@Override
protected void logError(String message) {
logger.severe(message);
}
@Override
protected void logError(String message, Throwable e) {
logger.log(Level.SEVERE, message, e);
}
@Override
protected void logError(String message, Throwable e) {
logger.log(Level.SEVERE, message, e);
}
public boolean isErrorEnabled() {
return logger.isLoggable(Level.SEVERE);
}
public boolean isErrorEnabled() {
return logger.isLoggable(Level.SEVERE);
}
public String getCategory() {
return logger.getName();
}
public String getCategory() {
return logger.getName();
}
}