You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2014/05/20 13:44:57 UTC

git commit: Allow user to pass filters to ex_list_networks method in the EC2 driver.

Repository: libcloud
Updated Branches:
  refs/heads/trunk 02854546c -> 819f1671f


Allow user to pass filters to ex_list_networks method in the EC2 driver.

Tomaz: Fix tests and lint.

Closes #294

Signed-off-by: Tomaz Muraus <to...@apache.org>


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

Branch: refs/heads/trunk
Commit: 819f1671ff7aeccc8996b2d77a98be4b72ed20c9
Parents: 0285454
Author: Lior Goikhburg <go...@gmail.com>
Authored: Thu May 15 21:12:01 2014 +0400
Committer: Tomaz Muraus <to...@apache.org>
Committed: Tue May 20 13:39:11 2014 +0200

----------------------------------------------------------------------
 CHANGES.rst                       |  4 +++
 libcloud/compute/drivers/ec2.py   | 35 ++++++++++++++++++++++++-
 libcloud/test/compute/test_ec2.py | 48 ++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/819f1671/CHANGES.rst
----------------------------------------------------------------------
diff --git a/CHANGES.rst b/CHANGES.rst
index 73bf6b6..ff51f61 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -146,6 +146,10 @@ Compute
   (GITHUB-295, LIBCLOUD-555)
   [syndicut]
 
+- Allow user to pass filters to ex_list_networks method in the EC2 driver.
+  (GITHUB-294)
+  [zerthimon]
+
 Storage
 ~~~~~~~
 

http://git-wip-us.apache.org/repos/asf/libcloud/blob/819f1671/libcloud/compute/drivers/ec2.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py
index 9962b2d..1f94d79 100644
--- a/libcloud/compute/drivers/ec2.py
+++ b/libcloud/compute/drivers/ec2.py
@@ -2342,15 +2342,48 @@ class BaseEC2NodeDriver(NodeDriver):
         )
         return image
 
-    def ex_list_networks(self):
+    def ex_list_networks(self, network_ids=None, filters=None):
         """
         Return a list of :class:`EC2Network` objects for the
         current region.
 
+        :param      network_ids: Return only networks matching the provided
+                                 network IDs. If not specified, a list of all
+                                 the networks in the corresponding region
+                                 is returned.
+        :type       network_ids: ``list``
+
+        :param      filters: The filters so that the response includes
+                             information for only certain networks.
+        :type       filters: ``dict``
+
         :rtype:     ``list`` of :class:`EC2Network`
         """
         params = {'Action': 'DescribeVpcs'}
 
+        if network_ids:
+            for network_idx, network_id in enumerate(network_ids):
+                network_idx += 1  # We want 1-based indexes
+                network_key = 'VpcId.%s' % network_idx
+                params[network_key] = network_id
+
+        if filters:
+            for filter_idx, filter_data in enumerate(filters.items()):
+                filter_idx += 1  # We want 1-based indexes
+                filter_name, filter_values = filter_data
+                filter_key = 'Filter.%s.Name' % filter_idx
+                params[filter_key] = filter_name
+
+                if isinstance(filter_values, (list, tuple)):
+                    for value_idx, value in enumerate(filter_values):
+                        value_idx += 1  # We want 1-based indexes
+                        value_key = 'Filter.%s.Value.%s' % (filter_idx,
+                                                            value_idx)
+                        params[value_key] = value
+                else:
+                    value_key = 'Filter.%s.Value.1' % filter_idx
+                    params[value_key] = filter_values
+
         return self._to_networks(
             self.connection.request(self.path, params=params).object
         )

http://git-wip-us.apache.org/repos/asf/libcloud/blob/819f1671/libcloud/test/compute/test_ec2.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_ec2.py b/libcloud/test/compute/test_ec2.py
index 0a2cf1e..3f531cd 100644
--- a/libcloud/test/compute/test_ec2.py
+++ b/libcloud/test/compute/test_ec2.py
@@ -919,6 +919,21 @@ class EC2Tests(LibcloudTestCase, TestCaseMixin):
         self.assertEqual('available', vpcs[1].extra['state'])
         self.assertEqual('dopt-7eded312', vpcs[1].extra['dhcp_options_id'])
 
+    def test_ex_list_networks_network_ids(self):
+        EC2MockHttp.type = 'network_ids'
+        network_ids = ['vpc-532335e1']
+
+        # We assert in the mock http method
+        self.driver.ex_list_networks(network_ids=network_ids)
+
+    def test_ex_list_networks_filters(self):
+        EC2MockHttp.type = 'filters'
+        filters = {'dhcp-options-id': 'dopt-7eded312',  # matches two networks
+                   'cidr': '192.168.51.0/24'}  # matches one network
+
+        # We assert in the mock http method
+        self.driver.ex_list_networks(filters=filters)
+
     def test_ex_create_network(self):
         vpc = self.driver.ex_create_network('192.168.55.0/24',
                                             name='Test VPC',
@@ -1349,6 +1364,39 @@ class EC2MockHttp(MockHttpTestCase):
         body = self.fixtures.load('describe_vpcs.xml')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def _network_ids_DescribeVpcs(self, method, url, body, headers):
+        expected_params = {
+            'VpcId.1': 'vpc-532335e1'
+        }
+        self.assertUrlContainsQueryParams(url, expected_params)
+
+        body = self.fixtures.load('describe_vpcs.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _filters_DescribeVpcs(self, method, url, body, headers):
+        expected_params_1 = {
+            'Filter.1.Name': 'dhcp-options-id',
+            'Filter.1.Value.1': 'dopt-7eded312',
+            'Filter.2.Name': 'cidr',
+            'Filter.2.Value.1': '192.168.51.0/24'
+        }
+
+        expected_params_2 = {
+            'Filter.1.Name': 'cidr',
+            'Filter.1.Value.1': '192.168.51.0/24',
+            'Filter.2.Name': 'dhcp-options-id',
+            'Filter.2.Value.1': 'dopt-7eded312'
+        }
+
+        try:
+            self.assertUrlContainsQueryParams(url, expected_params_1)
+        except AssertionError:
+            # dict ordering is not guaranteed
+            self.assertUrlContainsQueryParams(url, expected_params_2)
+
+        body = self.fixtures.load('describe_vpcs.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
     def _CreateVpc(self, method, url, body, headers):
         body = self.fixtures.load('create_vpc.xml')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])