libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From quent...@apache.org
Subject [2/6] libcloud git commit: implement ex_get_floating_ip for DigitalOcean
Date Fri, 09 Mar 2018 04:55:44 GMT
implement ex_get_floating_ip for DigitalOcean

Throws ValueError if the IP can not be found.

Inspired by ex_get_floating_ip from openstack.py,
but that one does `ip_obj, = [x for x in floating_ips if x.ip_address == ip]`
which will cause an ugly unpacking error if the IP can not be found:
`ValueError: need more than 0 values to unpack`

Signed-off-by: Quentin Pradet <quentinp@apache.org>


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

Branch: refs/heads/trunk
Commit: c8df05363cf0210c53864f1d1cde93bfb147ec7d
Parents: e0bada2
Author: Rick van de Loo <rickvandeloo@gmail.com>
Authored: Sun Mar 4 14:44:54 2018 +0100
Committer: Quentin Pradet <quentinp@apache.org>
Committed: Fri Mar 9 08:54:34 2018 +0400

----------------------------------------------------------------------
 libcloud/compute/drivers/digitalocean.py             | 15 +++++++++++++++
 .../fixtures/digitalocean_v2/list_floating_ips.json  |  2 +-
 libcloud/test/compute/test_digitalocean_v2.py        | 14 +++++++++++---
 3 files changed, 27 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/c8df0536/libcloud/compute/drivers/digitalocean.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/digitalocean.py b/libcloud/compute/drivers/digitalocean.py
index 286c2d0..fbb8397 100644
--- a/libcloud/compute/drivers/digitalocean.py
+++ b/libcloud/compute/drivers/digitalocean.py
@@ -534,6 +534,21 @@ class DigitalOcean_v2_NodeDriver(DigitalOcean_v2_BaseDriver,
             self._paginated_request('/v2/floating_ips', 'floating_ips')
         )
 
+    def ex_get_floating_ip(self, ip):
+        """
+        Get specified floating IP
+
+        :param      ip: floating IP to get
+        :type       ip: ``str``
+
+        :rtype: :class:`DigitalOcean_v2_FloatingIpAddress`
+        """
+        floating_ips = self.ex_list_floating_ips()
+        matching_ips = [x for x in floating_ips if x.ip_address == ip]
+        if not matching_ips:
+            raise ValueError('Floating ip %s not found' % ip)
+        return matching_ips[0]
+
     def _to_node(self, data):
         extra_keys = ['memory', 'vcpus', 'disk', 'region', 'image',
                       'size_slug', 'locked', 'created_at', 'networks',

http://git-wip-us.apache.org/repos/asf/libcloud/blob/c8df0536/libcloud/test/compute/fixtures/digitalocean_v2/list_floating_ips.json
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/fixtures/digitalocean_v2/list_floating_ips.json b/libcloud/test/compute/fixtures/digitalocean_v2/list_floating_ips.json
index 42b11b3..2960989 100644
--- a/libcloud/test/compute/fixtures/digitalocean_v2/list_floating_ips.json
+++ b/libcloud/test/compute/fixtures/digitalocean_v2/list_floating_ips.json
@@ -1,2 +1,2 @@
-{"floating_ips":[{"ip":"177.166.135.205","droplet":null,"region":{"name":"Amsterdam 3","slug":"ams3","sizes":["64gb","512mb","1gb","2gb","4gb","8gb","16gb","32gb","48gb","c-16","s-1vcpu-3gb","c-2","c-4","c-8","m-1vcpu-8gb","m-16gb","m-32gb","m-64gb","m-128gb","m-224gb","s-1vcpu-1gb","s-3vcpu-1gb","s-1vcpu-2gb","s-2vcpu-2gb","s-2vcpu-4gb","s-4vcpu-8gb","s-6vcpu-16gb","s-8vcpu-32gb","s-12vcpu-48gb","s-16vcpu-64gb","s-20vcpu-96gb","s-24vcpu-128gb"],"features":["private_networking","backups","ipv6","metadata","install_agent","storage"],"available":true},"locked":false},{"ip":"154.138.103.175","droplet":null,"region":{"name":"Amsterdam
3","slug":"ams3","sizes":["64gb","512mb","1gb","2gb","4gb","8gb","16gb","32gb","48gb","c-16","s-1vcpu-3gb","c-2","c-4","c-8","m-1vcpu-8gb","m-16gb","m-32gb","m-64gb","m-128gb","m-224gb","s-1vcpu-1gb","s-3vcpu-1gb","s-1vcpu-2gb","s-2vcpu-2gb","s-2vcpu-4gb","s-4vcpu-8gb","s-6vcpu-16gb","s-8vcpu-32gb","s-12vcpu-48gb","s-16vcpu-64gb","s-20vcpu-96gb","s-24vcpu-
 128gb"],"features":["private_networking","backups","ipv6","metadata","install_agent","storage"],"available":true},"locked":false}],"links":{},"meta":{"total":2}}
+{"floating_ips":[{"ip":"133.166.122.204","droplet":{"status":"active","kernel": null,"volume_ids":
[],"locked": false,"name":"vdloo-magweb","backup_ids": [],"created_at":"2018-03-01T10:15:06Z","snapshot_ids":
[],"size_slug":"2gb","id": 84155775,"next_backup_window": null,"vcpus": 2,"features": [],"image":
{"regions": ["ams3"],"min_disk_size": 80,"public": false,"name":"hypernode","distribution":"Ubuntu","created_at":"2018-05-23T14:16:34Z","size_gigabytes":
1.02,"type":"snapshot","slug": null,"id": 12367123},"memory": 4096,"region": {"available":
true,"slug":"ams3","features": ["private_networking","backups","ipv6","metadata","install_agent","storage"],"name":"Amsterdam
3","sizes": ["64gb","512mb","1gb","2gb","4gb","8gb","16gb","32gb","36gb","c-16","s-1vcpu-3gb","c-2","c-4","c-8","m-1vcpu-8gb","m-16gb","m-32gb","m-64gb","m-128gb","m-224gb","s-1vcpu-1gb","s-3vcpu-1gb","s-1vcpu-2gb","s-2vcpu-2gb","s-2vcpu-4gb","s-4vcpu-8gb","s-6vcpu-16gb","s-8vcpu-32gb","s-12vcpu-36gb","s-16vcpu-64gb",
 "s-20vcpu-96gb","s-24vcpu-128gb"]},"disk": 80,"networks": {"v4": [{"ip_address":"145.65.193.36","netmask":"255.255.280.0","type":"public","gateway":"145.65.192.1"}],"v6":
[]},"tags": [],"size": {"price_monthly": 20.0,"available": true,"vcpus": 2,"regions": ["ams2","ams3","blr1","fra1","lon1","nyc1","nyc2","nyc3","sfo1","sfo2","sgp1","tor1"],"memory":
4096,"transfer": 3.0,"disk": 80,"price_hourly": 0.02976,"slug":"2gb"}},"region":{"name":"Amsterdam
3","slug":"ams3","sizes":["64gb","512mb","1gb","2gb","4gb","8gb","16gb","32gb","36gb","c-16","s-1vcpu-3gb","c-2","c-4","c-8","m-1vcpu-8gb","m-16gb","m-32gb","m-64gb","m-128gb","m-224gb","s-1vcpu-1gb","s-3vcpu-1gb","s-1vcpu-2gb","s-2vcpu-2gb","s-2vcpu-4gb","s-4vcpu-8gb","s-6vcpu-16gb","s-8vcpu-32gb","s-12vcpu-36gb","s-16vcpu-64gb","s-20vcpu-96gb","s-24vcpu-128gb"],"features":["private_networking","backups","ipv6","metadata","install_agent","storage"],"available":true},"locked":false},{"ip":"154.138.103.175","droplet":null,"region":{"name":"
 Amsterdam 3","slug":"ams3","sizes":["64gb","512mb","1gb","2gb","4gb","8gb","16gb","32gb","36gb","c-16","s-1vcpu-3gb","c-2","c-4","c-8","m-1vcpu-8gb","m-16gb","m-32gb","m-64gb","m-128gb","m-224gb","s-1vcpu-1gb","s-3vcpu-1gb","s-1vcpu-2gb","s-2vcpu-2gb","s-2vcpu-4gb","s-4vcpu-8gb","s-6vcpu-16gb","s-8vcpu-32gb","s-12vcpu-36gb","s-16vcpu-64gb","s-20vcpu-96gb","s-24vcpu-128gb"],"features":["private_networking","backups","ipv6","metadata","install_agent","storage"],"available":true},"locked":false}],"links":{},"meta":{"total":2}}
 

http://git-wip-us.apache.org/repos/asf/libcloud/blob/c8df0536/libcloud/test/compute/test_digitalocean_v2.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_digitalocean_v2.py b/libcloud/test/compute/test_digitalocean_v2.py
index 02f0f03..4612bb3 100644
--- a/libcloud/test/compute/test_digitalocean_v2.py
+++ b/libcloud/test/compute/test_digitalocean_v2.py
@@ -328,10 +328,18 @@ class DigitalOcean_v2_Tests(LibcloudTestCase):
         self.assertEqual(len(floating_ips), 2, 'Wrong floating IPs count')
 
         floating_ip = floating_ips[0]
-        self.assertEqual(floating_ip.id, '177.166.135.205')
-        self.assertEqual(floating_ip.ip_address, '177.166.135.205')
+        self.assertEqual(floating_ip.id, '133.166.122.204')
+        self.assertEqual(floating_ip.ip_address, '133.166.122.204')
         self.assertEqual(floating_ip.extra['region']['slug'], 'ams3')
-        self.assertIsNone(floating_ip.node_id)
+        self.assertEqual(84155775, floating_ip.node_id)
+
+    def test_get_floating_ip(self):
+        floating_ip = self.driver.ex_get_floating_ip('133.166.122.204')
+
+        self.assertEqual(floating_ip.id, '133.166.122.204')
+        self.assertEqual(floating_ip.ip_address, '133.166.122.204')
+        self.assertEqual(floating_ip.extra['region']['slug'], 'ams3')
+        self.assertEqual(84155775, floating_ip.node_id)
 
 
 class DigitalOceanMockHttp(MockHttp):


Mime
View raw message