From 304c2143045ab5bafbae154b39e13355748f8a00 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 22 Sep 2010 10:49:48 -0700 Subject: [PATCH] Issue 366: generated keypair will clash with existing keypairs in EC2 --- .../functions/CreateUniqueKeyPair.java | 11 +-- .../handlers/ParseAWSErrorFromXmlContent.java | 2 +- .../functions/CreateUniqueKeyPairTest.java | 98 +++++++++++++++++++ .../ParseAWSErrorFromXmlContentTest.java | 6 ++ 4 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPairTest.java diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPair.java b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPair.java index 4dfe52808b..ce833df985 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPair.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPair.java @@ -26,13 +26,13 @@ import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; -import org.jclouds.aws.AWSResponseException; import org.jclouds.aws.ec2.EC2Client; import org.jclouds.aws.ec2.compute.domain.RegionAndName; import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.logging.Logger; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Supplier; @@ -59,7 +59,8 @@ public class CreateUniqueKeyPair implements Function { return createNewKeyPairInRegion(from.getRegion(), from.getName()); } - private KeyPair createNewKeyPairInRegion(String region, String tag) { + @VisibleForTesting + KeyPair createNewKeyPairInRegion(String region, String tag) { checkNotNull(region, "region"); checkNotNull(tag, "tag"); logger.debug(">> creating keyPair region(%s) tag(%s)", region, tag); @@ -68,10 +69,8 @@ public class CreateUniqueKeyPair implements Function { try { keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, getNextName(region, tag)); logger.debug("<< created keyPair(%s)", keyPair.getKeyName()); - } catch (AWSResponseException e) { - if (!e.getError().getCode().equals("InvalidKeyPair.Duplicate")) { - throw e; - } + } catch (IllegalStateException e) { + } } return keyPair; diff --git a/aws/core/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java b/aws/core/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java index a1f472d9d2..1ecc0d1fcc 100755 --- a/aws/core/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java +++ b/aws/core/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java @@ -94,7 +94,7 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler { && (error.getCode().endsWith(".NotFound") || error.getCode().endsWith(".Unknown"))) exception = new ResourceNotFoundException(message, exception); else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error - .getCode().equals("InvalidGroup.Duplicate"))) + .getCode().endsWith(".Duplicate"))) || (message != null && message.indexOf("already exists") != -1)) exception = new IllegalStateException(message, exception); else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure")) diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPairTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPairTest.java new file mode 100644 index 0000000000..edbc35ae5c --- /dev/null +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/CreateUniqueKeyPairTest.java @@ -0,0 +1,98 @@ +/** + * + * Copyright (C) 2010 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.aws.ec2.compute.functions; + +import static org.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; +import static org.testng.Assert.assertEquals; + +import java.net.UnknownHostException; + +import org.jclouds.aws.ec2.EC2Client; +import org.jclouds.aws.ec2.domain.KeyPair; +import org.jclouds.aws.ec2.services.KeyPairClient; +import org.testng.annotations.Test; + +import com.google.common.base.Supplier; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "ec2.CreateUniqueKeyPairTest") +public class CreateUniqueKeyPairTest { + @SuppressWarnings( { "unchecked" }) + @Test + public void testApply() throws UnknownHostException { + EC2Client client = createMock(EC2Client.class); + KeyPairClient keyClient = createMock(KeyPairClient.class); + Supplier uniqueIdSupplier = createMock(Supplier.class); + + KeyPair pair = createMock(KeyPair.class); + + expect(client.getKeyPairServices()).andReturn(keyClient).atLeastOnce(); + + expect(uniqueIdSupplier.get()).andReturn("1"); + expect(keyClient.createKeyPairInRegion("region", "jclouds#tag#region#1")).andReturn(pair); + + replay(client); + replay(keyClient); + replay(uniqueIdSupplier); + + CreateUniqueKeyPair parser = new CreateUniqueKeyPair(client, uniqueIdSupplier); + + assertEquals(parser.createNewKeyPairInRegion("region", "tag"), pair); + + verify(client); + verify(keyClient); + verify(uniqueIdSupplier); + } + + @SuppressWarnings( { "unchecked" }) + @Test + public void testApplyWithIllegalStateException() throws UnknownHostException { + EC2Client client = createMock(EC2Client.class); + KeyPairClient keyClient = createMock(KeyPairClient.class); + Supplier uniqueIdSupplier = createMock(Supplier.class); + + KeyPair pair = createMock(KeyPair.class); + + expect(client.getKeyPairServices()).andReturn(keyClient).atLeastOnce(); + + expect(uniqueIdSupplier.get()).andReturn("1"); + expect(keyClient.createKeyPairInRegion("region", "jclouds#tag#region#1")).andThrow(new IllegalStateException()); + expect(uniqueIdSupplier.get()).andReturn("2"); + expect(keyClient.createKeyPairInRegion("region", "jclouds#tag#region#2")).andReturn(pair); + + replay(client); + replay(keyClient); + replay(uniqueIdSupplier); + + CreateUniqueKeyPair parser = new CreateUniqueKeyPair(client, uniqueIdSupplier); + + assertEquals(parser.createNewKeyPairInRegion("region", "tag"), pair); + + verify(client); + verify(keyClient); + verify(uniqueIdSupplier); + + } +} diff --git a/aws/core/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java b/aws/core/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java index 3e9b212d70..221bea329d 100644 --- a/aws/core/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java @@ -75,6 +75,12 @@ public class ParseAWSErrorFromXmlContentTest { "InvalidGroup.Duplicate", IllegalStateException.class); } + @Test + public void test400WithInvalidKeyPairGroupDuplicateIllegalStateException() { + assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400,"Bad Request", "application/unknown", + "InvalidKeyPair.Duplicate", IllegalStateException.class); + } + @Test public void test400WithTextPlainIllegalArgumentException() { assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400, "Bad Request", "text/plain",