From 985d45122bc55f6ddf2e3e36f9aac0dca6abd3cf Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Fri, 11 Sep 2015 00:10:11 +0200 Subject: [PATCH] JCLOUDS-987: Create security groups in AWS if subnetId is specified --- .../ec2/compute/EC2ComputeService.java | 2 +- .../domain/RegionNameAndIngressRules.java | 10 ++- .../extensions/EC2SecurityGroupExtension.java | 4 +- ...rityGroupsAsNeededAndReturnRunOptions.java | 12 ++-- .../CreateSecurityGroupIfNeededTest.java | 6 +- ...GroupsAsNeededAndReturnRunOptionsTest.java | 30 ++++---- .../AWSEC2SecurityGroupExtension.java | 4 +- .../AWSEC2CreateSecurityGroupIfNeeded.java | 21 +++--- ...rityGroupsAsNeededAndReturnRunOptions.java | 26 ++++++- .../AWSEC2ComputeServiceApiMockTest.java | 30 ++++---- ...SEC2SecurityGroupExtensionApiMockTest.java | 2 - ...AWSEC2CreateSecurityGroupIfNeededTest.java | 8 ++- ...GroupsAsNeededAndReturnRunOptionsTest.java | 70 +++++++++++-------- 13 files changed, 131 insertions(+), 94 deletions(-) diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java index 701cbc2d2c..e84b65ea59 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ComputeService.java @@ -224,7 +224,7 @@ public class EC2ComputeService extends BaseComputeService { logger.debug(">> deleting securityGroup(%s)", groupName); client.getSecurityGroupApi().get().deleteSecurityGroupInRegion(region, groupName); // TODO: test this clear happens - securityGroupMap.invalidate(new RegionNameAndIngressRules(region, groupName, null, false)); + securityGroupMap.invalidate(new RegionNameAndIngressRules(region, groupName, null, false, null)); logger.debug("<< deleted securityGroup(%s)", groupName); } } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/domain/RegionNameAndIngressRules.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/domain/RegionNameAndIngressRules.java index 081d0754cd..f5b69fa968 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/domain/RegionNameAndIngressRules.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/domain/RegionNameAndIngressRules.java @@ -19,11 +19,13 @@ package org.jclouds.ec2.compute.domain; public class RegionNameAndIngressRules extends RegionAndName { private final int[] ports; private final boolean authorizeSelf; - - public RegionNameAndIngressRules(String region, String tag, int[] ports, boolean authorizeSelf) { + private final String vpcId; + + public RegionNameAndIngressRules(String region, String tag, int[] ports, boolean authorizeSelf, String vpcId) { super(region, tag); this.ports = ports; this.authorizeSelf = authorizeSelf; + this.vpcId = vpcId; } // intentionally not overriding equals or hash-code so that we can search only by region/tag in a @@ -37,4 +39,8 @@ public class RegionNameAndIngressRules extends RegionAndName { return authorizeSelf; } + public String getVpcId() { + return vpcId; + } + } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java index 6160fe684c..081d3cccfa 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/extensions/EC2SecurityGroupExtension.java @@ -158,7 +158,7 @@ public class EC2SecurityGroupExtension implements SecurityGroupExtension { public SecurityGroup createSecurityGroup(String name, String region) { String markerGroup = namingConvention.create().sharedNameForGroup(name); RegionNameAndIngressRules regionAndName = new RegionNameAndIngressRules(region, markerGroup, new int[] {}, - false); + false, null); groupCreator.getUnchecked(regionAndName); @@ -175,7 +175,7 @@ public class EC2SecurityGroupExtension implements SecurityGroupExtension { if (!client.getSecurityGroupApi().get().describeSecurityGroupsInRegion(region, groupName).isEmpty()) { client.getSecurityGroupApi().get().deleteSecurityGroupInRegion(region, groupName); // TODO: test this clear happens - groupCreator.invalidate(new RegionNameAndIngressRules(region, groupName, null, false)); + groupCreator.invalidate(new RegionNameAndIngressRules(region, groupName, null, false, null)); return true; } 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 833be0efe5..e860fa1857 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 @@ -106,7 +106,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions { } protected void addSecurityGroups(String region, String group, Template template, RunInstancesOptions instanceOptions) { - Set groups = getSecurityGroupsForTagAndOptions(region, group, template.getOptions()); + Set groups = getSecurityGroupsForTagAndOptions(region, group, null, template.getOptions()); instanceOptions.withSecurityGroups(groups); } @@ -161,26 +161,24 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions { } @VisibleForTesting - public Set getSecurityGroupsForTagAndOptions(String region, @Nullable String group, TemplateOptions options) { + public Set getSecurityGroupsForTagAndOptions(String region, @Nullable String group, @Nullable String vpcId, TemplateOptions options) { Builder groups = ImmutableSet.builder(); if (group != null) { String markerGroup = namingConvention.create().sharedNameForGroup(group); - groups.add(markerGroup); - RegionNameAndIngressRules regionNameAndIngressRulesForMarkerGroup; if (userSpecifiedTheirOwnGroups(options)) { regionNameAndIngressRulesForMarkerGroup = new RegionNameAndIngressRules(region, markerGroup, new int[] {}, - false); + false, vpcId); groups.addAll(EC2TemplateOptions.class.cast(options).getGroups()); } else { regionNameAndIngressRulesForMarkerGroup = new RegionNameAndIngressRules(region, markerGroup, options - .getInboundPorts(), true); + .getInboundPorts(), true, vpcId); } // this will create if not yet exists. - securityGroupMap.getUnchecked(regionNameAndIngressRulesForMarkerGroup); + groups.add(securityGroupMap.getUnchecked(regionNameAndIngressRulesForMarkerGroup)); } return groups.build(); } diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/loaders/CreateSecurityGroupIfNeededTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/loaders/CreateSecurityGroupIfNeededTest.java index 98ee834cc9..d562665087 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/compute/loaders/CreateSecurityGroupIfNeededTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/loaders/CreateSecurityGroupIfNeededTest.java @@ -63,7 +63,7 @@ public class CreateSecurityGroupIfNeededTest { CreateSecurityGroupIfNeeded function = new CreateSecurityGroupIfNeeded(client, tester); - assertEquals("group", function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true))); + assertEquals("group", function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true, null))); verify(client); verify(group); @@ -83,7 +83,7 @@ public class CreateSecurityGroupIfNeededTest { CreateSecurityGroupIfNeeded function = new CreateSecurityGroupIfNeeded(client, tester); - assertEquals("group", function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true))); + assertEquals("group", function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true, null))); verify(client); @@ -100,6 +100,6 @@ public class CreateSecurityGroupIfNeededTest { replay(client); CreateSecurityGroupIfNeeded function = new CreateSecurityGroupIfNeeded(client, tester); - function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true)); + function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true, null)); } } 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 cc61fec79a..055eebe1e6 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 @@ -112,7 +112,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { "createNewKeyPairUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( - "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + "getSecurityGroupsForTagAndOptions", String.class, String.class, String.class, + TemplateOptions.class) }); EC2TemplateOptions options = createMock(EC2TemplateOptions.class); Template template = createMock(Template.class); @@ -124,7 +125,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { expect(options.getBlockDeviceMappings()).andReturn(ImmutableSet. of()).atLeastOnce(); expect(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options)).andReturn( systemGeneratedKeyPairName); - expect(strategy.getSecurityGroupsForTagAndOptions(region, group, options)).andReturn(generatedGroups); + expect(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options)).andReturn(generatedGroups); expect(options.getUserData()).andReturn(null); expect(options.getClientToken()).andReturn(null); @@ -168,7 +169,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { "createNewKeyPairUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( - "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + "getSecurityGroupsForTagAndOptions", String.class, String.class, String.class, + TemplateOptions.class) }); EC2TemplateOptions options = createMock(EC2TemplateOptions.class); Template template = createMock(Template.class); @@ -181,7 +183,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { expect(options.getClientToken()).andReturn("some-token"); expect(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options)).andReturn( systemGeneratedKeyPairName); - expect(strategy.getSecurityGroupsForTagAndOptions(region, group, options)).andReturn(generatedGroups); + expect(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options)).andReturn(generatedGroups); expect(options.getUserData()).andReturn("hello".getBytes()); // replay mocks @@ -426,15 +428,15 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { expect(options.getGroups()).andReturn(groupIds).atLeastOnce(); expect(options.getInboundPorts()).andReturn(ports).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); - expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn(group); + ports, shouldAuthorizeSelf, null); + expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn(generatedMarkerGroup); // replay mocks replay(options); replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -460,7 +462,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { expect(options.getGroups()).andReturn(groupIds).atLeastOnce(); expect(options.getInboundPorts()).andReturn(ports).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); + ports, shouldAuthorizeSelf, null); expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn(generatedMarkerGroup); // replay mocks @@ -468,7 +470,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -494,7 +496,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { expect(options.getGroups()).andReturn(groupIds).atLeastOnce(); expect(options.getInboundPorts()).andReturn(ports).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); + ports, shouldAuthorizeSelf, null); expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn(generatedMarkerGroup); // replay mocks @@ -502,7 +504,7 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -527,17 +529,17 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest { // setup expectations expect(options.getGroups()).andReturn(groupIds).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); + ports, shouldAuthorizeSelf, null); expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)) - .andReturn(groupExisted ? "group" : null); + .andReturn(groupExisted ? generatedMarkerGroup : null); // replay mocks replay(options); replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtension.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtension.java index 68fc14f3cf..db1e8f24b4 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtension.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtension.java @@ -72,7 +72,7 @@ public class AWSEC2SecurityGroupExtension extends EC2SecurityGroupExtension { public SecurityGroup createSecurityGroup(String name, String region) { String markerGroup = namingConvention.create().sharedNameForGroup(name); RegionNameAndIngressRules regionAndName = new RegionNameAndIngressRules(region, markerGroup, new int[] {}, - false); + false, null); groupCreator.getUnchecked(regionAndName); String groupId = groupNameToId.apply(regionAndName.slashEncode()); @@ -106,7 +106,7 @@ public class AWSEC2SecurityGroupExtension extends EC2SecurityGroupExtension { if (group != null) { client.getSecurityGroupApi().get().deleteSecurityGroupInRegionById(region, groupId); // TODO: test this clear happens - groupCreator.invalidate(new RegionNameAndIngressRules(region, group.getName(), null, false)); + groupCreator.invalidate(new RegionNameAndIngressRules(region, group.getName(), null, false, null)); return true; } diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeeded.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeeded.java index 81f3e3498d..9e17354e2f 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeeded.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeeded.java @@ -29,6 +29,7 @@ import javax.inject.Singleton; import org.jclouds.aws.ec2.AWSEC2Api; import org.jclouds.aws.ec2.features.AWSSecurityGroupApi; +import org.jclouds.aws.ec2.options.CreateSecurityGroupOptions; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules; @@ -69,17 +70,20 @@ public class AWSEC2CreateSecurityGroupIfNeeded extends CacheLoader> creating securityGroup region(%s) name(%s)", region, name); try { - securityApi.createSecurityGroupInRegion(region, name, name); + CreateSecurityGroupOptions options = new CreateSecurityGroupOptions(); + if (vpcId != null) { + options.vpcId(vpcId); + } + String id = securityApi.createSecurityGroupInRegionAndReturnId(region, name, name, options); boolean created = securityGroupEventualConsistencyDelay.apply(new RegionAndName(region, name)); if (!created) throw new RuntimeException(String.format("security group %s/%s is not available after creating", region, @@ -87,12 +91,6 @@ public class AWSEC2CreateSecurityGroupIfNeeded extends CacheLoader permissions = ImmutableSet.builder(); - String id; - if (name.startsWith("sg-")) { - id = name; - } else { - id = groupNameToId.apply(new RegionAndName(region, name).slashEncode()); - } if (ports.length > 0) { for (Map.Entry range : getPortRangesFromList(ports).entrySet()) { @@ -126,9 +124,10 @@ public class AWSEC2CreateSecurityGroupIfNeeded extends CacheLoader importExistingKeyPair; + private final AWSEC2Api awsEC2Api; @Inject public CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions( @@ -68,11 +74,13 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions @Named("PLACEMENT") LoadingCache placementGroupMap, CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded, Function importExistingKeyPair, - GroupNamingConvention.Factory namingConvention) { + GroupNamingConvention.Factory namingConvention, + AWSEC2Api awsEC2Api) { super(makeKeyPair, credentialsMap, securityGroupMap, optionsProvider, namingConvention); this.placementGroupMap = placementGroupMap; this.createPlacementGroupIfNeeded = createPlacementGroupIfNeeded; this.importExistingKeyPair = importExistingKeyPair; + this.awsEC2Api = awsEC2Api; } public AWSRunInstancesOptions execute(String region, String group, Template template) { @@ -180,9 +188,21 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions awsInstanceOptions.withSecurityGroupIds(awsTemplateOptions.getGroupIds()); String subnetId = awsTemplateOptions.getSubnetId(); if (subnetId != null) { - AWSRunInstancesOptions.class.cast(instanceOptions).withSubnetId(subnetId); + Set groups = getSecurityGroupsForTagAndOptions(region, group, vpcIdForSubnet(subnetId), template.getOptions()); + awsInstanceOptions.withSubnetId(subnetId); + awsInstanceOptions.withSecurityGroupIds(groups); } else { - super.addSecurityGroups(region, group, template, instanceOptions); + Set groups = getSecurityGroupsForTagAndOptions(region, group, null, template.getOptions()); + awsInstanceOptions.withSecurityGroupIds(groups); } } + + @VisibleForTesting + String vpcIdForSubnet(String subnetId) { + Optional subnet = awsEC2Api.getSubnetApi().get().filter(new SubnetFilterBuilder().subnetId(subnetId).build()).first(); + if (!subnet.isPresent()) { + throw new IllegalArgumentException("Subnet " + subnetId + " not found"); + } + return subnet.get().getVpcId(); + } } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceApiMockTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceApiMockTest.java index 4e55c764de..f6475782c2 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceApiMockTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2ComputeServiceApiMockTest.java @@ -51,6 +51,11 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { enqueueXml(DEFAULT_REGION, "/availabilityZones.xml"); enqueueXml(DEFAULT_REGION, "/describe_images.xml"); enqueueXml(DEFAULT_REGION, "/describe_images_cc.xml"); + enqueueXml(DEFAULT_REGION, "/describe_subnets.xml"); + enqueueXml(DEFAULT_REGION, "/created_securitygroup.xml"); + enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); + enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); + enqueueXml(DEFAULT_REGION, "/authorize_securitygroup_ingress_response.xml"); enqueueXml(DEFAULT_REGION, "/request_spot_instances-ebs.xml"); enqueueXml(DEFAULT_REGION, "/request_spot_instances-ebs.xml"); enqueueXml(DEFAULT_REGION, "/describe_images_ebs.xml"); @@ -61,7 +66,7 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { Template template = computeService.templateBuilder().locationId("us-east-1a").build(); template.getOptions().as(AWSEC2TemplateOptions.class) - .spotPrice(1f).subnetId("subnet-xyz").keyPair("Demo").blockUntilRunning(false); + .spotPrice(1f).subnetId("subnet-9d4a7b6c").keyPair("Demo").blockUntilRunning(false); NodeMetadata node = Iterables.getOnlyElement(computeService.createNodesInGroup("test", 1, template)); assertEquals(node.getId(), "us-east-1/sir-228e6406"); @@ -70,7 +75,12 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { assertPosted(DEFAULT_REGION, "Action=DescribeAvailabilityZones"); assertPosted(DEFAULT_REGION, "Action=DescribeImages&Filter.1.Name=owner-id&Filter.1.Value.1=137112412989&Filter.1.Value.2=801119661308&Filter.1.Value.3=063491364108&Filter.1.Value.4=099720109477&Filter.1.Value.5=411009282317&Filter.2.Name=state&Filter.2.Value.1=available&Filter.3.Name=image-type&Filter.3.Value.1=machine"); assertPosted(DEFAULT_REGION, "Action=DescribeImages&Filter.1.Name=virtualization-type&Filter.1.Value.1=hvm&Filter.2.Name=architecture&Filter.2.Value.1=x86_64&Filter.3.Name=owner-id&Filter.3.Value.1=137112412989&Filter.3.Value.2=099720109477&Filter.4.Name=hypervisor&Filter.4.Value.1=xen&Filter.5.Name=state&Filter.5.Value.1=available&Filter.6.Name=image-type&Filter.6.Value.1=machine&Filter.7.Name=root-device-type&Filter.7.Value.1=ebs"); - assertPosted(DEFAULT_REGION, "Action=RequestSpotInstances&SpotPrice=1.0&InstanceCount=1&LaunchSpecification.ImageId=ami-" + getDefaultImageId() + "&LaunchSpecification.Placement.AvailabilityZone=us-east-1a&LaunchSpecification.InstanceType=" + getDefaultSmallestInstanceType() + "&LaunchSpecification.SubnetId=subnet-xyz&LaunchSpecification.KeyName=Demo&LaunchSpecification.UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK"); + assertPosted(DEFAULT_REGION, "Action=DescribeSubnets&Filter.1.Name=subnet-id&Filter.1.Value.1=subnet-9d4a7b6c"); + assertPosted(DEFAULT_REGION, "Action=CreateSecurityGroup&GroupName=jclouds%23test&GroupDescription=jclouds%23test&VpcId=vpc-1a2b3c4d"); + assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); + assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); + assertPosted(DEFAULT_REGION, "Action=AuthorizeSecurityGroupIngress&GroupId=sg-3c6ef654&IpPermissions.0.IpProtocol=tcp&IpPermissions.0.FromPort=22&IpPermissions.0.ToPort=22&IpPermissions.0.IpRanges.0.CidrIp=0.0.0.0/0&IpPermissions.1.IpProtocol=tcp&IpPermissions.1.FromPort=0&IpPermissions.1.ToPort=65535&IpPermissions.1.Groups.0.UserId=993194456877&IpPermissions.1.Groups.0.GroupId=sg-3c6ef654&IpPermissions.2.IpProtocol=udp&IpPermissions.2.FromPort=0&IpPermissions.2.ToPort=65535&IpPermissions.2.Groups.0.UserId=993194456877&IpPermissions.2.Groups.0.GroupId=sg-3c6ef654"); + assertPosted(DEFAULT_REGION, "Action=RequestSpotInstances&SpotPrice=1.0&InstanceCount=1&LaunchSpecification.ImageId=ami-" + getDefaultImageId() + "&LaunchSpecification.Placement.AvailabilityZone=us-east-1a&LaunchSpecification.SecurityGroupId.1=sg-3c6ef654&LaunchSpecification.InstanceType=" + getDefaultSmallestInstanceType() + "&LaunchSpecification.SubnetId=subnet-9d4a7b6c&LaunchSpecification.KeyName=Demo&LaunchSpecification.UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK"); assertPosted(DEFAULT_REGION, "Action=DescribeSpotInstanceRequests&SpotInstanceRequestId.1=sir-228e6406"); assertPosted(DEFAULT_REGION, "Action=DescribeImages&ImageId.1=ami-595a0a1c"); assertPosted(DEFAULT_REGION, "Action=CreateTags&Tag.1.Key=Name&Tag.1.Value=test-228e6406&ResourceId.1=sir-228e6406"); @@ -86,7 +96,6 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { enqueueXml(DEFAULT_REGION, "/created_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); - enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/authorize_securitygroup_ingress_response.xml"); enqueueXml(DEFAULT_REGION, "/request_spot_instances-ebs.xml"); enqueueXml(DEFAULT_REGION, "/request_spot_instances-ebs.xml"); @@ -110,9 +119,8 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { assertPosted(DEFAULT_REGION, "Action=CreateSecurityGroup&GroupName=jclouds%23test&GroupDescription=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); - assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=AuthorizeSecurityGroupIngress&GroupId=sg-3c6ef654&IpPermissions.0.IpProtocol=tcp&IpPermissions.0.FromPort=22&IpPermissions.0.ToPort=22&IpPermissions.0.IpRanges.0.CidrIp=0.0.0.0/0&IpPermissions.1.IpProtocol=tcp&IpPermissions.1.FromPort=0&IpPermissions.1.ToPort=65535&IpPermissions.1.Groups.0.UserId=993194456877&IpPermissions.1.Groups.0.GroupId=sg-3c6ef654&IpPermissions.2.IpProtocol=udp&IpPermissions.2.FromPort=0&IpPermissions.2.ToPort=65535&IpPermissions.2.Groups.0.UserId=993194456877&IpPermissions.2.Groups.0.GroupId=sg-3c6ef654"); - assertPosted(DEFAULT_REGION, "Action=RequestSpotInstances&SpotPrice=1.0&InstanceCount=1&LaunchSpecification.ImageId=ami-" + getDefaultImageId() + "&LaunchSpecification.Placement.AvailabilityZone=us-east-1a&LaunchSpecification.SecurityGroup.1=jclouds%23test&LaunchSpecification.InstanceType=" + getDefaultSmallestInstanceType() + "&LaunchSpecification.UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&LaunchSpecification.IamInstanceProfile.Arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Ainstance-profile/application_abc/component_xyz/Webserver"); + assertPosted(DEFAULT_REGION, "Action=RequestSpotInstances&SpotPrice=1.0&InstanceCount=1&LaunchSpecification.ImageId=ami-" + getDefaultImageId() + "&LaunchSpecification.Placement.AvailabilityZone=us-east-1a&LaunchSpecification.SecurityGroupId.1=sg-3c6ef654&LaunchSpecification.InstanceType=" + getDefaultSmallestInstanceType() + "&LaunchSpecification.UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&LaunchSpecification.IamInstanceProfile.Arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Ainstance-profile/application_abc/component_xyz/Webserver"); assertPosted(DEFAULT_REGION, "Action=DescribeSpotInstanceRequests&SpotInstanceRequestId.1=sir-228e6406"); assertPosted(DEFAULT_REGION, "Action=DescribeImages&ImageId.1=ami-595a0a1c"); assertPosted(DEFAULT_REGION, "Action=CreateTags&Tag.1.Key=Name&Tag.1.Value=test-228e6406&ResourceId.1=sir-228e6406"); @@ -126,7 +134,6 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { enqueueXml(DEFAULT_REGION, "/created_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); - enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/authorize_securitygroup_ingress_response.xml"); enqueueXml(DEFAULT_REGION, "/request_spot_instances-ebs.xml"); enqueueXml(DEFAULT_REGION, "/request_spot_instances-ebs.xml"); @@ -150,9 +157,8 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { assertPosted(DEFAULT_REGION, "Action=CreateSecurityGroup&GroupName=jclouds%23test&GroupDescription=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); - assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=AuthorizeSecurityGroupIngress&GroupId=sg-3c6ef654&IpPermissions.0.IpProtocol=tcp&IpPermissions.0.FromPort=22&IpPermissions.0.ToPort=22&IpPermissions.0.IpRanges.0.CidrIp=0.0.0.0/0&IpPermissions.1.IpProtocol=tcp&IpPermissions.1.FromPort=0&IpPermissions.1.ToPort=65535&IpPermissions.1.Groups.0.UserId=993194456877&IpPermissions.1.Groups.0.GroupId=sg-3c6ef654&IpPermissions.2.IpProtocol=udp&IpPermissions.2.FromPort=0&IpPermissions.2.ToPort=65535&IpPermissions.2.Groups.0.UserId=993194456877&IpPermissions.2.Groups.0.GroupId=sg-3c6ef654"); - assertPosted(DEFAULT_REGION, "Action=RequestSpotInstances&SpotPrice=1.0&InstanceCount=1&LaunchSpecification.ImageId=ami-" + getDefaultImageId() + "&LaunchSpecification.Placement.AvailabilityZone=us-east-1a&LaunchSpecification.SecurityGroup.1=jclouds%23test&LaunchSpecification.InstanceType=" + getDefaultSmallestInstanceType() + "&LaunchSpecification.UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&LaunchSpecification.IamInstanceProfile.Name=Webserver"); + assertPosted(DEFAULT_REGION, "Action=RequestSpotInstances&SpotPrice=1.0&InstanceCount=1&LaunchSpecification.ImageId=ami-" + getDefaultImageId() + "&LaunchSpecification.Placement.AvailabilityZone=us-east-1a&LaunchSpecification.SecurityGroupId.1=sg-3c6ef654&LaunchSpecification.InstanceType=" + getDefaultSmallestInstanceType() + "&LaunchSpecification.UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&LaunchSpecification.IamInstanceProfile.Name=Webserver"); assertPosted(DEFAULT_REGION, "Action=DescribeSpotInstanceRequests&SpotInstanceRequestId.1=sir-228e6406"); assertPosted(DEFAULT_REGION, "Action=DescribeImages&ImageId.1=ami-595a0a1c"); assertPosted(DEFAULT_REGION, "Action=CreateTags&Tag.1.Key=Name&Tag.1.Value=test-228e6406&ResourceId.1=sir-228e6406"); @@ -166,7 +172,6 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { enqueueXml(DEFAULT_REGION, "/created_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); - enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/authorize_securitygroup_ingress_response.xml"); enqueueXml(DEFAULT_REGION, "/new_instance.xml"); enqueueXml(DEFAULT_REGION, "/describe_instances_running-1.xml"); @@ -186,9 +191,8 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { assertPosted(DEFAULT_REGION, "Action=CreateSecurityGroup&GroupName=jclouds%23test&GroupDescription=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); - assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=AuthorizeSecurityGroupIngress&GroupId=sg-3c6ef654&IpPermissions.0.IpProtocol=tcp&IpPermissions.0.FromPort=22&IpPermissions.0.ToPort=22&IpPermissions.0.IpRanges.0.CidrIp=0.0.0.0/0&IpPermissions.1.IpProtocol=tcp&IpPermissions.1.FromPort=0&IpPermissions.1.ToPort=65535&IpPermissions.1.Groups.0.UserId=993194456877&IpPermissions.1.Groups.0.GroupId=sg-3c6ef654&IpPermissions.2.IpProtocol=udp&IpPermissions.2.FromPort=0&IpPermissions.2.ToPort=65535&IpPermissions.2.Groups.0.UserId=993194456877&IpPermissions.2.Groups.0.GroupId=sg-3c6ef654"); - assertPosted(DEFAULT_REGION, "Action=RunInstances&ImageId=ami-8ce4b5c9&MinCount=1&MaxCount=1&InstanceType=" + getDefaultParavirtualInstanceType() + "&SecurityGroup.1=jclouds%23test&UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&IamInstanceProfile.Arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Ainstance-profile/application_abc/component_xyz/Webserver"); + assertPosted(DEFAULT_REGION, "Action=RunInstances&ImageId=ami-8ce4b5c9&MinCount=1&MaxCount=1&InstanceType=" + getDefaultParavirtualInstanceType() + "&SecurityGroupId.1=sg-3c6ef654&UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&IamInstanceProfile.Arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Ainstance-profile/application_abc/component_xyz/Webserver"); assertPosted(DEFAULT_REGION, "Action=DescribeInstances&InstanceId.1=i-2baa5550"); assertPosted(DEFAULT_REGION, "Action=DescribeImages&ImageId.1=ami-aecd60c7"); assertPosted(DEFAULT_REGION, "Action=CreateTags&Tag.1.Key=Name&Tag.1.Value=test-2baa5550&ResourceId.1=i-2baa5550"); @@ -202,7 +206,6 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { enqueueXml(DEFAULT_REGION, "/created_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); - enqueueXml(DEFAULT_REGION, "/new_securitygroup.xml"); enqueueXml(DEFAULT_REGION, "/authorize_securitygroup_ingress_response.xml"); enqueueXml(DEFAULT_REGION, "/new_instance.xml"); enqueueXml(DEFAULT_REGION, "/describe_instances_running-1.xml"); @@ -222,9 +225,8 @@ public class AWSEC2ComputeServiceApiMockTest extends BaseAWSEC2ApiMockTest { assertPosted(DEFAULT_REGION, "Action=CreateSecurityGroup&GroupName=jclouds%23test&GroupDescription=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); - assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23test"); assertPosted(DEFAULT_REGION, "Action=AuthorizeSecurityGroupIngress&GroupId=sg-3c6ef654&IpPermissions.0.IpProtocol=tcp&IpPermissions.0.FromPort=22&IpPermissions.0.ToPort=22&IpPermissions.0.IpRanges.0.CidrIp=0.0.0.0/0&IpPermissions.1.IpProtocol=tcp&IpPermissions.1.FromPort=0&IpPermissions.1.ToPort=65535&IpPermissions.1.Groups.0.UserId=993194456877&IpPermissions.1.Groups.0.GroupId=sg-3c6ef654&IpPermissions.2.IpProtocol=udp&IpPermissions.2.FromPort=0&IpPermissions.2.ToPort=65535&IpPermissions.2.Groups.0.UserId=993194456877&IpPermissions.2.Groups.0.GroupId=sg-3c6ef654"); - assertPosted(DEFAULT_REGION, "Action=RunInstances&ImageId=ami-8ce4b5c9&MinCount=1&MaxCount=1&InstanceType=" + getDefaultParavirtualInstanceType() + "&SecurityGroup.1=jclouds%23test&UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&IamInstanceProfile.Name=Webserver"); + assertPosted(DEFAULT_REGION, "Action=RunInstances&ImageId=ami-8ce4b5c9&MinCount=1&MaxCount=1&InstanceType=" + getDefaultParavirtualInstanceType() + "&SecurityGroupId.1=sg-3c6ef654&UserData=I2Nsb3VkLWNvbmZpZwpyZXBvX3VwZ3JhZGU6IG5vbmUK&IamInstanceProfile.Name=Webserver"); assertPosted(DEFAULT_REGION, "Action=DescribeInstances&InstanceId.1=i-2baa5550"); assertPosted(DEFAULT_REGION, "Action=DescribeImages&ImageId.1=ami-aecd60c7"); assertPosted(DEFAULT_REGION, "Action=CreateTags&Tag.1.Key=Name&Tag.1.Value=test-2baa5550&ResourceId.1=i-2baa5550"); diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionApiMockTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionApiMockTest.java index 1bee5d55c6..24c91e2ed3 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionApiMockTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionApiMockTest.java @@ -141,7 +141,6 @@ public class AWSEC2SecurityGroupExtensionApiMockTest extends BaseAWSEC2ApiMockTe enqueueXml(DEFAULT_REGION, "/describe_securitygroups_extension_single.xml"); enqueueXml(DEFAULT_REGION, "/describe_securitygroups_extension_single.xml"); enqueueXml(DEFAULT_REGION, "/describe_securitygroups_extension_single.xml"); - enqueueXml(DEFAULT_REGION, "/describe_securitygroups_extension_single.xml"); enqueueXml(DEFAULT_REGION, "/availabilityZones.xml"); SecurityGroup newGroup = extension() @@ -156,7 +155,6 @@ public class AWSEC2SecurityGroupExtensionApiMockTest extends BaseAWSEC2ApiMockTe "Action=CreateSecurityGroup&GroupName=jclouds%23some-group&GroupDescription=jclouds%23some-group"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23some-group"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23some-group"); - assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupName.1=jclouds%23some-group"); assertPosted(DEFAULT_REGION, "Action=DescribeSecurityGroups&GroupId.1=sg-3c6ef654"); assertPosted(DEFAULT_REGION, "Action=DescribeAvailabilityZones"); } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeededTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeededTest.java index d1691a1956..bf87309dbb 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeededTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/loaders/AWSEC2CreateSecurityGroupIfNeededTest.java @@ -27,6 +27,7 @@ import java.util.Set; import java.util.concurrent.ExecutionException; import org.jclouds.aws.ec2.features.AWSSecurityGroupApi; +import org.jclouds.aws.ec2.options.CreateSecurityGroupOptions; import org.jclouds.ec2.compute.domain.RegionAndName; import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules; import org.jclouds.ec2.compute.functions.EC2SecurityGroupIdFromName; @@ -76,9 +77,10 @@ public class AWSEC2CreateSecurityGroupIfNeededTest { .tenantIdGroupNamePair("ownerId", "sg-123456") .build()); - client.createSecurityGroupInRegion("region", "group", "group"); + expect( + client.createSecurityGroupInRegionAndReturnId("region", "group", "group", + new CreateSecurityGroupOptions().vpcId("vpc"))).andReturn("sg-123456"); expect(group.getOwnerId()).andReturn("ownerId"); - expect(groupIdFromName.apply("region/group")).andReturn("sg-123456"); client.authorizeSecurityGroupIngressInRegion("region", "sg-123456", permissions.build()); expect(client.describeSecurityGroupsInRegion("region", "group")).andReturn(Set.class.cast(groups)); @@ -89,7 +91,7 @@ public class AWSEC2CreateSecurityGroupIfNeededTest { AWSEC2CreateSecurityGroupIfNeeded function = new AWSEC2CreateSecurityGroupIfNeeded(client, groupIdFromName, tester); - assertEquals("group", function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true))); + assertEquals("sg-123456", function.load(new RegionNameAndIngressRules("region", "group", new int[] { 22 }, true, "vpc"))); verify(client); verify(group); diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java index 4b5b9d46fa..27f20b1863 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/strategy/CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsTest.java @@ -35,6 +35,7 @@ import java.util.concurrent.ConcurrentMap; import javax.inject.Provider; import org.jclouds.aws.domain.Region; +import org.jclouds.aws.ec2.AWSEC2Api; import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions; import org.jclouds.aws.ec2.domain.PlacementGroup; import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial; @@ -95,7 +96,8 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT "createNewPlacementGroupUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( - "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + "getSecurityGroupsForTagAndOptions", String.class, String.class, String.class, + TemplateOptions.class) }); AWSEC2TemplateOptions options = createMock(AWSEC2TemplateOptions.class); Template template = createMock(Template.class); @@ -107,7 +109,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getBlockDeviceMappings()).andReturn(ImmutableSet. of()).atLeastOnce(); expect(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options)).andReturn( systemGeneratedKeyPairName); - expect(strategy.getSecurityGroupsForTagAndOptions(region, group, options)).andReturn(generatedGroups); + expect(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options)).andReturn(generatedGroups); expect(options.getGroupIds()).andReturn(ImmutableSet. of()); expect(options.getSubnetId()).andReturn(null); expect(options.getUserData()).andReturn(null); @@ -157,7 +159,8 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT "createNewPlacementGroupUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( - "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + "getSecurityGroupsForTagAndOptions", String.class, String.class, String.class, + TemplateOptions.class) }); AWSEC2TemplateOptions options = createMock(AWSEC2TemplateOptions.class); Template template = createMock(Template.class); @@ -171,7 +174,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT systemGeneratedKeyPairName); expect(strategy.createNewPlacementGroupUnlessUserSpecifiedOtherwise(region, group, options)).andReturn( generatedGroup); - expect(strategy.getSecurityGroupsForTagAndOptions(region, group, options)).andReturn(generatedGroups); + expect(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options)).andReturn(generatedGroups); expect(options.getGroupIds()).andReturn(ImmutableSet. of()); expect(options.getSubnetId()).andReturn(null); expect(options.getUserData()).andReturn(null); @@ -222,7 +225,8 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT "createNewPlacementGroupUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( - "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + "getSecurityGroupsForTagAndOptions", String.class, String.class, String.class, + TemplateOptions.class) }); AWSEC2TemplateOptions options = createMock(AWSEC2TemplateOptions.class); Template template = createMock(Template.class); @@ -236,7 +240,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT systemGeneratedKeyPairName); expect(strategy.createNewPlacementGroupUnlessUserSpecifiedOtherwise(region, group, options)).andReturn( generatedGroup); - expect(strategy.getSecurityGroupsForTagAndOptions(region, group, options)).andReturn(generatedGroups); + expect(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options)).andReturn(generatedGroups); expect(options.getGroupIds()).andReturn(ImmutableSet. of()); expect(options.getSubnetId()).andReturn(null); expect(options.getUserData()).andReturn(null); @@ -285,7 +289,10 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT "createNewPlacementGroupUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( - "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + "getSecurityGroupsForTagAndOptions", String.class, String.class, String.class, + TemplateOptions.class), + CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( + "vpcIdForSubnet", String.class)}); AWSEC2TemplateOptions options = createMock(AWSEC2TemplateOptions.class); Template template = createMock(Template.class); @@ -301,6 +308,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getSubnetId()).andReturn("1"); expect(options.getUserData()).andReturn(null); expect(options.isMonitoringEnabled()).andReturn(false); + expect(strategy.vpcIdForSubnet("1")).andReturn("vpc1"); // replay mocks replay(options); @@ -346,7 +354,8 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT "createNewPlacementGroupUnlessUserSpecifiedOtherwise", String.class, String.class, TemplateOptions.class), CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptions.class.getDeclaredMethod( - "getSecurityGroupsForTagAndOptions", String.class, String.class, TemplateOptions.class) }); + "getSecurityGroupsForTagAndOptions", String.class, String.class, String.class, + TemplateOptions.class) }); AWSEC2TemplateOptions options = createMock(AWSEC2TemplateOptions.class); Template template = createMock(Template.class); @@ -358,7 +367,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getBlockDeviceMappings()).andReturn(ImmutableSet. of()).atLeastOnce(); expect(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options)).andReturn( systemGeneratedKeyPairName); - expect(strategy.getSecurityGroupsForTagAndOptions(region, group, options)).andReturn(generatedGroups); + expect(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options)).andReturn(generatedGroups); expect(options.getGroupIds()).andReturn(ImmutableSet. of()); expect(options.getSubnetId()).andReturn(null); expect(options.getUserData()).andReturn("hello".getBytes()); @@ -654,7 +663,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT Set groupNames = ImmutableSet. of(); int[] ports = {}; boolean shouldAuthorizeSelf = true; - Set returnVal = ImmutableSet. of(generatedMarkerGroup); + Set returnVal = ImmutableSet. of("sg-12345"); // create mocks CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy(); @@ -665,15 +674,15 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getGroups()).andReturn(groupNames).atLeastOnce(); expect(options.getInboundPorts()).andReturn(ports).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); - expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn(group); + ports, shouldAuthorizeSelf, null); + expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn("sg-12345"); // replay mocks replay(options); replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -688,7 +697,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT Set groupNames = ImmutableSet. of(); int[] ports = { 22, 80 }; boolean shouldAuthorizeSelf = true; - Set returnVal = ImmutableSet. of(generatedMarkerGroup); + Set returnVal = ImmutableSet. of("sg-12345"); // create mocks CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy(); @@ -699,15 +708,15 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getGroups()).andReturn(groupNames).atLeastOnce(); expect(options.getInboundPorts()).andReturn(ports).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); - expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn(generatedMarkerGroup); + ports, shouldAuthorizeSelf, null); + expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn("sg-12345"); // replay mocks replay(options); replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -722,7 +731,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT Set groupNames = ImmutableSet. of(); int[] ports = {}; boolean shouldAuthorizeSelf = true; - Set returnVal = ImmutableSet. of(generatedMarkerGroup); + Set returnVal = ImmutableSet. of("sg-12345"); // create mocks CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy(); @@ -733,15 +742,15 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getGroups()).andReturn(groupNames).atLeastOnce(); expect(options.getInboundPorts()).andReturn(ports).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); - expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn(generatedMarkerGroup); + ports, shouldAuthorizeSelf, null); + expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)).andReturn("sg-12345"); // replay mocks replay(options); replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -757,7 +766,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT int[] ports = {}; boolean shouldAuthorizeSelf = true; boolean groupExisted = true; - Set returnVal = ImmutableSet. of(generatedMarkerGroup, "group1", "group2"); + Set returnVal = ImmutableSet. of("sg-12345", "group1", "group2"); // create mocks CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy(); @@ -767,17 +776,17 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getGroupIds()).andReturn(ImmutableSet. of()); expect(options.getGroups()).andReturn(groupNames).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); + ports, shouldAuthorizeSelf, null); expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)) - .andReturn(groupExisted ? "group" : null); + .andReturn(groupExisted ? "sg-12345" : null); // replay mocks replay(options); replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -793,7 +802,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT int[] ports = {}; boolean shouldAuthorizeSelf = true; boolean groupExisted = true; - Set returnVal = ImmutableSet. of(generatedMarkerGroup); + Set returnVal = ImmutableSet. of("sg-12345"); // create mocks CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy(); @@ -803,17 +812,17 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT expect(options.getGroupIds()).andReturn(ImmutableSet. of("group1", "group2")); expect(options.getGroups()).andReturn(groupNames).atLeastOnce(); RegionNameAndIngressRules regionNameAndIngressRules = new RegionNameAndIngressRules(region, generatedMarkerGroup, - ports, shouldAuthorizeSelf); + ports, shouldAuthorizeSelf, null); expect(strategy.securityGroupMap.getUnchecked(regionNameAndIngressRules)) - .andReturn(groupExisted ? "group" : null); + .andReturn(groupExisted ? "sg-12345" : null); // replay mocks replay(options); replayStrategy(strategy); // run - assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, options), returnVal); + assertEquals(strategy.getSecurityGroupsForTagAndOptions(region, group, null, options), returnVal); // verify mocks verify(options); @@ -931,6 +940,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT CreatePlacementGroupIfNeeded createPlacementGroupIfNeeded = createMock(CreatePlacementGroupIfNeeded.class); GroupNamingConvention.Factory namingConventionFactory = createMock(GroupNamingConvention.Factory.class); GroupNamingConvention namingConvention = createMock(GroupNamingConvention.class); + AWSEC2Api api = createMock(AWSEC2Api.class); expect(namingConventionFactory.create()).andReturn(namingConvention).anyTimes(); expect(namingConvention.sharedNameForGroup("group")).andReturn("jclouds#group").anyTimes(); expect(makeKeyPair.apply(anyObject(RegionAndName.class))).andReturn(null).anyTimes(); @@ -939,7 +949,7 @@ public class CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptionsT return new CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions(makeKeyPair, credentialsMap, securityGroupMap, OPTIONS_PROVIDER, placementGroupMap, createPlacementGroupIfNeeded, importExistingKeyPair, - namingConventionFactory); + namingConventionFactory, api); } private void replayStrategy(CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy) {