mirror of https://github.com/apache/jclouds.git
Issue 366: generated keypair will clash with existing keypairs in EC2
This commit is contained in:
parent
1ef64845a0
commit
304c214304
|
@ -26,13 +26,13 @@ import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.jclouds.aws.AWSResponseException;
|
|
||||||
import org.jclouds.aws.ec2.EC2Client;
|
import org.jclouds.aws.ec2.EC2Client;
|
||||||
import org.jclouds.aws.ec2.compute.domain.RegionAndName;
|
import org.jclouds.aws.ec2.compute.domain.RegionAndName;
|
||||||
import org.jclouds.aws.ec2.domain.KeyPair;
|
import org.jclouds.aws.ec2.domain.KeyPair;
|
||||||
import org.jclouds.compute.reference.ComputeServiceConstants;
|
import org.jclouds.compute.reference.ComputeServiceConstants;
|
||||||
import org.jclouds.logging.Logger;
|
import org.jclouds.logging.Logger;
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
|
|
||||||
|
@ -59,7 +59,8 @@ public class CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> {
|
||||||
return createNewKeyPairInRegion(from.getRegion(), from.getName());
|
return createNewKeyPairInRegion(from.getRegion(), from.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private KeyPair createNewKeyPairInRegion(String region, String tag) {
|
@VisibleForTesting
|
||||||
|
KeyPair createNewKeyPairInRegion(String region, String tag) {
|
||||||
checkNotNull(region, "region");
|
checkNotNull(region, "region");
|
||||||
checkNotNull(tag, "tag");
|
checkNotNull(tag, "tag");
|
||||||
logger.debug(">> creating keyPair region(%s) tag(%s)", region, tag);
|
logger.debug(">> creating keyPair region(%s) tag(%s)", region, tag);
|
||||||
|
@ -68,10 +69,8 @@ public class CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> {
|
||||||
try {
|
try {
|
||||||
keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, getNextName(region, tag));
|
keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, getNextName(region, tag));
|
||||||
logger.debug("<< created keyPair(%s)", keyPair.getKeyName());
|
logger.debug("<< created keyPair(%s)", keyPair.getKeyName());
|
||||||
} catch (AWSResponseException e) {
|
} catch (IllegalStateException e) {
|
||||||
if (!e.getError().getCode().equals("InvalidKeyPair.Duplicate")) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return keyPair;
|
return keyPair;
|
||||||
|
|
|
@ -94,7 +94,7 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
|
||||||
&& (error.getCode().endsWith(".NotFound") || error.getCode().endsWith(".Unknown")))
|
&& (error.getCode().endsWith(".NotFound") || error.getCode().endsWith(".Unknown")))
|
||||||
exception = new ResourceNotFoundException(message, exception);
|
exception = new ResourceNotFoundException(message, exception);
|
||||||
else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error
|
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))
|
|| (message != null && message.indexOf("already exists") != -1))
|
||||||
exception = new IllegalStateException(message, exception);
|
exception = new IllegalStateException(message, exception);
|
||||||
else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure"))
|
else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure"))
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.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<String> 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<String> 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -75,6 +75,12 @@ public class ParseAWSErrorFromXmlContentTest {
|
||||||
"<Error><Code>InvalidGroup.Duplicate</Code></Error>", IllegalStateException.class);
|
"<Error><Code>InvalidGroup.Duplicate</Code></Error>", IllegalStateException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test400WithInvalidKeyPairGroupDuplicateIllegalStateException() {
|
||||||
|
assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400,"Bad Request", "application/unknown",
|
||||||
|
"<Error><Code>InvalidKeyPair.Duplicate</Code></Error>", IllegalStateException.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test400WithTextPlainIllegalArgumentException() {
|
public void test400WithTextPlainIllegalArgumentException() {
|
||||||
assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400, "Bad Request", "text/plain",
|
assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400, "Bad Request", "text/plain",
|
||||||
|
|
Loading…
Reference in New Issue