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 3b04335218..d159957048 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 @@ -22,11 +22,16 @@ import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.collect.Iterables.transform; -import java.util.Set; - import javax.inject.Inject; import javax.inject.Named; +import java.util.Set; +import com.google.common.base.Function; +import com.google.common.base.Supplier; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.Iterables; +import com.google.common.collect.Multimap; +import com.google.common.util.concurrent.ListeningExecutorService; import org.jclouds.Constants; import org.jclouds.aws.ec2.AWSEC2Api; import org.jclouds.aws.util.AWSUtils; @@ -41,13 +46,6 @@ import org.jclouds.location.Region; import org.jclouds.net.domain.IpPermission; import org.jclouds.net.domain.IpProtocol; -import com.google.common.base.Function; -import com.google.common.base.Supplier; -import com.google.common.cache.LoadingCache; -import com.google.common.collect.Iterables; -import com.google.common.collect.Multimap; -import com.google.common.util.concurrent.ListeningExecutorService; - /** * An extension to compute service to allow for the manipulation of {@link SecurityGroup}s. Implementation * is optional by providers. @@ -79,7 +77,7 @@ public class AWSEC2SecurityGroupExtension extends EC2SecurityGroupExtension { false); groupCreator.getUnchecked(regionAndName); - String groupId = groupNameToId.apply(markerGroup); + String groupId = groupNameToId.apply(regionAndName.slashEncode()); return getSecurityGroupById(regionAndName.getRegion() + "/" + groupId); } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionExpectTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionExpectTest.java index f13737a10e..40558094ae 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionExpectTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/extensions/AWSEC2SecurityGroupExtensionExpectTest.java @@ -20,10 +20,17 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; +import javax.ws.rs.core.MediaType; import java.util.Set; -import javax.ws.rs.core.MediaType; - +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; import org.jclouds.aws.ec2.compute.internal.BaseAWSEC2ComputeServiceExpectTest; import org.jclouds.compute.domain.SecurityGroup; import org.jclouds.compute.domain.SecurityGroupBuilder; @@ -36,15 +43,6 @@ import org.jclouds.net.domain.IpPermission; import org.jclouds.net.domain.IpProtocol; import org.testng.annotations.Test; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMap.Builder; -import com.google.common.collect.ImmutableMultimap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; -import com.google.common.collect.LinkedHashMultimap; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; - /** * * @author Andrew Bayer @@ -354,6 +352,62 @@ public class AWSEC2SecurityGroupExtensionExpectTest extends BaseAWSEC2ComputeSer assertTrue(newPerm.getTenantIdGroupNamePairs().values().contains(origGroup.getProviderId())); } + public void testCreateSecurityGroup() { + HttpRequest createSecurityGroupExtRequest = + formSigner.filter(HttpRequest.builder() + .method("POST") + .endpoint("https://ec2." + region + ".amazonaws.com/") + .addHeader("Host", "ec2." + region + ".amazonaws.com") + .addFormParam("Action", "CreateSecurityGroup") + .addFormParam("GroupDescription", "jclouds#some-group") + .addFormParam("GroupName", "jclouds#some-group").build()); + + HttpRequest describeSecurityGroupsSingleRequest = + formSigner.filter(HttpRequest.builder() + .method("POST") + .endpoint("https://ec2." + region + ".amazonaws.com/") + .addHeader("Host", "ec2." + region + ".amazonaws.com") + .addFormParam("Action", "DescribeSecurityGroups") + .addFormParam("GroupName.1", "jclouds#some-group").build()); + + HttpRequest describeSecurityGroupsByIdRequest = + formSigner.filter(HttpRequest.builder() + .method("POST") + .endpoint("https://ec2." + region + ".amazonaws.com/") + .addHeader("Host", "ec2." + region + ".amazonaws.com") + .addFormParam("Action", "DescribeSecurityGroups") + .addFormParam("GroupId.1", "sg-3c6ef654").build()); + + HttpResponse describeSecurityGroupsSingleResponse = + HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType( + "/describe_securitygroups_extension_single.xml", MediaType.APPLICATION_XML)).build(); + + + Builder requestResponseMap = ImmutableMap. builder(); + requestResponseMap.put(describeRegionsRequest, describeRegionsResponse); + requestResponseMap.put(describeAvailabilityZonesRequest, describeAvailabilityZonesResponse); + requestResponseMap.put(describeSecurityGroupsSingleRequest, describeSecurityGroupsSingleResponse); + requestResponseMap.put(describeSecurityGroupsByIdRequest, describeSecurityGroupsSingleResponse); + requestResponseMap.put(createKeyPairRequest, createKeyPairResponse); + requestResponseMap.put(createSecurityGroupExtRequest, createSecurityGroupResponse); + + requestResponseMap.put(authorizeSecurityGroupIngressRequest22, authorizeSecurityGroupIngressResponse); + requestResponseMap.put(authorizeSecurityGroupIngressRequestGroup, authorizeSecurityGroupIngressResponse); + + + SecurityGroupExtension extension = requestsSendResponses(requestResponseMap.build()).getSecurityGroupExtension().get(); + + SecurityGroup group = extension.createSecurityGroup("some-group", new LocationBuilder() + .scope(LocationScope.REGION) + .id(region) + .description("region") + .build()); + + assertEquals("sg-3c6ef654", group.getProviderId()); + assertEquals(region + "/sg-3c6ef654", group.getId()); + } + private Multimap emptyMultimap() { return LinkedHashMultimap.create(); }