diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java index 64c8eb219a..97be5ce8e0 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.java @@ -39,6 +39,7 @@ import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.options.RunInstancesOptions; import org.jclouds.compute.domain.Template; import org.jclouds.compute.options.TemplateOptions; +import org.jclouds.rest.annotations.Provider; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Sets; @@ -49,6 +50,8 @@ import com.google.common.collect.Sets; */ @Singleton public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions { + @VisibleForTesting + final String provider; @VisibleForTesting final Map credentialsMap; @VisibleForTesting @@ -63,11 +66,12 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions final CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded; @Inject - CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions(Map credentialsMap, - @Named("SECURITY") Map securityGroupMap, + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions(@Provider String provider, + Map credentialsMap, @Named("SECURITY") Map securityGroupMap, @Named("PLACEMENT") Map placementGroupMap, CreateUniqueKeyPair createUniqueKeyPair, CreateSecurityGroupIfNeeded createSecurityGroupIfNeeded, CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded) { + this.provider = provider; this.credentialsMap = credentialsMap; this.securityGroupMap = securityGroupMap; this.placementGroupMap = placementGroupMap; @@ -76,9 +80,18 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions this.createPlacementGroupIfNeeded = createPlacementGroupIfNeeded; } + // this method only exists so that we can mock + String getProvider() { + return provider; + } + public RunInstancesOptions execute(String region, String tag, Template template) { - RunInstancesOptions instanceOptions = asType(template.getHardware().getId()).withAdditionalInfo(tag); + RunInstancesOptions instanceOptions = asType(template.getHardware().getId()); + + // Eucalyptus currently doesn't support additional info + if (!"eucalyptus".equals(getProvider())) + instanceOptions.withAdditionalInfo(tag); String keyPairName = createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, template.getOptions()); diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java index c65f73be49..4affe571f8 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java @@ -56,7 +56,7 @@ import com.google.common.collect.ImmutableSet; @Test(groups = "unit", testName = "ec2.CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest") public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest { - public void testExecuteWithDefaultOptions() throws SecurityException, NoSuchMethodException { + public void testExecuteWithDefaultOptionsEC2() throws SecurityException, NoSuchMethodException { // setup constants String region = Region.AP_SOUTHEAST_1; String tag = "tag"; @@ -69,6 +69,8 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = createMock( CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class, new Method[] { + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class + .getDeclaredMethod("getProvider"), CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( "createNewKeyPairUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), @@ -87,6 +89,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, options)).andReturn( systemGeneratedKeyPairName); expect(strategy.getSecurityGroupsForTagAndOptions(region, tag, options)).andReturn(generatedGroups); + expect(strategy.getProvider()).andReturn("ec2"); expect(options.getSubnetId()).andReturn(null); expect(options.getUserData()).andReturn(null); @@ -112,6 +115,65 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT verify(strategy); } + public void testEucalyptusDoesNotUseAdditionalInfo() throws SecurityException, NoSuchMethodException { + // setup constants + String region = Region.AP_SOUTHEAST_1; + String tag = "tag"; + Hardware size = EC2HardwareBuilder.m1_small().build(); + String systemGeneratedKeyPairName = "systemGeneratedKeyPair"; + String generatedGroup = "group"; + Set generatedGroups = ImmutableSet.of(generatedGroup); + + // create mocks + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = createMock( + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class, + new Method[] { + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class + .getDeclaredMethod("getProvider"), + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( + "createNewKeyPairUnlessUserSpecifiedOtherwise", String.class, String.class, + TemplateOptions.class), + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( + "createNewPlacementGroupUnlessUserSpecifiedOtherwise", String.class, String.class, + TemplateOptions.class), + CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( + "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + + EC2TemplateOptions options = createMock(EC2TemplateOptions.class); + Template template = createMock(Template.class); + + // setup expectations + expect(template.getHardware()).andReturn(size).atLeastOnce(); + expect(template.getOptions()).andReturn(options).atLeastOnce(); + expect(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, tag, options)).andReturn( + systemGeneratedKeyPairName); + expect(strategy.getSecurityGroupsForTagAndOptions(region, tag, options)).andReturn(generatedGroups); + expect(strategy.getProvider()).andReturn("eucalyptus"); + expect(options.getSubnetId()).andReturn(null); + expect(options.getUserData()).andReturn(null); + + // replay mocks + replay(options); + replay(template); + replay(strategy); + + // run + RunInstancesOptions runOptions = strategy.execute(region, tag, template); + assertEquals(runOptions.buildQueryParameters(), ImmutableMultimap. of()); + assertEquals( + runOptions.buildFormParameters().entries(), + ImmutableMultimap. of("InstanceType", size.getProviderId(), "SecurityGroup.1", + generatedGroup, "KeyName", systemGeneratedKeyPairName).entries()); + assertEquals(runOptions.buildMatrixParameters(), ImmutableMultimap. of()); + assertEquals(runOptions.buildRequestHeaders(), ImmutableMultimap. of()); + assertEquals(runOptions.buildStringPayload(), null); + + // verify mocks + verify(options); + verify(template); + verify(strategy); + } + public void testExecuteForCCAutomatic() throws SecurityException, NoSuchMethodException { // setup constants String region = Region.US_EAST_1; @@ -291,7 +353,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT String systemGeneratedKeyPairName = "systemGeneratedKeyPair"; String generatedGroup = "group"; Set generatedGroups = ImmutableSet.of(generatedGroup); - + // create mocks CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = createMock( CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions.class, @@ -328,7 +390,8 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT assertEquals( runOptions.buildFormParameters().entries(), ImmutableMultimap. of("InstanceType", size.getProviderId(), "AdditionalInfo", tag, - "SecurityGroup.1", "group", "KeyName", systemGeneratedKeyPairName, "UserData", Base64.encodeBytes("hello".getBytes())).entries()); + "SecurityGroup.1", "group", "KeyName", systemGeneratedKeyPairName, "UserData", + Base64.encodeBytes("hello".getBytes())).entries()); assertEquals(runOptions.buildMatrixParameters(), ImmutableMultimap. of()); assertEquals(runOptions.buildRequestHeaders(), ImmutableMultimap. of()); assertEquals(runOptions.buildStringPayload(), null); @@ -681,8 +744,12 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT verify(strategy.createPlacementGroupIfNeeded); } - @SuppressWarnings("unchecked") private CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions setupStrategy() { + return setupStrategy("ec2"); + } + + @SuppressWarnings("unchecked") + private CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions setupStrategy(String provider) { Map credentialsMap = createMock(Map.class); Map securityGroupMap = createMock(Map.class); Map placementGroupMap = createMock(Map.class); @@ -690,8 +757,9 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT CreateSecurityGroupIfNeeded createSecurityGroupIfNeeded = createMock(CreateSecurityGroupIfNeeded.class); CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded = createMock(CreatePlacementGroupIfNeeded.class); - return new CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions(credentialsMap, securityGroupMap, - placementGroupMap, createUniqueKeyPair, createSecurityGroupIfNeeded, createPlacementGroupIfNeeded); + return new CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions(provider, credentialsMap, + securityGroupMap, placementGroupMap, createUniqueKeyPair, createSecurityGroupIfNeeded, + createPlacementGroupIfNeeded); } private void replayStrategy(CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy) {