You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by qu...@apache.org on 2018/03/09 04:55:46 UTC
[4/6] libcloud git commit: implement ex_create_floating_ip for
DigitalOcean
implement ex_create_floating_ip for DigitalOcean
create a new unassociated floating IP in a region
Signed-off-by: Quentin Pradet <qu...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/d613edf7
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/d613edf7
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/d613edf7
Branch: refs/heads/trunk
Commit: d613edf7151750b3b7c2f92070dca81e208e7e95
Parents: 4dd20ff
Author: Rick van de Loo <ri...@gmail.com>
Authored: Sun Mar 4 14:15:23 2018 +0100
Committer: Quentin Pradet <qu...@apache.org>
Committed: Fri Mar 9 08:54:34 2018 +0400
----------------------------------------------------------------------
libcloud/compute/drivers/digitalocean.py | 59 ++++++++++++++++++++
.../digitalocean_v2/create_floating_ip.json | 1 +
libcloud/test/compute/test_digitalocean_v2.py | 22 ++++++++
3 files changed, 82 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/libcloud/blob/d613edf7/libcloud/compute/drivers/digitalocean.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/digitalocean.py b/libcloud/compute/drivers/digitalocean.py
index 5815955..91dee7b 100644
--- a/libcloud/compute/drivers/digitalocean.py
+++ b/libcloud/compute/drivers/digitalocean.py
@@ -493,6 +493,24 @@ class DigitalOcean_v2_NodeDriver(DigitalOcean_v2_BaseDriver,
method='DELETE')
return res.status == httplib.NO_CONTENT
+ def ex_create_floating_ip(self, location):
+ """
+ Create new floating IP reserved to a region.
+
+ The newly created floating IP will not be associated to a Droplet.
+
+ See https://developers.digitalocean.com/documentation/v2/#floating-ips
+
+ :param location: Which data center to create the floating IP in.
+ :type location: :class:`.NodeLocation`
+
+ :rtype: :class:`DigitalOcean_v2_FloatingIpAddress`
+ """
+ attr = {'region': location.id}
+ resp = self.connection.request('/v2/floating_ips',
+ data=json.dumps(attr), method='POST')
+ return self._to_floating_ip(resp.object['floating_ip'])
+
def _to_node(self, data):
extra_keys = ['memory', 'vcpus', 'disk', 'region', 'image',
'size_slug', 'locked', 'created_at', 'networks',
@@ -573,3 +591,44 @@ class DigitalOcean_v2_NodeDriver(DigitalOcean_v2_BaseDriver,
return VolumeSnapshot(id=data['id'], name=data['name'],
size=data['size_gigabytes'],
driver=self, extra=extra)
+
+ def _to_floating_ips(self, obj):
+ return [self._to_floating_ip(ip) for ip in obj]
+
+ def _to_floating_ip(self, obj):
+ return DigitalOcean_v2_FloatingIpAddress(
+ # There is no ID, but the IP is unique so we can use that
+ id=obj['ip'],
+ ip_address=obj['ip'],
+ node_id=obj['droplet']['id'] if obj['droplet'] else None,
+ extra={
+ 'region': obj['region'],
+ },
+ driver=self
+ )
+
+
+class DigitalOcean_v2_FloatingIpAddress(object):
+ """
+ Floating IP info.
+ """
+
+ def __init__(self, id, ip_address, node_id=None, extra=None, driver=None):
+ self.id = str(id)
+ self.ip_address = ip_address
+ self.extra = extra
+ self.node_id = node_id
+ self.driver = driver
+
+ def delete(self):
+ """
+ Delete this floating IP
+
+ :rtype: ``bool``
+ """
+ return self.driver.ex_delete_floating_ip(self)
+
+ def __repr__(self):
+ return ('<DigitalOcean_v2_FloatingIpAddress: id=%s, ip_addr=%s,'
+ ' driver=%s>'
+ % (self.id, self.ip_address, self.driver))
http://git-wip-us.apache.org/repos/asf/libcloud/blob/d613edf7/libcloud/test/compute/fixtures/digitalocean_v2/create_floating_ip.json
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/fixtures/digitalocean_v2/create_floating_ip.json b/libcloud/test/compute/fixtures/digitalocean_v2/create_floating_ip.json
new file mode 100644
index 0000000..dbacd74
--- /dev/null
+++ b/libcloud/test/compute/fixtures/digitalocean_v2/create_floating_ip.json
@@ -0,0 +1 @@
+{"floating_ip":{"ip":"167.138.123.111","droplet":null,"region":{"name":"New York 1","slug":"nyc1","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":{}}
http://git-wip-us.apache.org/repos/asf/libcloud/blob/d613edf7/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 1b049f9..f2cc3f7 100644
--- a/libcloud/test/compute/test_digitalocean_v2.py
+++ b/libcloud/test/compute/test_digitalocean_v2.py
@@ -292,6 +292,21 @@ class DigitalOcean_v2_Tests(LibcloudTestCase):
result = self.driver.delete_volume_snapshot(snapshot)
self.assertTrue(result)
+ def test_ex_create_floating_ip(self):
+ nyc1 = [r for r in self.driver.list_locations() if r.id == 'nyc1'][0]
+ floating_ip = self.driver.ex_create_floating_ip(nyc1)
+
+ # Note that this is the ID. There is no real ID for a floating IP at
+ # DigitalOcean, but the IP is unique so we can use that instead.
+ self.assertEqual(floating_ip.id, '167.138.123.111')
+ self.assertEqual(floating_ip.ip_address, '167.138.123.111')
+ self.assertEqual(floating_ip.extra['region']['slug'], 'nyc1')
+ # The newly created floating IP reserved to a region is not
+ # associated with any droplet. See the DigitalOcean API docs
+ # how to create a floating IP that is associated with an instance
+ # from the start. This API call creates an unattached IP.
+ self.assertIsNone(floating_ip.node_id)
+
class DigitalOceanMockHttp(MockHttp):
fixtures = ComputeFileFixtures('digitalocean_v2')
@@ -452,6 +467,13 @@ class DigitalOceanMockHttp(MockHttp):
return (httplib.NO_CONTENT, None, {},
httplib.responses[httplib.NO_CONTENT])
+ def _v2_floating_ips(self, method, url, body, headers):
+ if method == 'POST':
+ body = self.fixtures.load('create_floating_ip.json')
+ return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+ else:
+ raise NotImplementedError()
+
if __name__ == '__main__':
sys.exit(unittest.main())