libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vd...@apache.org
Subject [16/23] libcloud git commit: Add create and delete subnets
Date Tue, 04 Dec 2018 08:54:31 GMT
Add create and delete subnets


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

Branch: refs/heads/trunk
Commit: ca5430d689e621e6f1fb6d4231352471b9a7a91e
Parents: 33e694f
Author: micafer <micafer1@upv.es>
Authored: Tue Oct 23 12:19:50 2018 +0200
Committer: Rick van de Loo <rickvandeloo@gmail.com>
Committed: Tue Dec 4 09:45:48 2018 +0100

----------------------------------------------------------------------
 libcloud/compute/drivers/openstack.py           | 40 +++++++++++++++++++-
 .../fixtures/openstack_v1.1/_v2_0__subnet.json  | 32 ++++++++++++++++
 libcloud/test/compute/test_openstack.py         | 27 ++++++++++++-
 3 files changed, 96 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/ca5430d6/libcloud/compute/drivers/openstack.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/openstack.py b/libcloud/compute/drivers/openstack.py
index 0de0ab3..ec71091 100644
--- a/libcloud/compute/drivers/openstack.py
+++ b/libcloud/compute/drivers/openstack.py
@@ -1681,7 +1681,7 @@ class OpenStack_1_1_NodeDriver(OpenStackNodeDriver):
 
     def ex_delete_network(self, network):
         """
-        Get a list of NodeNetorks that are available.
+        Delete a Network
 
         :param network: Network which should be used
         :type network: :class:`OpenStackNetwork`
@@ -2914,6 +2914,44 @@ class OpenStack_2_NodeDriver(OpenStack_1_1_NodeDriver):
             self._subnets_url_prefix).object
         return self._to_subnets(response)
 
+    def ex_create_subnet(self, name, network, cidr, ip_version=4):
+        """
+        Create a new Subnet
+
+        :param name: Name of subnet which should be used
+        :type name: ``str``
+
+        :param network: Parent network of the subnet
+        :type network: ``OpenStackNetwork``
+
+        :param cidr: cidr of network which should be used
+        :type cidr: ``str``
+
+        :param ip_version: ip_version of subnet which should be used
+        :type ip_version: ``int``
+
+        :rtype: :class:`OpenStack_2_SubNet`
+        """
+        data = {'subnet': {'cidr': cidr, 'network_id': network.id,
+                           'ip_version': ip_version, 'name': name}}
+        response = self.connection.request(self._subnets_url_prefix,
+                                           method='POST', data=data).object
+        return self._to_subnet(response['subnet'])
+
+    def ex_delete_subnet(self, subnet):
+        """
+        Delete a Subnet
+
+        :param subnet: Subnet which should be deleted
+        :type subnet: :class:`OpenStack_2_SubNet`
+
+        :rtype: ``bool``
+        """
+        resp = self.connection.request('%s/%s' % (self._subnets_url_prefix,
+                                                  subnet.id),
+                                       method='DELETE')
+        return resp.status in (httplib.NO_CONTENT, httplib.ACCEPTED)
+
     def ex_list_ports(self):
         """
         List all OpenStack_2_PortInterfaces

http://git-wip-us.apache.org/repos/asf/libcloud/blob/ca5430d6/libcloud/test/compute/fixtures/openstack_v1.1/_v2_0__subnet.json
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/fixtures/openstack_v1.1/_v2_0__subnet.json b/libcloud/test/compute/fixtures/openstack_v1.1/_v2_0__subnet.json
new file mode 100644
index 0000000..dba344a
--- /dev/null
+++ b/libcloud/test/compute/fixtures/openstack_v1.1/_v2_0__subnet.json
@@ -0,0 +1,32 @@
+{
+    "subnet":
+        {
+            "name": "name",
+            "enable_dhcp": true,
+            "network_id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
+            "segment_id": null,
+            "project_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
+            "tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
+            "dns_nameservers": [],
+            "allocation_pools": [
+                {
+                    "start": "10.0.0.2",
+                    "end": "10.0.0.254"
+                }
+            ],
+            "host_routes": [],
+            "ip_version": 4,
+            "gateway_ip": "10.0.0.1",
+            "cidr": "10.0.0.0/24",
+            "id": "08eae331-0402-425a-923c-34f7cfe39c1b",
+            "created_at": "2016-10-10T14:35:34Z",
+            "description": "",
+            "ipv6_address_mode": null,
+            "ipv6_ra_mode": null,
+            "revision_number": 2,
+            "service_types": [],
+            "subnetpool_id": null,
+            "tags": ["tag1,tag2"],
+            "updated_at": "2016-10-10T14:35:34Z"
+        }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/ca5430d6/libcloud/test/compute/test_openstack.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_openstack.py b/libcloud/test/compute/test_openstack.py
index db7b357..e567f17 100644
--- a/libcloud/test/compute/test_openstack.py
+++ b/libcloud/test/compute/test_openstack.py
@@ -1758,6 +1758,17 @@ class OpenStack_2_Tests(OpenStack_1_1_Tests):
         self.assertEqual(subnet.name, 'private-subnet')
         self.assertEqual(subnet.cidr, '10.0.0.0/24')
 
+    def test_ex_create_subnet(self):
+        network = self.driver.ex_list_networks()[0]
+        subnet = self.driver.ex_create_subnet('name', network, '10.0.0.0/24')
+
+        self.assertEqual(subnet.name, 'name')
+        self.assertEqual(subnet.cidr, '10.0.0.0/24')
+
+    def test_ex_delete_subnet(self):
+        subnet = self.driver.ex_list_subnets()[0]
+        self.assertTrue(self.driver.ex_delete_subnet(subnet=subnet))
+
     def test_ex_list_network(self):
         networks = self.driver.ex_list_networks()
         network = networks[0]
@@ -2440,9 +2451,21 @@ class OpenStack_1_1_MockHttp(MockHttp, unittest.TestCase):
             body = ''
             return (httplib.NO_CONTENT, body, self.json_content_headers, httplib.responses[httplib.OK])
 
+    def _v2_1337_v2_0_subnets_08eae331_0402_425a_923c_34f7cfe39c1b(self, method, url, body,
headers):
+        if method == 'GET':
+            body = self.fixtures.load('_v2_0__subnet.json')
+            return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK])
+        if method == 'DELETE':
+            body = ''
+            return (httplib.NO_CONTENT, body, self.json_content_headers, httplib.responses[httplib.OK])
+
     def _v2_1337_v2_0_subnets(self, method, url, body, headers):
-        body = self.fixtures.load('_v2_0__subnets.json')
-        return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK])
+        if method == 'POST':
+            body = self.fixtures.load('_v2_0__subnet.json')
+            return (httplib.CREATED, body, self.json_content_headers, httplib.responses[httplib.OK])
+        else:
+            body = self.fixtures.load('_v2_0__subnets.json')
+            return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK])
 
     def _v2_1337_volumes_detail(self, method, url, body, headers):
         body = self.fixtures.load('_v2_0__volumes.json')


Mime
View raw message