← Back to team overview

txaws-dev team mailing list archive

[Merge] lp:~franciscosouza/txaws/txaws-authorizerevokesecgroupid into lp:txaws

 

Francisco Souza has proposed merging lp:~franciscosouza/txaws/txaws-authorizerevokesecgroupid into lp:txaws.

Requested reviews:
  txAWS Committers (txaws-dev)

For more details, see:
https://code.launchpad.net/~franciscosouza/txaws/txaws-authorizerevokesecgroupid/+merge/133961

ec2/client: accept id in authorize and revoke

When using VPC, security groups are identified by ID, not name.

https://codereview.appspot.com/6814123/

-- 
https://code.launchpad.net/~franciscosouza/txaws/txaws-authorizerevokesecgroupid/+merge/133961
Your team txAWS Committers is requested to review the proposed merge of lp:~franciscosouza/txaws/txaws-authorizerevokesecgroupid into lp:txaws.
=== modified file 'txaws/ec2/client.py'
--- txaws/ec2/client.py	2012-05-05 00:17:02 +0000
+++ txaws/ec2/client.py	2012-11-12 17:14:22 +0000
@@ -139,7 +139,7 @@
         return d.addCallback(self.parser.truth_return)
 
     def authorize_security_group(
-        self, group_name, source_group_name="", source_group_owner_id="",
+        self, group_name=None, group_id=None, source_group_name="", source_group_owner_id="",
         ip_protocol="", from_port="", to_port="", cidr_ip=""):
         """
         There are two ways to use C{authorize_security_group}:
@@ -150,6 +150,8 @@
 
         @param group_name: The group you will be modifying with a new
             authorization.
+        @param group_id: The id of the group you will be modifying with
+            a new authorization.
 
         Optionally, the following parameters:
         @param source_group_name: Name of security group to authorize access to
@@ -188,7 +190,12 @@
             msg = ("You must specify either both group parameters or "
                    "all the ip parameters.")
             raise ValueError(msg)
-        parameters["GroupName"] = group_name
+        if group_id:
+            parameters["GroupId"] = group_id
+        elif group_name:
+            parameters["GroupName"] = group_name
+        else:
+            raise ValueError("You must specify either the group name of the group id.")
         query = self.query_factory(
             action="AuthorizeSecurityGroupIngress", creds=self.creds,
             endpoint=self.endpoint, other_params=parameters)
@@ -224,7 +231,7 @@
         return d
 
     def revoke_security_group(
-        self, group_name, source_group_name="", source_group_owner_id="",
+        self, group_name=None, group_id=None, source_group_name="", source_group_owner_id="",
         ip_protocol="", from_port="", to_port="", cidr_ip=""):
         """
         There are two ways to use C{revoke_security_group}:
@@ -273,7 +280,12 @@
             msg = ("You must specify either both group parameters or "
                    "all the ip parameters.")
             raise ValueError(msg)
-        parameters["GroupName"] = group_name
+        if group_id:
+            parameters["GroupId"] = group_id
+        elif group_name:
+            parameters["GroupName"] = group_name
+        else:
+            raise ValueError("You must specify either the group name of the group id.")
         query = self.query_factory(
             action="RevokeSecurityGroupIngress", creds=self.creds,
             endpoint=self.endpoint, other_params=parameters)

=== modified file 'txaws/ec2/tests/test_client.py'
--- txaws/ec2/tests/test_client.py	2012-03-02 22:00:10 +0000
+++ txaws/ec2/tests/test_client.py	2012-11-12 17:14:22 +0000
@@ -676,9 +676,42 @@
         creds = AWSCredentials("foo", "bar")
         ec2 = client.EC2Client(creds, query_factory=StubQuery)
         d = ec2.authorize_security_group(
-            "WebServers", source_group_name="AppServers",
-            source_group_owner_id="123456789123")
-        return self.assertTrue(d)
+            group_name="WebServers", source_group_name="AppServers",
+            source_group_owner_id="123456789123")
+        return self.assertTrue(d)
+
+    def test_authorize_security_group_using_group_id(self):
+        class StubQuery(object):
+
+            def __init__(stub, action="", creds=None, endpoint=None,
+                         other_params={}):
+                self.assertEqual(action, "AuthorizeSecurityGroupIngress")
+                self.assertEqual(creds.access_key, "foo")
+                self.assertEqual(creds.secret_key, "bar")
+                self.assertEqual(other_params, {
+                    "GroupId": "sg-a1b2c3d4e5f6",
+                    "SourceSecurityGroupName": "AppServers",
+                    "SourceSecurityGroupOwnerId": "123456789123",
+                    })
+
+            def submit(self):
+                return succeed(payload.sample_authorize_security_group)
+
+        creds = AWSCredentials("foo", "bar")
+        ec2 = client.EC2Client(creds, query_factory=StubQuery)
+        d = ec2.authorize_security_group(
+            group_id="sg-a1b2c3d4e5f6", source_group_name="AppServers",
+            source_group_owner_id="123456789123")
+        return self.assertTrue(d)
+
+    def test_authorize_security_group_without_group_id_and_group_name(self):
+        creds = AWSCredentials("foo", "bar")
+        ec2 = client.EC2Client(creds)
+        error = self.assertRaises(ValueError, ec2.authorize_security_group,
+                source_group_name="AppServers", source_group_owner_id="123456789123")
+        self.assertEquals(
+            str(error),
+            "You must specify either the group name of the group id.")
 
     def test_authorize_security_group_with_ip_permissions(self):
         """
@@ -707,7 +740,7 @@
         creds = AWSCredentials("foo", "bar")
         ec2 = client.EC2Client(creds, query_factory=StubQuery)
         d = ec2.authorize_security_group(
-            "WebServers", ip_protocol="tcp", from_port="22", to_port="80",
+            group_name="WebServers", ip_protocol="tcp", from_port="22", to_port="80",
             cidr_ip="0.0.0.0/0")
         return self.assertTrue(d)
 
@@ -722,16 +755,12 @@
         """
         creds = AWSCredentials("foo", "bar")
         ec2 = client.EC2Client(creds)
-        self.assertRaises(ValueError, ec2.authorize_security_group,
-                "WebServers", ip_protocol="tcp", from_port="22")
-        try:
-            ec2.authorize_security_group(
-                "WebServers", ip_protocol="tcp", from_port="22")
-        except Exception, error:
-            self.assertEquals(
-                str(error),
-                ("You must specify either both group parameters or all the "
-                 "ip parameters."))
+        error = self.assertRaises(ValueError, ec2.authorize_security_group,
+                group_name="WebServers", ip_protocol="tcp", from_port="22")
+        self.assertEquals(
+            str(error),
+            ("You must specify either both group parameters or all the "
+             "ip parameters."))
 
     def test_authorize_group_permission(self):
         """
@@ -822,6 +851,30 @@
             source_group_owner_id="123456789123")
         return self.assertTrue(d)
 
+    def test_revoke_security_group_using_group_id(self):
+        class StubQuery(object):
+
+            def __init__(stub, action="", creds=None, endpoint=None,
+                         other_params={}):
+                self.assertEqual(action, "RevokeSecurityGroupIngress")
+                self.assertEqual(creds.access_key, "foo")
+                self.assertEqual(creds.secret_key, "bar")
+                self.assertEqual(other_params, {
+                    "GroupId": "sg-a1a1a1",
+                    "SourceSecurityGroupName": "AppServers",
+                    "SourceSecurityGroupOwnerId": "123456789123",
+                    })
+
+            def submit(self):
+                return succeed(payload.sample_revoke_security_group)
+
+        creds = AWSCredentials("foo", "bar")
+        ec2 = client.EC2Client(creds, query_factory=StubQuery)
+        d = ec2.revoke_security_group(
+            group_id="sg-a1a1a1", source_group_name="AppServers",
+            source_group_owner_id="123456789123")
+        return self.assertTrue(d)
+
     def test_revoke_security_group_with_ip_permissions(self):
         """
         L{EC2Client.revoke_security_group} returns a C{Deferred} that
@@ -853,6 +906,15 @@
             cidr_ip="0.0.0.0/0")
         return self.assertTrue(d)
 
+    def test_revoke_security_group_without_group_id_and_group_name(self):
+        creds = AWSCredentials("foo", "bar")
+        ec2 = client.EC2Client(creds)
+        error = self.assertRaises(ValueError, ec2.revoke_security_group,
+                source_group_name="AppServers", source_group_owner_id="123456789123")
+        self.assertEquals(
+            str(error),
+            "You must specify either the group name of the group id.")
+
     def test_revoke_security_group_with_missing_parameters(self):
         """
         L{EC2Client.revoke_security_group} returns a C{Deferred} that
@@ -864,16 +926,12 @@
         """
         creds = AWSCredentials("foo", "bar")
         ec2 = client.EC2Client(creds)
-        self.assertRaises(ValueError, ec2.authorize_security_group,
-                "WebServers", ip_protocol="tcp", from_port="22")
-        try:
-            ec2.authorize_security_group(
-                "WebServers", ip_protocol="tcp", from_port="22")
-        except Exception, error:
-            self.assertEquals(
-                str(error),
-                ("You must specify either both group parameters or all the "
-                 "ip parameters."))
+        error = self.assertRaises(ValueError, ec2.revoke_security_group,
+                group_name="WebServers", ip_protocol="tcp", from_port="22")
+        self.assertEquals(
+            str(error),
+            ("You must specify either both group parameters or all the "
+             "ip parameters."))
 
     def test_revoke_group_permission(self):
         """


Follow ups