From 9bac52d315212e93484f9bf98bd891a2cf50a21d Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 2 Jul 2010 12:05:41 -0700 Subject: [PATCH] Issue 298: fixed regression on authorizationexception not propagating, and also missing gogrid file --- .../blobstore/BlobStoreContextFactory.java | 63 +++-- .../compute/ComputeServiceContextFactory.java | 65 +++-- .../StubComputeServiceContextModule.java | 2 +- .../compute/util/ComputeServiceUtils.java | 5 +- .../compute/BaseComputeServiceLiveTest.java | 246 ++++++++---------- .../StubComputeServiceIntegrationTest.java | 159 ++++++----- .../org/jclouds/rest/RestContextFactory.java | 75 +++--- .../src/main/java/org/jclouds/util/Utils.java | 43 ++- .../test/java/org/jclouds/util/UtilsTest.java | 2 +- .../functions/ReturnEmptySetOnNotFound.java | 47 ++++ 10 files changed, 397 insertions(+), 310 deletions(-) create mode 100644 gogrid/src/main/java/org/jclouds/gogrid/functions/ReturnEmptySetOnNotFound.java diff --git a/blobstore/src/main/java/org/jclouds/blobstore/BlobStoreContextFactory.java b/blobstore/src/main/java/org/jclouds/blobstore/BlobStoreContextFactory.java index 09bfd280e9..e8a358f42a 100644 --- a/blobstore/src/main/java/org/jclouds/blobstore/BlobStoreContextFactory.java +++ b/blobstore/src/main/java/org/jclouds/blobstore/BlobStoreContextFactory.java @@ -19,6 +19,7 @@ package org.jclouds.blobstore; import static org.jclouds.rest.RestContextFactory.createContextBuilder; +import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException; import java.util.Properties; @@ -63,21 +64,31 @@ public class BlobStoreContextFactory { this.contextFactory = restContextFactory; } + public static BlobStoreContext buildContextUnwrappingExceptions( + BlobStoreContextBuilder builder) { + try { + return builder.buildBlobStoreContext(); + } catch (Exception e) { + return propagateAuthorizationOrOriginalException(e); + } + } + /** * @see RestContextFactory#createContextBuilder(String, String, String) */ public BlobStoreContext createContext(String provider, String identity, String credential) { - return BlobStoreContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, identity, credential)) - .buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class.cast(contextFactory + .createContextBuilder(provider, identity, credential)); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(String, Properties) */ public BlobStoreContext createContext(String provider, Properties overrides) { - return BlobStoreContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, overrides)).buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class.cast(contextFactory + .createContextBuilder(provider, overrides)); + return buildContextUnwrappingExceptions(builder); } /** @@ -85,9 +96,10 @@ public class BlobStoreContextFactory { */ public BlobStoreContext createContext(String provider, Iterable modules, Properties overrides) { - return BlobStoreContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, modules, overrides)) - .buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class.cast(contextFactory + .createContextBuilder(provider, modules, overrides)); + return buildContextUnwrappingExceptions(builder); + } /** @@ -95,27 +107,29 @@ public class BlobStoreContextFactory { */ public BlobStoreContext createContext(String provider, @Nullable String identity, @Nullable String credential, Iterable modules) { - return BlobStoreContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, identity, credential, modules)) - .buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class.cast(contextFactory + .createContextBuilder(provider, identity, credential, modules)); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties) */ - public BlobStoreContext createContext(String providerName, @Nullable String identity, + public BlobStoreContext createContext(String provider, @Nullable String identity, @Nullable String credential, Iterable modules, Properties overrides) { - return BlobStoreContextBuilder.class.cast( - contextFactory.createContextBuilder(providerName, identity, credential, modules, - overrides)).buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class.cast(contextFactory + .createContextBuilder(provider, identity, credential, modules, overrides)); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(ContextSpec) */ public BlobStoreContext createContext(ContextSpec contextSpec) { - return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec)) - .buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class + .cast(createContextBuilder(contextSpec)); + return buildContextUnwrappingExceptions(builder); + } /** @@ -123,16 +137,18 @@ public class BlobStoreContextFactory { */ public BlobStoreContext createContext(ContextSpec contextSpec, Iterable modules) { - return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec, modules)) - .buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class + .cast(createContextBuilder(contextSpec, modules)); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(ContextSpec, Properties) */ public BlobStoreContext createContext(ContextSpec contextSpec, Properties overrides) { - return BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec, overrides)) - .buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class + .cast(createContextBuilder(contextSpec, overrides)); + return buildContextUnwrappingExceptions(builder); } /** @@ -140,7 +156,8 @@ public class BlobStoreContextFactory { */ public BlobStoreContext createContext(ContextSpec contextSpec, Iterable modules, Properties overrides) { - return BlobStoreContextBuilder.class.cast( - createContextBuilder(contextSpec, modules, overrides)).buildBlobStoreContext(); + BlobStoreContextBuilder builder = BlobStoreContextBuilder.class + .cast(createContextBuilder(contextSpec, modules, overrides)); + return buildContextUnwrappingExceptions(builder); } } \ No newline at end of file diff --git a/compute/src/main/java/org/jclouds/compute/ComputeServiceContextFactory.java b/compute/src/main/java/org/jclouds/compute/ComputeServiceContextFactory.java index ddb8eea10d..504978f0ed 100644 --- a/compute/src/main/java/org/jclouds/compute/ComputeServiceContextFactory.java +++ b/compute/src/main/java/org/jclouds/compute/ComputeServiceContextFactory.java @@ -19,6 +19,7 @@ package org.jclouds.compute; import static org.jclouds.rest.RestContextFactory.createContextBuilder; +import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException; import java.util.Properties; @@ -63,22 +64,31 @@ public class ComputeServiceContextFactory { this.contextFactory = restContextFactory; } + public static ComputeServiceContext buildContextUnwrappingExceptions( + ComputeServiceContextBuilder builder) { + try { + return builder.buildComputeServiceContext(); + } catch (Exception e) { + return propagateAuthorizationOrOriginalException(e); + } + } + /** * @see RestContextFactory#createContextBuilder(String, String, String) */ public ComputeServiceContext createContext(String provider, String identity, String credential) { - return ComputeServiceContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, identity, credential)) - .buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(contextFactory.createContextBuilder(provider, identity, credential)); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(String, Properties) */ public ComputeServiceContext createContext(String provider, Properties overrides) { - return ComputeServiceContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, overrides)) - .buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(contextFactory.createContextBuilder(provider, overrides)); + return buildContextUnwrappingExceptions(builder); } /** @@ -86,9 +96,10 @@ public class ComputeServiceContextFactory { */ public ComputeServiceContext createContext(String provider, Iterable modules, Properties overrides) { - return ComputeServiceContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, modules, overrides)) - .buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(contextFactory.createContextBuilder(provider, modules, overrides)); + return buildContextUnwrappingExceptions(builder); + } /** @@ -96,27 +107,30 @@ public class ComputeServiceContextFactory { */ public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential, Iterable modules) { - return ComputeServiceContextBuilder.class.cast( - contextFactory.createContextBuilder(provider, identity, credential, modules)) - .buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(contextFactory.createContextBuilder(provider, identity, credential, modules)); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties) */ - public ComputeServiceContext createContext(String providerName, @Nullable String identity, + public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential, Iterable modules, Properties overrides) { - return ComputeServiceContextBuilder.class.cast( - contextFactory.createContextBuilder(providerName, identity, credential, modules, - overrides)).buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(contextFactory.createContextBuilder(provider, identity, credential, modules, + overrides)); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(ContextSpec) */ public ComputeServiceContext createContext(ContextSpec contextSpec) { - return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec)) - .buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(createContextBuilder(contextSpec)); + return buildContextUnwrappingExceptions(builder); + } /** @@ -124,8 +138,9 @@ public class ComputeServiceContextFactory { */ public ComputeServiceContext createContext(ContextSpec contextSpec, Iterable modules) { - return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec, modules)) - .buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(createContextBuilder(contextSpec, modules)); + return buildContextUnwrappingExceptions(builder); } /** @@ -133,8 +148,9 @@ public class ComputeServiceContextFactory { */ public ComputeServiceContext createContext(ContextSpec contextSpec, Properties overrides) { - return ComputeServiceContextBuilder.class.cast(createContextBuilder(contextSpec, overrides)) - .buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(createContextBuilder(contextSpec, overrides)); + return buildContextUnwrappingExceptions(builder); } /** @@ -142,7 +158,8 @@ public class ComputeServiceContextFactory { */ public ComputeServiceContext createContext(ContextSpec contextSpec, Iterable modules, Properties overrides) { - return ComputeServiceContextBuilder.class.cast( - createContextBuilder(contextSpec, modules, overrides)).buildComputeServiceContext(); + ComputeServiceContextBuilder builder = ComputeServiceContextBuilder.class + .cast(createContextBuilder(contextSpec, modules, overrides)); + return buildContextUnwrappingExceptions(builder); } } \ No newline at end of file diff --git a/compute/src/main/java/org/jclouds/compute/stub/config/StubComputeServiceContextModule.java b/compute/src/main/java/org/jclouds/compute/stub/config/StubComputeServiceContextModule.java index 069cc24f73..0ea1b81688 100644 --- a/compute/src/main/java/org/jclouds/compute/stub/config/StubComputeServiceContextModule.java +++ b/compute/src/main/java/org/jclouds/compute/stub/config/StubComputeServiceContextModule.java @@ -398,7 +398,7 @@ public class StubComputeServiceContextModule extends AbstractModule { @Singleton Location getLocation(@org.jclouds.rest.annotations.Provider String providerName) { Location provider = new LocationImpl(LocationScope.PROVIDER, providerName, providerName, null); - return new LocationImpl(LocationScope.ZONE, "memory", "memory", provider); + return new LocationImpl(LocationScope.ZONE, providerName, providerName, provider); } @Provides diff --git a/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java b/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java index 536aa9ede5..e47ba41cd5 100644 --- a/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java +++ b/compute/src/main/java/org/jclouds/compute/util/ComputeServiceUtils.java @@ -18,14 +18,12 @@ */ package org.jclouds.compute.util; - import java.util.Formatter; import java.util.Map; import java.util.NoSuchElementException; import java.util.Map.Entry; import java.util.concurrent.Callable; - import org.jclouds.compute.ComputeServiceContextBuilder; import org.jclouds.compute.domain.Architecture; import org.jclouds.compute.domain.ComputeMetadata; @@ -145,7 +143,8 @@ public class ComputeServiceUtils { public static boolean isKeyAuth(NodeMetadata createdNode) { return createdNode.getCredentials().credential != null - && createdNode.getCredentials().credential.startsWith("-----BEGIN RSA PRIVATE KEY-----"); + && createdNode.getCredentials().credential + .startsWith("-----BEGIN RSA PRIVATE KEY-----"); } /** diff --git a/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java b/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java index cf1becc9a2..5c1f0fe85f 100755 --- a/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java +++ b/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java @@ -99,8 +99,8 @@ public abstract class BaseComputeServiceLiveTest { protected Map keyPair; @BeforeGroups(groups = { "integration", "live" }) - public void setupClient() throws InterruptedException, ExecutionException, - TimeoutException, IOException { + public void setupClient() throws InterruptedException, ExecutionException, TimeoutException, + IOException { if (tag == null) tag = checkNotNull(provider, "provider"); setupCredentials(); @@ -110,33 +110,29 @@ public abstract class BaseComputeServiceLiveTest { Injector injector = createSshClientInjector(); sshFactory = injector.getInstance(SshClient.Factory.class); SocketOpen socketOpen = injector.getInstance(SocketOpen.class); - socketTester = new RetryablePredicate(socketOpen, 60, 1, - TimeUnit.SECONDS); + socketTester = new RetryablePredicate(socketOpen, 60, 1, TimeUnit.SECONDS); injector.injectMembers(socketOpen); // add logger } protected void setupKeyPair() throws FileNotFoundException, IOException { String secretKeyFile; try { - secretKeyFile = checkNotNull(System - .getProperty("jclouds.test.ssh.keyfile"), - "jclouds.test.ssh.keyfile"); + secretKeyFile = checkNotNull(System.getProperty("jclouds.test.ssh.keyfile"), + "jclouds.test.ssh.keyfile"); } catch (NullPointerException e) { secretKeyFile = System.getProperty("user.home") + "/.ssh/id_rsa"; } checkSecretKeyFile(secretKeyFile); String secret = Files.toString(new File(secretKeyFile), Charsets.UTF_8); - assert secret.startsWith("-----BEGIN RSA PRIVATE KEY-----") : "invalid key:\n" - + secret; - keyPair = ImmutableMap. of("private", secret, "public", - Files.toString(new File(secretKeyFile + ".pub"), Charsets.UTF_8)); + assert secret.startsWith("-----BEGIN RSA PRIVATE KEY-----") : "invalid key:\n" + secret; + keyPair = ImmutableMap. of("private", secret, "public", Files.toString( + new File(secretKeyFile + ".pub"), Charsets.UTF_8)); } protected void setupCredentials() { - identity = checkNotNull(System.getProperty("jclouds.test.identity"), - "jclouds.test.identity"); + identity = checkNotNull(System.getProperty("jclouds.test.identity"), "jclouds.test.identity"); credential = checkNotNull(System.getProperty("jclouds.test.credential"), - "jclouds.test.credential"); + "jclouds.test.credential"); } protected Injector createSshClientInjector() { @@ -146,20 +142,16 @@ public abstract class BaseComputeServiceLiveTest { private void initializeContextAndClient() throws IOException { if (context != null) context.close(); - context = new ComputeServiceContextFactory() - .createContext(provider, identity, credential, ImmutableSet.of( - new Log4JLoggingModule(), getSshModule())); + context = new ComputeServiceContextFactory().createContext(provider, identity, credential, + ImmutableSet.of(new Log4JLoggingModule(), getSshModule())); client = context.getComputeService(); } - private void checkSecretKeyFile(String secretKeyFile) - throws FileNotFoundException { - Utils - .checkNotEmpty(secretKeyFile, - "System property: [jclouds.test.ssh.keyfile] set to an empty string"); + private void checkSecretKeyFile(String secretKeyFile) throws FileNotFoundException { + Utils.checkNotEmpty(secretKeyFile, + "System property: [jclouds.test.ssh.keyfile] set to an empty string"); if (!new File(secretKeyFile).exists()) { - throw new FileNotFoundException("secretKeyFile not found at: " - + secretKeyFile); + throw new FileNotFoundException("secretKeyFile not found at: " + secretKeyFile); } } @@ -169,10 +161,10 @@ public abstract class BaseComputeServiceLiveTest { @Test(enabled = true, expectedExceptions = AuthorizationException.class) public void testCorrectAuthException() throws Exception { new ComputeServiceContextFactory().createContext(provider, "MOMMA", "MIA", - ImmutableSet. of(new Log4JLoggingModule())).close(); + ImmutableSet. of(new Log4JLoggingModule())).close(); } - @Test(enabled = true) + @Test(enabled = true, dependsOnMethods = "testCorrectAuthException") public void testImagesCache() throws Exception { client.listImages(); long time = System.currentTimeMillis(); @@ -183,9 +175,8 @@ public abstract class BaseComputeServiceLiveTest { // since surefire and eclipse don't otherwise guarantee the order, we are // starting this one alphabetically before create2nodes.. - @Test(enabled = true, dependsOnMethods = "testImagesCache") - public void testAScriptExecutionAfterBootWithBasicTemplate() - throws Exception { + @Test(enabled = true, dependsOnMethods = { "testImagesCache" }) + public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception { String tag = this.tag + "run"; try { client.destroyNodesMatching(NodePredicates.withTag(tag)); @@ -195,21 +186,18 @@ public abstract class BaseComputeServiceLiveTest { TemplateOptions options = client.templateOptions().blockOnPort(22, 120); try { - Set nodes = client.runNodesWithTag(tag, 1, - options); + Set nodes = client.runNodesWithTag(tag, 1, options); Credentials good = nodes.iterator().next().getCredentials(); assert good.identity != null; assert good.credential != null; Image image = Iterables.get(nodes, 0).getImage(); try { - Map responses = runScriptWithCreds( - tag, image.getOsFamily(), new Credentials(good.identity, - "romeo")); + Map responses = runScriptWithCreds(tag, image + .getOsFamily(), new Credentials(good.identity, "romeo")); assert false : "shouldn't pass with a bad password\n" + responses; } catch (RunScriptOnNodesException e) { - assert Throwables.getRootCause(e).getMessage() - .contains("Auth fail") : e; + assert Throwables.getRootCause(e).getMessage().contains("Auth fail") : e; } runScriptWithCreds(tag, image.getOsFamily(), good); @@ -221,11 +209,10 @@ public abstract class BaseComputeServiceLiveTest { } } - @Test(enabled = true, dependsOnMethods = "testImagesCache") + @Test(enabled = true, dependsOnMethods = { "testImagesCache" }) public void testTemplateMatch() throws Exception { template = buildTemplate(client.templateBuilder()); - Template toMatch = client.templateBuilder().imageId( - template.getImage().getId()).build(); + Template toMatch = client.templateBuilder().imageId(template.getImage().getId()).build(); assertEquals(toMatch.getImage(), template.getImage()); } @@ -240,14 +227,14 @@ public abstract class BaseComputeServiceLiveTest { } template = buildTemplate(client.templateBuilder()); - template.getOptions().installPrivateKey(keyPair.get("private")) - .authorizePublicKey(keyPair.get("public")).runScript( - buildScript(template.getImage().getOsFamily()).getBytes()); + template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey( + keyPair.get("public")).runScript( + buildScript(template.getImage().getOsFamily()).getBytes()); try { nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 2, template)); } catch (RunNodesException e) { - nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e - .getNodeErrors().keySet())); + nodes = Sets.newTreeSet(Iterables.concat(e.getSuccessfulNodes(), e.getNodeErrors() + .keySet())); throw e; } assertEquals(nodes.size(), 2); @@ -274,11 +261,9 @@ public abstract class BaseComputeServiceLiveTest { } @Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript") - public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() - throws Exception { + public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception { initializeContextAndClient(); - TreeSet nodes = Sets.newTreeSet(client.runNodesWithTag(tag, - 1, template)); + TreeSet nodes = Sets.newTreeSet(client.runNodesWithTag(tag, 1, template)); checkNodes(nodes, tag); NodeMetadata node = nodes.first(); this.nodes.add(node); @@ -287,13 +272,11 @@ public abstract class BaseComputeServiceLiveTest { assertEquals(node.getImage(), template.getImage()); } - protected Map runScriptWithCreds( - final String tag, OsFamily osFamily, Credentials creds) - throws RunScriptOnNodesException { + protected Map runScriptWithCreds(final String tag, + OsFamily osFamily, Credentials creds) throws RunScriptOnNodesException { try { - return client.runScriptOnNodesMatching(NodePredicates - .runningWithTag(tag), buildScript(osFamily).getBytes(), - RunScriptOptions.Builder.overrideCredentialsWith(creds)); + return client.runScriptOnNodesMatching(NodePredicates.runningWithTag(tag), buildScript( + osFamily).getBytes(), RunScriptOptions.Builder.overrideCredentialsWith(creds)); } catch (SshException e) { if (Throwables.getRootCause(e).getMessage().contains("Auth fail")) { // System.err.printf("bad credentials: %s:%s for %s%n", @@ -304,15 +287,14 @@ public abstract class BaseComputeServiceLiveTest { } } - protected void checkNodes(Iterable nodes, String tag) - throws IOException { + protected void checkNodes(Iterable nodes, String tag) throws IOException { for (NodeMetadata node : nodes) { assertNotNull(node.getProviderId()); assertNotNull(node.getTag()); assertEquals(node.getTag(), tag); assertEquals(node.getState(), NodeState.RUNNING); - assert node.getPublicAddresses().size() >= 1 - || node.getPrivateAddresses().size() >= 1 : "no ips in" + node; + assert node.getPublicAddresses().size() >= 1 || node.getPrivateAddresses().size() >= 1 : "no ips in" + + node; assertNotNull(node.getCredentials()); if (node.getCredentials().identity != null) { assertNotNull(node.getCredentials().identity); @@ -328,55 +310,49 @@ public abstract class BaseComputeServiceLiveTest { protected String buildScript(OsFamily osFamily) { switch (osFamily) { - case UBUNTU: - return new StringBuilder()// - .append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")// - .append("cp /etc/apt/sources.list /etc/apt/sources.list.old\n")// - .append( - "sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list\n")// - .append("apt-get update\n")// - .append("apt-get install -f -y --force-yes openjdk-6-jdk\n")// - .append("wget -qO/usr/bin/runurl run.alestic.com/runurl\n")// - .append("chmod 755 /usr/bin/runurl\n")// - .toString(); - case CENTOS: - case RHEL: - return new StringBuilder() - .append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n") - .append( - "echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n") - .append( - "echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n") - .append( - "echo \"baseurl=http://ec2-us-east-mirror.rightscale.com/epel/5/i386/\" >> /etc/yum.repos.d/CentOS-Base.repo\n") - .append( - "echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n") - .append("yum --nogpgcheck -y install java-1.6.0-openjdk\n") - .append( - "echo \"export PATH=\\\"/usr/lib/jvm/jre-1.6.0-openjdk/bin/:\\$PATH\\\"\" >> /root/.bashrc\n") - .toString(); - default: - throw new IllegalArgumentException(osFamily.toString()); + case UBUNTU: + return new StringBuilder()// + .append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")// + .append("cp /etc/apt/sources.list /etc/apt/sources.list.old\n")// + .append( + "sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list\n")// + .append("apt-get update\n")// + .append("apt-get install -f -y --force-yes openjdk-6-jdk\n")// + .append("wget -qO/usr/bin/runurl run.alestic.com/runurl\n")// + .append("chmod 755 /usr/bin/runurl\n")// + .toString(); + case CENTOS: + case RHEL: + return new StringBuilder() + .append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n") + .append("echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n") + .append("echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n") + .append( + "echo \"baseurl=http://ec2-us-east-mirror.rightscale.com/epel/5/i386/\" >> /etc/yum.repos.d/CentOS-Base.repo\n") + .append("echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n") + .append("yum --nogpgcheck -y install java-1.6.0-openjdk\n") + .append( + "echo \"export PATH=\\\"/usr/lib/jvm/jre-1.6.0-openjdk/bin/:\\$PATH\\\"\" >> /root/.bashrc\n") + .toString(); + default: + throw new IllegalArgumentException(osFamily.toString()); } } @Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired") public void testGet() throws Exception { - Set metadataSet = Sets.newHashSet(Iterables - .filter(client.listNodesDetailsMatching(NodePredicates.all()), - Predicates.and(NodePredicates.withTag(tag), Predicates - .not(NodePredicates.TERMINATED)))); + Set metadataSet = Sets.newHashSet(Iterables.filter(client + .listNodesDetailsMatching(NodePredicates.all()), Predicates.and(NodePredicates + .withTag(tag), Predicates.not(NodePredicates.TERMINATED)))); for (NodeMetadata node : nodes) { metadataSet.remove(node); NodeMetadata metadata = client.getNodeMetadata(node.getId()); assertEquals(metadata.getProviderId(), node.getProviderId()); assertEquals(metadata.getTag(), node.getTag()); - assertLocationSameOrChild(metadata.getLocation(), template - .getLocation()); + assertLocationSameOrChild(metadata.getLocation(), template.getLocation()); assertEquals(metadata.getImage(), template.getImage()); assertEquals(metadata.getState(), NodeState.RUNNING); - assertEquals(metadata.getPrivateAddresses(), node - .getPrivateAddresses()); + assertEquals(metadata.getPrivateAddresses(), node.getPrivateAddresses()); assertEquals(metadata.getPublicAddresses(), node.getPublicAddresses()); } assertNodeZero(metadataSet); @@ -384,8 +360,7 @@ public abstract class BaseComputeServiceLiveTest { protected void assertNodeZero(Set metadataSet) { assert metadataSet.size() == 0 : String.format( - "nodes left in set: [%s] which didn't match set: [%s]", - metadataSet, nodes); + "nodes left in set: [%s] which didn't match set: [%s]", metadataSet, nodes); } @Test(enabled = true, dependsOnMethods = "testGet") @@ -400,7 +375,7 @@ public abstract class BaseComputeServiceLiveTest { TemplateOptions options = new TemplateOptions().withMetadata(); Template t = client.templateBuilder().smallest().options(options).build(); assert t.getOptions().isIncludeMetadata() : "The metadata option should be 'true' " - + "for the created template"; + + "for the created template"; } public void testListNodes() throws Exception { @@ -412,8 +387,7 @@ public abstract class BaseComputeServiceLiveTest { } public void testGetNodesWithDetails() throws Exception { - for (NodeMetadata node : client.listNodesDetailsMatching(NodePredicates - .all())) { + for (NodeMetadata node : client.listNodesDetailsMatching(NodePredicates.all())) { assert node.getProviderId() != null : node; assert node.getLocation() != null : node; assertEquals(node.getType(), ComputeType.NODE); @@ -427,7 +401,7 @@ public abstract class BaseComputeServiceLiveTest { if (nodeMetadata.getState() == NodeState.RUNNING) { assert nodeMetadata.getPublicAddresses() != null : nodeMetadata; assert nodeMetadata.getPublicAddresses().size() > 0 - || nodeMetadata.getPrivateAddresses().size() > 0 : nodeMetadata; + || nodeMetadata.getPrivateAddresses().size() > 0 : nodeMetadata; assertNotNull(nodeMetadata.getPrivateAddresses()); } } @@ -449,26 +423,26 @@ public abstract class BaseComputeServiceLiveTest { assert location != location.getParent() : location; assert location.getScope() != null : location; switch (location.getScope()) { - case PROVIDER: - assertProvider(location); - break; - case REGION: - assertProvider(location.getParent()); - break; - case ZONE: - Location provider = location.getParent().getParent(); - // zone can be a direct descendant of provider - if (provider == null) - provider = location.getParent(); - assertProvider(provider); - break; - case HOST: - Location provider2 = location.getParent().getParent().getParent(); - // zone can be a direct descendant of provider - if (provider2 == null) - provider2 = location.getParent().getParent(); - assertProvider(provider2); - break; + case PROVIDER: + assertProvider(location); + break; + case REGION: + assertProvider(location.getParent()); + break; + case ZONE: + Location provider = location.getParent().getParent(); + // zone can be a direct descendant of provider + if (provider == null) + provider = location.getParent(); + assertProvider(provider); + break; + case HOST: + Location provider2 = location.getParent().getParent().getParent(); + // zone can be a direct descendant of provider + if (provider2 == null) + provider2 = location.getParent().getParent(); + assertProvider(provider2); + break; } } } @@ -482,18 +456,14 @@ public abstract class BaseComputeServiceLiveTest { } // no inbound ports - TemplateOptions options = client.templateOptions().blockUntilRunning( - false).inboundPorts(); + TemplateOptions options = client.templateOptions().blockUntilRunning(false).inboundPorts(); try { long time = System.currentTimeMillis(); - Set nodes = client.runNodesWithTag(tag, 1, - options); + Set nodes = client.runNodesWithTag(tag, 1, options); NodeMetadata node = Iterables.getOnlyElement(nodes); - assertEquals(node.getState(), NodeState.PENDING); - + assert node.getState() != NodeState.RUNNING; long duration = System.currentTimeMillis() - time; - assert duration < 30 * 1000 : "duration longer than 30 seconds!: " - + duration / 1000; + assert duration < 30 * 1000 : "duration longer than 30 seconds!: " + duration / 1000; } finally { client.destroyNodesMatching(NodePredicates.withTag(tag)); } @@ -529,15 +499,13 @@ public abstract class BaseComputeServiceLiveTest { } } - protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) - throws IOException { - IPSocket socket = new IPSocket(Iterables - .get(node.getPublicAddresses(), 0), 22); + protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) throws IOException { + IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), 22); socketTester.apply(socket); // TODO add transitionTo option that accepts // a socket conection // state. - SshClient ssh = sshFactory.create(socket, node.getCredentials().identity, - keyPair.get("private").getBytes()); + SshClient ssh = sshFactory.create(socket, node.getCredentials().identity, keyPair.get( + "private").getBytes()); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); @@ -551,13 +519,11 @@ public abstract class BaseComputeServiceLiveTest { } @AfterTest - protected void cleanup() throws InterruptedException, ExecutionException, - TimeoutException { + protected void cleanup() throws InterruptedException, ExecutionException, TimeoutException { if (nodes != null) { client.destroyNodesMatching(NodePredicates.withTag(tag)); - for (NodeMetadata node : Iterables.filter(client - .listNodesDetailsMatching(NodePredicates.all()), NodePredicates - .withTag(tag))) { + for (NodeMetadata node : Iterables.filter(client.listNodesDetailsMatching(NodePredicates + .all()), NodePredicates.withTag(tag))) { assert node.getState() == NodeState.TERMINATED : node; } } diff --git a/compute/src/test/java/org/jclouds/compute/StubComputeServiceIntegrationTest.java b/compute/src/test/java/org/jclouds/compute/StubComputeServiceIntegrationTest.java index ebf13f79be..7363797802 100644 --- a/compute/src/test/java/org/jclouds/compute/StubComputeServiceIntegrationTest.java +++ b/compute/src/test/java/org/jclouds/compute/StubComputeServiceIntegrationTest.java @@ -66,8 +66,7 @@ import com.google.inject.Module; * @author Adrian Cole */ @Test(groups = "live", enabled = true, sequential = true, testName = "stub.StubComputeServiceIntegrationTest") -public class StubComputeServiceIntegrationTest extends - BaseComputeServiceLiveTest { +public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTest { private static final ExecResponse EXEC_GOOD = new ExecResponse("", "", 0); private static final ExecResponse EXEC_BAD = new ExecResponse("", "", 1); @@ -85,10 +84,9 @@ public class StubComputeServiceIntegrationTest extends @Test public void testTemplateBuilder() { Template defaultTemplate = client.templateBuilder().build(); - assertEquals(defaultTemplate.getImage().getArchitecture(), - Architecture.X86_64); + assertEquals(defaultTemplate.getImage().getArchitecture(), Architecture.X86_64); assertEquals(defaultTemplate.getImage().getOsFamily(), OsFamily.UBUNTU); - assertEquals(defaultTemplate.getLocation().getId(), "memory"); + assertEquals(defaultTemplate.getLocation().getId(), provider); assertEquals(defaultTemplate.getSize().getCores(), 4.0d); } @@ -111,21 +109,17 @@ public class StubComputeServiceIntegrationTest extends expect(open.apply(new IPSocket("144.175.1.4", 22))).andReturn(true); expect( - factory.create(eq(new IPSocket("144.175.1.1", 22)), - eq("root"), aryEq(keyPair.get("private").getBytes()))) - .andReturn(client1).atLeastOnce(); + factory.create(eq(new IPSocket("144.175.1.1", 22)), eq("root"), aryEq(keyPair + .get("private").getBytes()))).andReturn(client1).atLeastOnce(); expect( - factory.create(eq(new IPSocket("144.175.1.2", 22)), - eq("root"), aryEq(keyPair.get("private").getBytes()))) - .andReturn(client2).atLeastOnce(); + factory.create(eq(new IPSocket("144.175.1.2", 22)), eq("root"), aryEq(keyPair + .get("private").getBytes()))).andReturn(client2).atLeastOnce(); expect( - factory.create(eq(new IPSocket("144.175.1.3", 22)), - eq("root"), aryEq(keyPair.get("private").getBytes()))) - .andReturn(client3).atLeastOnce(); + factory.create(eq(new IPSocket("144.175.1.3", 22)), eq("root"), aryEq(keyPair + .get("private").getBytes()))).andReturn(client3).atLeastOnce(); expect( - factory.create(eq(new IPSocket("144.175.1.4", 22)), - eq("root"), aryEq(keyPair.get("private").getBytes()))) - .andReturn(client4).atLeastOnce(); + factory.create(eq(new IPSocket("144.175.1.4", 22)), eq("root"), aryEq(keyPair + .get("private").getBytes()))).andReturn(client4).atLeastOnce(); helloAndJava(client1); helloAndJava(client2); @@ -147,10 +141,8 @@ public class StubComputeServiceIntegrationTest extends private void helloAndJava(SshClient client) { client.connect(); - expect(client.exec("echo hello")).andReturn( - new ExecResponse("hello", "", 0)); - expect(client.exec("java -version")).andReturn( - new ExecResponse("", "OpenJDK", 0)); + expect(client.exec("echo hello")).andReturn(new ExecResponse("hello", "", 0)); + expect(client.exec("java -version")).andReturn(new ExecResponse("", "OpenJDK", 0)); client.disconnect(); } @@ -170,26 +162,21 @@ public class StubComputeServiceIntegrationTest extends SshClient client3 = createMock(SshClient.class); SshClient client4 = createMock(SshClient.class); - expect( - factory.create(new IPSocket("144.175.1.1", 22), "root", - "romeo")).andThrow(new SshException("Auth fail")); - expect( - factory.create(new IPSocket("144.175.1.1", 22), "root", - "password1")).andReturn(client1).atLeastOnce(); + expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "romeo")).andThrow( + new SshException("Auth fail")); + expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "password1")).andReturn( + client1).atLeastOnce(); client1.connect(); runScript(client1, "computeserv", 1); client1.disconnect(); - expect( - factory.create(new IPSocket("144.175.1.2", 22), "root", - "password2")).andReturn(client2).atLeastOnce(); - expect( - factory.create(new IPSocket("144.175.1.3", 22), "root", - "password3")).andReturn(client3).atLeastOnce(); - expect( - factory.create(new IPSocket("144.175.1.4", 22), "root", - "password4")).andReturn(client4).atLeastOnce(); + expect(factory.create(new IPSocket("144.175.1.2", 22), "root", "password2")).andReturn( + client2).atLeastOnce(); + expect(factory.create(new IPSocket("144.175.1.3", 22), "root", "password3")).andReturn( + client3).atLeastOnce(); + expect(factory.create(new IPSocket("144.175.1.4", 22), "root", "password4")).andReturn( + client4).atLeastOnce(); runScriptAndInstallSsh(client2, "runscript", 2); runScriptAndInstallSsh(client3, "runscript", 3); @@ -204,17 +191,14 @@ public class StubComputeServiceIntegrationTest extends bind(SshClient.Factory.class).toInstance(factory); } - private void runScriptAndInstallSsh(SshClient client, - String scriptName, int nodeId) { + private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) { client.connect(); runScript(client, scriptName, nodeId); expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD); - expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys")) - .andReturn(EXEC_GOOD); - expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn( - EXEC_GOOD); + expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys")).andReturn(EXEC_GOOD); + expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn(EXEC_GOOD); client.put(eq(".ssh/id_rsa.pub"), isEq(keyPair.get("public"))); expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD); @@ -228,26 +212,18 @@ public class StubComputeServiceIntegrationTest extends private void runScript(SshClient client, String scriptName, int nodeId) { client.put(eq("" + scriptName + ""), isEq(initScript(scriptName, - buildScript(OsFamily.UBUNTU)))); + buildScript(OsFamily.UBUNTU)))); - expect(client.exec("chmod 755 " + scriptName + "")).andReturn( - EXEC_GOOD); + expect(client.exec("chmod 755 " + scriptName + "")).andReturn(EXEC_GOOD); expect(client.getUsername()).andReturn("root").atLeastOnce(); - expect(client.getHostAddress()).andReturn(nodeId + "") - .atLeastOnce(); - expect(client.exec("./" + scriptName + " init")).andReturn( - EXEC_GOOD); - expect(client.exec("./" + scriptName + " start")).andReturn( - EXEC_GOOD); - expect(client.exec("./" + scriptName + " status")).andReturn( - EXEC_GOOD); + expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce(); + expect(client.exec("./" + scriptName + " init")).andReturn(EXEC_GOOD); + expect(client.exec("./" + scriptName + " start")).andReturn(EXEC_GOOD); + expect(client.exec("./" + scriptName + " status")).andReturn(EXEC_GOOD); // next status says the script is done, since not found. - expect(client.exec("./" + scriptName + " status")).andReturn( - EXEC_BAD); - expect(client.exec("./" + scriptName + " tail")).andReturn( - EXEC_GOOD); - expect(client.exec("./" + scriptName + " tailerr")).andReturn( - EXEC_GOOD); + expect(client.exec("./" + scriptName + " status")).andReturn(EXEC_BAD); + expect(client.exec("./" + scriptName + " tail")).andReturn(EXEC_GOOD); + expect(client.exec("./" + scriptName + " tailerr")).andReturn(EXEC_GOOD); } }; @@ -264,11 +240,10 @@ public class StubComputeServiceIntegrationTest extends } public static String initScript(String scriptName, String script) { - return new InitBuilder(scriptName, "/tmp/" + scriptName, "/tmp/" - + scriptName, ImmutableMap. of(), Iterables - .toArray(Splitter.on("\n").split( - new String(checkNotNull(script, "script"))), String.class)) - .build(org.jclouds.scriptbuilder.domain.OsFamily.UNIX); + return new InitBuilder(scriptName, "/tmp/" + scriptName, "/tmp/" + scriptName, ImmutableMap + . of(), Iterables.toArray(Splitter.on("\n").split( + new String(checkNotNull(script, "script"))), String.class)) + .build(org.jclouds.scriptbuilder.domain.OsFamily.UNIX); } public static InputStream isEq(String value) { @@ -279,12 +254,10 @@ public class StubComputeServiceIntegrationTest extends public void testAssignability() throws Exception { @SuppressWarnings("unused") RestContext, ConcurrentMap> stubContext = new ComputeServiceContextFactory() - .createContext(provider, identity, credential) - .getProviderSpecificContext(); + .createContext(provider, identity, credential).getProviderSpecificContext(); } - private static class InputStreamEquals implements IArgumentMatcher, - Serializable { + private static class InputStreamEquals implements IArgumentMatcher, Serializable { private static final long serialVersionUID = 583055160049982067L; @@ -330,8 +303,8 @@ public class StubComputeServiceIntegrationTest extends if (o == null || !this.getClass().equals(o.getClass())) return false; InputStreamEquals other = (InputStreamEquals) o; - return this.expected == null && other.expected == null - || this.expected != null && this.expected.equals(other.expected); + return this.expected == null && other.expected == null || this.expected != null + && this.expected.equals(other.expected); } @Override @@ -343,8 +316,50 @@ public class StubComputeServiceIntegrationTest extends @Override protected void setupKeyPair() throws FileNotFoundException, IOException { - keyPair = ImmutableMap. of("public", "ssh-rsa", - "private", "-----BEGIN RSA PRIVATE KEY-----"); + keyPair = ImmutableMap. of("public", "ssh-rsa", "private", + "-----BEGIN RSA PRIVATE KEY-----"); + } + + // TODO: I have absolutely no idea why I have to redeclare all this cruft. If I don't, then we + // get all sorts of not allowed to depend on errors. + @Override + public void testImagesCache() throws Exception { + super.testImagesCache(); + } + + @Test(enabled = true, dependsOnMethods = { "testImagesCache" }) + public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception { + super.testAScriptExecutionAfterBootWithBasicTemplate(); + } + + @Test(enabled = true, dependsOnMethods = "testTemplateMatch") + public void testCreateTwoNodesWithRunScript() throws Exception { + super.testCreateTwoNodesWithRunScript(); + } + + @Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript") + public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception { + super.testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired(); + } + + @Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired") + public void testGet() throws Exception { + super.testGet(); + } + + @Test(enabled = true, dependsOnMethods = "testGet") + public void testOptionToNotBlock() throws Exception { + super.testOptionToNotBlock(); + } + + @Test(enabled = true, dependsOnMethods = "testGet") + public void testReboot() throws Exception { + super.testReboot(); + } + + @Test(enabled = true, dependsOnMethods = { "testImagesCache" }) + public void testTemplateMatch() throws Exception { + super.testTemplateMatch(); } } \ No newline at end of file diff --git a/core/src/main/java/org/jclouds/rest/RestContextFactory.java b/core/src/main/java/org/jclouds/rest/RestContextFactory.java index f1ec2ccb7a..b7689cf29b 100644 --- a/core/src/main/java/org/jclouds/rest/RestContextFactory.java +++ b/core/src/main/java/org/jclouds/rest/RestContextFactory.java @@ -19,6 +19,11 @@ package org.jclouds.rest; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.Iterables.toArray; +import static org.jclouds.util.Utils.initContextBuilder; +import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException; +import static org.jclouds.util.Utils.resolveContextBuilderClass; +import static org.jclouds.util.Utils.resolvePropertiesBuilderClass; import java.io.IOException; import java.util.Properties; @@ -27,11 +32,9 @@ import javax.annotation.Nullable; import javax.inject.Inject; import org.jclouds.PropertiesBuilder; -import org.jclouds.util.Utils; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; import com.google.inject.Module; /** @@ -239,8 +242,8 @@ public class RestContextFactory { Class sync; Class async; try { - contextBuilderClass = Utils.resolveContextBuilderClass(providerName, props); - propertiesBuilderClass = Utils.resolvePropertiesBuilderClass(providerName, props); + contextBuilderClass = resolveContextBuilderClass(providerName, props); + propertiesBuilderClass = resolvePropertiesBuilderClass(providerName, props); sync = (Class) (syncClassName != null ? Class.forName(syncClassName) : null); async = (Class) (syncClassName != null ? Class.forName(asyncClassName) : null); } catch (Exception e) { @@ -271,7 +274,6 @@ public class RestContextFactory { public static RestContextBuilder createContextBuilder( ContextSpec contextSpec, Iterable modules, Properties overrides) { try { - PropertiesBuilder builder = contextSpec.propertiesBuilderClass.getConstructor( Properties.class).newInstance(overrides); @@ -283,69 +285,70 @@ public class RestContextFactory { if (contextSpec.endpoint != null) builder.endpoint(contextSpec.endpoint); - RestContextBuilder contextBuilder = Utils.initContextBuilder( + RestContextBuilder contextBuilder = initContextBuilder( contextSpec.contextBuilderClass, contextSpec.sync, contextSpec.async, builder .build()); - contextBuilder.withModules(Iterables.toArray(modules, Module.class)); + contextBuilder.withModules(toArray(modules, Module.class)); return contextBuilder; } catch (Exception e) { - AuthorizationException aex = Utils - .getFirstThrowableOfType(e, AuthorizationException.class); - if (aex != null) - throw aex; - Throwables.propagate(e); - assert false : "exception should have propogated " + e; - return null; + return propagateAuthorizationOrOriginalException(e); } } /** * @see RestContextFactory#createContextBuilder(String, String, String) */ - @SuppressWarnings("unchecked") - public RestContext createContext(String provider, String identity, - String credential) { - return (RestContext) createContextBuilder(provider, identity, credential) - .buildContext(); + public RestContext createContext(String provider, String identity, String credential) { + RestContextBuilder builder = createContextBuilder(provider, identity, credential); + return buildContextUnwrappingExceptions(builder); + } + + public static RestContext buildContextUnwrappingExceptions( + RestContextBuilder builder) { + try { + return builder.buildContext(); + } catch (Exception e) { + return propagateAuthorizationOrOriginalException(e); + } } /** * @see RestContextFactory#createContextBuilder(String, Properties) */ - @SuppressWarnings("unchecked") public RestContext createContext(String provider, Properties overrides) { - return (RestContext) createContextBuilder(provider, overrides).buildContext(); + RestContextBuilder builder = createContextBuilder(provider, overrides); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(String, Iterable) */ - @SuppressWarnings("unchecked") public RestContext createContext(String provider, Iterable modules, Properties overrides) { - return (RestContext) createContextBuilder(provider, modules, overrides).buildContext(); + RestContextBuilder builder = createContextBuilder(provider, modules, overrides); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(String, String,String, Iterable) */ - @SuppressWarnings("unchecked") public RestContext createContext(String provider, @Nullable String identity, @Nullable String credential, Iterable modules) { - return (RestContext) createContextBuilder(provider, identity, credential, modules) - .buildContext(); + RestContextBuilder builder = createContextBuilder(provider, identity, credential, + modules); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties) */ - @SuppressWarnings("unchecked") - public RestContext createContext(String providerName, @Nullable String identity, + public RestContext createContext(String provider, @Nullable String identity, @Nullable String credential, Iterable modules, Properties overrides) { - return (RestContext) createContextBuilder(providerName, identity, credential, modules, - overrides).buildContext(); + RestContextBuilder builder = createContextBuilder(provider, identity, credential, + modules, overrides); + return buildContextUnwrappingExceptions(builder); } /** @@ -353,14 +356,16 @@ public class RestContextFactory { */ public static RestContext createContext(ContextSpec contextSpec, Iterable modules, Properties overrides) { - return createContextBuilder(contextSpec, modules, overrides).buildContext(); + RestContextBuilder builder = createContextBuilder(contextSpec, modules, overrides); + return buildContextUnwrappingExceptions(builder); } /** * @see RestContextFactory#createContextBuilder(ContextSpec) */ public static RestContext createContext(ContextSpec contextSpec) { - return createContextBuilder(contextSpec).buildContext(); + RestContextBuilder builder = createContextBuilder(contextSpec); + return buildContextUnwrappingExceptions(builder); } /** @@ -368,7 +373,8 @@ public class RestContextFactory { */ public static RestContext createContext(ContextSpec contextSpec, Iterable modules) { - return createContextBuilder(contextSpec, modules).buildContext(); + RestContextBuilder builder = createContextBuilder(contextSpec, modules); + return buildContextUnwrappingExceptions(builder); } /** @@ -376,6 +382,7 @@ public class RestContextFactory { */ public static RestContext createContext(ContextSpec contextSpec, Properties overrides) { - return createContextBuilder(contextSpec, overrides).buildContext(); + RestContextBuilder builder = createContextBuilder(contextSpec, overrides); + return buildContextUnwrappingExceptions(builder); } } diff --git a/core/src/main/java/org/jclouds/util/Utils.java b/core/src/main/java/org/jclouds/util/Utils.java index d88e85f878..a25c9ceaa0 100644 --- a/core/src/main/java/org/jclouds/util/Utils.java +++ b/core/src/main/java/org/jclouds/util/Utils.java @@ -20,6 +20,14 @@ package org.jclouds.util; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Predicates.instanceOf; +import static com.google.common.base.Predicates.notNull; +import static com.google.common.base.Throwables.getCausalChain; +import static com.google.common.base.Throwables.propagate; +import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Iterables.find; +import static com.google.common.collect.Iterables.get; +import static com.google.common.collect.Iterables.transform; import static org.jclouds.util.Patterns.CHAR_TO_PATTERN; import static org.jclouds.util.Patterns.TOKEN_TO_PATTERN; @@ -44,17 +52,16 @@ import javax.annotation.Resource; import org.jclouds.PropertiesBuilder; import org.jclouds.logging.Logger; +import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.RestContextBuilder; import com.google.common.base.Charsets; import com.google.common.base.Function; import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.base.Supplier; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.io.ByteStreams; import com.google.common.io.Closeables; @@ -69,6 +76,15 @@ import com.google.inject.spi.Message; */ public class Utils { + public static T propagateAuthorizationOrOriginalException(Exception e) { + AuthorizationException aex = getFirstThrowableOfType(e, AuthorizationException.class); + if (aex != null) + throw aex; + Throwables.propagate(e); + assert false : "exception should have propogated " + e; + return null; + } + /** * Like Ordering, but handle the case where there are multiple valid maximums */ @@ -100,28 +116,31 @@ public class Utils { } public static Object propagateOrNull(Exception from) { - Throwables.propagate(from); + propagate(from); assert false : "exception should have propogated"; return null; } @SuppressWarnings("unchecked") public static T getFirstThrowableOfType(Throwable from, Class clazz) { + if (from instanceof ProvisionException) + return getFirstThrowableOfType(ProvisionException.class.cast(from), clazz); try { - return (T) Iterables.find(Throwables.getCausalChain(from), Predicates.instanceOf(clazz)); + return (T) find(getCausalChain(from), instanceOf(clazz)); } catch (NoSuchElementException e) { return null; } } - public static Throwable firstRootCauseOrOriginalException(ProvisionException e) { + public static T getFirstThrowableOfType(ProvisionException e, + Class clazz) { for (Message message : e.getErrorMessages()) { - Throwable cause = Throwables.getRootCause(message.getCause()); + T cause = getFirstThrowableOfType(message.getCause(), clazz); if (cause instanceof ProvisionException) - return firstRootCauseOrOriginalException(ProvisionException.class.cast(cause)); + return getFirstThrowableOfType(ProvisionException.class.cast(cause), clazz); return cause; } - return e; + return null; } public static String replaceTokens(String value, Iterable> tokenValues) { @@ -319,7 +338,7 @@ public class Utils { @SuppressWarnings("unchecked") public static Iterable getSupportedProvidersOfTypeInProperties( final Class type, final Properties properties) { - return Iterables.filter(Iterables.transform(Iterables.filter(properties.entrySet(), + return filter(transform(filter(properties.entrySet(), new Predicate>() { @Override @@ -334,19 +353,19 @@ public class Utils { public String apply(Entry from) { String keyString = from.getKey().toString(); try { - String provider = Iterables.get(Splitter.on('.').split(keyString), 0); + String provider = get(Splitter.on('.').split(keyString), 0); Class> clazz = resolveContextBuilderClass( provider, properties); if (type.isAssignableFrom(clazz)) return provider; } catch (ClassNotFoundException e) { } catch (Exception e) { - Throwables.propagate(e); + propagate(e); } return null; } - }), Predicates.notNull()); + }), notNull()); } @SuppressWarnings("unchecked") diff --git a/core/src/test/java/org/jclouds/util/UtilsTest.java b/core/src/test/java/org/jclouds/util/UtilsTest.java index e330f6a1b1..1a222ac1a8 100644 --- a/core/src/test/java/org/jclouds/util/UtilsTest.java +++ b/core/src/test/java/org/jclouds/util/UtilsTest.java @@ -45,7 +45,7 @@ public class UtilsTest { AuthorizationException aex = createMock(AuthorizationException.class); Message message = new Message(ImmutableList.of(), "test", aex); ProvisionException pex = new ProvisionException(ImmutableSet.of(message)); - assertEquals(Utils.firstRootCauseOrOriginalException(pex), aex); + assertEquals(Utils.getFirstThrowableOfType(pex, AuthorizationException.class), aex); } public void testGetFirstThrowableOfTypeOuter() { diff --git a/gogrid/src/main/java/org/jclouds/gogrid/functions/ReturnEmptySetOnNotFound.java b/gogrid/src/main/java/org/jclouds/gogrid/functions/ReturnEmptySetOnNotFound.java new file mode 100644 index 0000000000..68fe4fe0d0 --- /dev/null +++ b/gogrid/src/main/java/org/jclouds/gogrid/functions/ReturnEmptySetOnNotFound.java @@ -0,0 +1,47 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.gogrid.functions; + +import static org.jclouds.util.Utils.propagateOrNull; + +import java.util.Set; + +import javax.inject.Singleton; + +import org.jclouds.gogrid.domain.Server; +import org.jclouds.rest.ResourceNotFoundException; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class ReturnEmptySetOnNotFound implements Function> { + @SuppressWarnings("unchecked") + public Set apply(Exception from) { + if (from instanceof ResourceNotFoundException) { + return ImmutableSet. of(); + } + return Set.class.cast(propagateOrNull(from)); + } +} \ No newline at end of file