Issue: LIBCLOUD-463 Add VPC support for security group creation (EC2 driver)

Signed-off-by: Tomaz Muraus <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/8c382875
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/8c382875
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/8c382875

Branch: refs/heads/trunk
Commit: 8c38287512a7bbc5f253c36d57e06f2c8741205e
Parents: 7269120
Author: Chris DeRamus <[email protected]>
Authored: Fri Dec 20 07:23:37 2013 -0500
Committer: Tomaz Muraus <[email protected]>
Committed: Fri Dec 20 15:43:03 2013 +0100

----------------------------------------------------------------------
 libcloud/compute/drivers/ec2.py                 | 28 +++++++++++++-------
 .../fixtures/ec2/create_security_group.xml      |  5 ++++
 libcloud/test/compute/test_ec2.py               | 11 ++++++++
 3 files changed, 35 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/8c382875/libcloud/compute/drivers/ec2.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py
index bb2d4e7..2f8dcfe 100644
--- a/libcloud/compute/drivers/ec2.py
+++ b/libcloud/compute/drivers/ec2.py
@@ -1312,26 +1312,36 @@ class BaseEC2NodeDriver(NodeDriver):
 
         return groups
 
-    def ex_create_security_group(self, name, description):
+    def ex_create_security_group(self, name, description, vpc_id=None):
         """
-        Creates a new Security Group
+        Creates a new Security Group in EC2-Classic or a targetted VPC
 
-        @note: This is a non-standard extension API, and only works for EC2.
-
-        :param      name: The name of the security group to Create.
-                          This must be unique.
-        :type       name: ``str``
+        :param      name:        The name of the security group to Create.
+                                 This must be unique.
+        :type       name:        ``str``
 
         :param      description: Human readable description of a Security
         Group.
         :type       description: ``str``
 
-        :rtype: ``str``
+        :param      description: Optional identifier for VPC networks
+        :type       description: ``str``
+
+        :rtype: ``dict``
         """
         params = {'Action': 'CreateSecurityGroup',
                   'GroupName': name,
                   'GroupDescription': description}
-        return self.connection.request(self.path, params=params).object
+
+        if vpc_id is not None:
+            params['VpcId'] = vpc_id
+
+        response = self.connection.request(self.path, params=params).object
+        group_id = findattr(element=response, xpath='groupId',
+                            namespace=NAMESPACE)
+        return {
+            'group_id': group_id
+        }
 
     def ex_authorize_security_group(self, name, from_port, to_port, cidr_ip,
                                     protocol='tcp'):

http://git-wip-us.apache.org/repos/asf/libcloud/blob/8c382875/libcloud/test/compute/fixtures/ec2/create_security_group.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/fixtures/ec2/create_security_group.xml 
b/libcloud/test/compute/fixtures/ec2/create_security_group.xml
new file mode 100644
index 0000000..f714eb6
--- /dev/null
+++ b/libcloud/test/compute/fixtures/ec2/create_security_group.xml
@@ -0,0 +1,5 @@
+<CreateSecurityGroupResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/";>
+  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
+  <return>true</return>
+  <groupId>sg-52e2f530</groupId>
+</CreateSecurityGroupResponse>

http://git-wip-us.apache.org/repos/asf/libcloud/blob/8c382875/libcloud/test/compute/test_ec2.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_ec2.py 
b/libcloud/test/compute/test_ec2.py
index 14cae57..4364d69 100644
--- a/libcloud/test/compute/test_ec2.py
+++ b/libcloud/test/compute/test_ec2.py
@@ -708,6 +708,13 @@ class EC2Tests(LibcloudTestCase, TestCaseMixin):
                     'max-elastic-ips': 5}
         self.assertEqual(limits['resource'], expected)
 
+    def test_ex_create_security_group(self):
+        group = self.driver.ex_create_security_group("WebServers",
+                                                     "Rules to protect web 
nodes",
+                                                     "vpc-143cab4")
+
+        self.assertEqual(group["group_id"], "sg-52e2f530")
+
 
 class EC2USWest1Tests(EC2Tests):
     region = 'us-west-1'
@@ -979,6 +986,10 @@ class EC2MockHttp(MockHttpTestCase):
         body = self.fixtures.load('describe_account_attributes.xml')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def _CreateSecurityGroup(self, method, url, body, headers):
+        body = self.fixtures.load('create_security_group.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
 
 class EucMockHttp(EC2MockHttp):
     fixtures = ComputeFileFixtures('ec2')

Reply via email to