Repository: libcloud Updated Branches: refs/heads/trunk 6a8933fca -> f07c6dd45
implement attach/detach floating IPs for DO add methods to the DigitalOcean_v2_NodeDriver to attach and detach floating IPs from instances uniform to the `ex_attach_floating_ip_to_node` and `ex_detach_floating_ip_from_node` methods from the [OpenStack_1_1_NodeDriver](https://github.com/apache/libcloud/blob/2b298b7cd8d17b4b1c0db20292fdb7867a512ac6/libcloud/compute/drivers/openstack.py#L2394) see https://developers.digitalocean.com/documentation/v2/#assign-a-floating-ip-to-a-droplet and https://developers.digitalocean.com/documentation/v2/#unassign-a-floating-ip Signed-off-by: Quentin Pradet <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/f8fbd1dd Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/f8fbd1dd Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/f8fbd1dd Branch: refs/heads/trunk Commit: f8fbd1dd050a5a8d8c7af983d1f09087873d7d21 Parents: 6a8933f Author: Rick van de Loo <[email protected]> Authored: Mon Mar 19 15:32:44 2018 +0100 Committer: Quentin Pradet <[email protected]> Committed: Wed Mar 21 11:11:30 2018 +0400 ---------------------------------------------------------------------- libcloud/compute/drivers/digitalocean.py | 48 ++++++++++++++++++++ .../digitalocean_v2/attach_floating_ip.json | 1 + .../digitalocean_v2/detach_floating_ip.json | 1 + libcloud/test/compute/test_digitalocean_v2.py | 30 ++++++++++++ 4 files changed, 80 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/f8fbd1dd/libcloud/compute/drivers/digitalocean.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/digitalocean.py b/libcloud/compute/drivers/digitalocean.py index fbb8397..12e4a60 100644 --- a/libcloud/compute/drivers/digitalocean.py +++ b/libcloud/compute/drivers/digitalocean.py @@ -549,6 +549,54 @@ class DigitalOcean_v2_NodeDriver(DigitalOcean_v2_BaseDriver, raise ValueError('Floating ip %s not found' % ip) return matching_ips[0] + def ex_attach_floating_ip_to_node(self, node, ip): + """ + Attach the floating IP to the node + + :param node: node + :type node: :class:`Node` + + :param ip: floating IP to attach + :type ip: ``str`` or :class:`DigitalOcean_v2_FloatingIpAddress` + + :rtype: ``bool`` + """ + data = { + 'type': 'assign', + 'droplet_id': node.id + } + resp = self.connection.request( + '/v2/floating_ips/%s/actions' % ip.ip_address, + data=json.dumps(data), method='POST' + ) + return resp.status == httplib.CREATED + + def ex_detach_floating_ip_from_node(self, node, ip): + """ + Detach a floating IP from the given node + + Note: the 'node' object is not used in this method but it is added + to the signature of ex_detach_floating_ip_from_node anyway so it + conforms to the interface of the method of the same name for other + drivers like for example OpenStack. + + :param node: Node from which the IP should be detached + :type node: :class:`Node` + + :param ip: Floating IP to detach + :type ip: :class:`DigitalOcean_v2_FloatingIpAddress` + + :rtype: ``bool`` + """ + data = { + 'type': 'unassign' + } + resp = self.connection.request( + '/v2/floating_ips/%s/actions' % ip.ip_address, + data=json.dumps(data), method='POST' + ) + return resp.status == httplib.CREATED + 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/f8fbd1dd/libcloud/test/compute/fixtures/digitalocean_v2/attach_floating_ip.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/digitalocean_v2/attach_floating_ip.json b/libcloud/test/compute/fixtures/digitalocean_v2/attach_floating_ip.json new file mode 100644 index 0000000..c5773f0 --- /dev/null +++ b/libcloud/test/compute/fixtures/digitalocean_v2/attach_floating_ip.json @@ -0,0 +1 @@ +{"action":{"id":397123123,"status":"in-progress","type":"assign_ip","started_at":"2018-03-19T14:16:04Z","completed_at":null,"resource_id":1812306133,"resource_type":"floating_ip","region":{"name":"Amsterdam 3","slug":"ams3","sizes":["512mb","1gb","2gb","4gb","8gb","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","16gb","s-8vcpu-32gb","32gb","s-12vcpu-48gb","48gb","s-16vcpu-64gb","64gb","s-20vcpu-96gb"],"features":["private_networking","backups","ipv6","metadata","install_agent","storage"],"available":true},"region_slug":"ams3"}} http://git-wip-us.apache.org/repos/asf/libcloud/blob/f8fbd1dd/libcloud/test/compute/fixtures/digitalocean_v2/detach_floating_ip.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/digitalocean_v2/detach_floating_ip.json b/libcloud/test/compute/fixtures/digitalocean_v2/detach_floating_ip.json new file mode 100644 index 0000000..eecb8fb --- /dev/null +++ b/libcloud/test/compute/fixtures/digitalocean_v2/detach_floating_ip.json @@ -0,0 +1 @@ +{"action":{"id":397123123,"status":"in-progress","type":"unassign_ip","started_at":"2018-03-19T14:19:33Z","completed_at":null,"resource_id":9946493837,"resource_type":"floating_ip","region":{"name":"Amsterdam 3","slug":"ams3","sizes":["512mb","1gb","2gb","4gb","8gb","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","16gb","s-8vcpu-32gb","32gb","s-12vcpu-48gb","48gb","s-16vcpu-64gb","64gb","s-20vcpu-96gb"],"features":["private_networking","backups","ipv6","metadata","install_agent","storage"],"available":true},"region_slug":"ams3"}} http://git-wip-us.apache.org/repos/asf/libcloud/blob/f8fbd1dd/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 4612bb3..775929d 100644 --- a/libcloud/test/compute/test_digitalocean_v2.py +++ b/libcloud/test/compute/test_digitalocean_v2.py @@ -341,6 +341,22 @@ class DigitalOcean_v2_Tests(LibcloudTestCase): self.assertEqual(floating_ip.extra['region']['slug'], 'ams3') self.assertEqual(84155775, floating_ip.node_id) + def test_ex_attach_floating_ip_to_node(self): + node = self.driver.list_nodes()[0] + floating_ip = self.driver.ex_get_floating_ip('133.166.122.204') + + ret = self.driver.ex_attach_floating_ip_to_node(node, floating_ip) + + self.assertTrue(ret) + + def test_ex_detach_floating_ip_from_node(self): + node = self.driver.list_nodes()[0] + floating_ip = self.driver.ex_get_floating_ip('154.138.103.175') + + ret = self.driver.ex_detach_floating_ip_from_node(node, floating_ip) + + self.assertTrue(ret) + class DigitalOceanMockHttp(MockHttp): fixtures = ComputeFileFixtures('digitalocean_v2') @@ -518,6 +534,20 @@ class DigitalOceanMockHttp(MockHttp): else: raise NotImplementedError() + def _v2_floating_ips_133_166_122_204_actions(self, method, url, body, headers): + if method == 'POST': + body = self.fixtures.load('attach_floating_ip.json') + return (httplib.CREATED, body, {}, httplib.responses[httplib.CREATED]) + else: + raise NotImplementedError() + + def _v2_floating_ips_154_138_103_175_actions(self, method, url, body, headers): + if method == 'POST': + body = self.fixtures.load('detach_floating_ip.json') + return (httplib.CREATED, body, {}, httplib.responses[httplib.CREATED]) + else: + raise NotImplementedError() + if __name__ == '__main__': sys.exit(unittest.main())
