diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.java index 2965efaf83..833be0efe5 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.java @@ -147,17 +147,15 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions { // base EC2 driver currently does not support key import protected String createOrImportKeyPair(String region, String group, TemplateOptions options) { RegionAndName regionAndGroup = new RegionAndName(region, group); - KeyPair keyPair; + KeyPair keyPair = makeKeyPair.apply(new RegionAndName(region, group)); // make sure that we don't request multiple keys simultaneously - synchronized (credentialsMap) { - // if there is already a keypair for the group specified, use it - if (credentialsMap.containsKey(regionAndGroup)) - return credentialsMap.get(regionAndGroup).getKeyName(); - - // otherwise create a new keypair and key it under the group and also the regular keyname - keyPair = makeKeyPair.apply(new RegionAndName(region, group)); - credentialsMap.put(regionAndGroup, keyPair); + // if there is already a keypair for the group specified, use it + // otherwise create a new keypair and key it under the group and also the regular keyname + KeyPair origValue = credentialsMap.putIfAbsent(regionAndGroup, keyPair); + if (origValue != null) { + return origValue.getKeyName(); } + credentialsMap.put(new RegionAndName(region, keyPair.getKeyName()), keyPair); return keyPair.getKeyName(); } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java index 527123a927..cc61fec79a 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/strategy/CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java @@ -17,6 +17,7 @@ package org.jclouds.ec2.compute.strategy; import static com.google.common.io.BaseEncoding.base64; +import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; @@ -356,8 +357,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { expect(options.getKeyPair()).andReturn(userSuppliedKeyPair); expect(options.shouldAutomaticallyCreateKeyPair()).andReturn(shouldAutomaticallyCreateKeyPair); expect(keyPair.getKeyName()).andReturn(systemGeneratedKeyPairName).atLeastOnce(); - expect(strategy.credentialsMap.containsKey(new RegionAndName(region, group))).andReturn(true); - expect(strategy.credentialsMap.get(new RegionAndName(region, group))).andReturn(keyPair); + expect(strategy.credentialsMap.putIfAbsent(anyObject(RegionAndName.class), anyObject(KeyPair.class))).andReturn(keyPair); expect(options.getRunScript()).andReturn(null); // replay mocks @@ -557,6 +557,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { LoadingCache securityGroupMap = createMock(LoadingCache.class); GroupNamingConvention.Factory namingConventionFactory = createMock(GroupNamingConvention.Factory.class); GroupNamingConvention namingConvention = createMock(GroupNamingConvention.class); + expect(makeKeyPair.apply(anyObject(RegionAndName.class))).andReturn(null).anyTimes(); expect(namingConventionFactory.create()).andReturn(namingConvention).anyTimes(); expect(namingConvention.sharedNameForGroup("group")).andReturn("jclouds#group").anyTimes(); replay(namingConventionFactory); diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/compute/options/GoGridTemplateOptions.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/compute/options/GoGridTemplateOptions.java index 22b27c890a..52572acd47 100644 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/compute/options/GoGridTemplateOptions.java +++ b/providers/gogrid/src/main/java/org/jclouds/gogrid/compute/options/GoGridTemplateOptions.java @@ -52,10 +52,6 @@ public class GoGridTemplateOptions extends TemplateOptions implements Cloneable @Override public void copyTo(TemplateOptions to) { super.copyTo(to); - if (to instanceof GoGridTemplateOptions) { - @SuppressWarnings("unused") - GoGridTemplateOptions eTo = GoGridTemplateOptions.class.cast(to); - } } public static final GoGridTemplateOptions NONE = new GoGridTemplateOptions();