From 44e99451d787537c17e57af30c8c93ae7c63fc80 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 2 Jul 2024 15:42:15 +0200 Subject: [PATCH 1/4] implements #2027 --- CHANGES.rst | 5 +++++ libcloud/compute/drivers/openstack.py | 11 ++++++++--- libcloud/test/compute/test_openstack.py | 2 ++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 37d847781f..ab6f441741 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -28,6 +28,11 @@ Common Compute ~~~~~~~ +- [OpenStack] Optional node port ID to attach the floating IP in OpenStack + ex_attach_floating_ip_to_node function. + (#2028) + [Miguel Caballer - @micafer] + - [OpenStack] Add metadata fields ``os_distro`` and ``os_version`` provided by OpenStack Image API (if set) to the ``extra`` field of the OpenStack NodeImage. (#1982) diff --git a/libcloud/compute/drivers/openstack.py b/libcloud/compute/drivers/openstack.py index cea1974567..f09e1d704e 100644 --- a/libcloud/compute/drivers/openstack.py +++ b/libcloud/compute/drivers/openstack.py @@ -4298,7 +4298,7 @@ def ex_delete_floating_ip(self, ip): resp = self.network_connection.request("/v2.0/floatingips/%s" % ip.id, method="DELETE") return resp.status in (httplib.NO_CONTENT, httplib.ACCEPTED) - def ex_attach_floating_ip_to_node(self, node, ip): + def ex_attach_floating_ip_to_node(self, node, ip, port_id=None): """ Attach the floating IP to the node @@ -4308,6 +4308,9 @@ def ex_attach_floating_ip_to_node(self, node, ip): :param ip: floating IP to attach :type ip: ``str`` or :class:`OpenStack_1_1_FloatingIpAddress` + :param port_id: Optional node port ID to attach the floating IP + :type ip: ``str`` + :rtype: ``bool`` """ ip_id = None @@ -4320,13 +4323,15 @@ def ex_attach_floating_ip_to_node(self, node, ip): ip_id = fip.id if not ip_id: return False - ports = self.ex_get_node_ports(node) + if not port_id: + ports = self.ex_get_node_ports(node) + port_id = ports[0].id if ports: # Set to the first node port resp = self.network_connection.request( "/v2.0/floatingips/%s" % ip_id, method="PUT", - data={"floatingip": {"port_id": ports[0].id}}, + data={"floatingip": {"port_id": port_id}}, ) return resp.status == httplib.OK else: diff --git a/libcloud/test/compute/test_openstack.py b/libcloud/test/compute/test_openstack.py index 4fcf08c753..b4f8a36b8d 100644 --- a/libcloud/test/compute/test_openstack.py +++ b/libcloud/test/compute/test_openstack.py @@ -1677,8 +1677,10 @@ def test_ex_attach_floating_ip_to_node(self): node = self.driver.create_node(name="racktest", image=image, size=size) node.id = 4242 ip = "42.42.42.42" + port_id = 'ce531f90-199f-48c0-816c-13e38010b442' self.assertTrue(self.driver.ex_attach_floating_ip_to_node(node, ip)) + self.assertTrue(self.driver.ex_attach_floating_ip_to_node(node, ip, port_id)) def test_detach_floating_ip_from_node(self): image = NodeImage(id=11, name="Ubuntu 8.10 (intrepid)", driver=self.driver) From f61e19c0926372dc6e9c6c659e84a11460d65434 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 2 Jul 2024 15:53:56 +0200 Subject: [PATCH 2/4] implements #2027 --- libcloud/compute/drivers/openstack.py | 5 +++-- libcloud/test/compute/test_openstack.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/libcloud/compute/drivers/openstack.py b/libcloud/compute/drivers/openstack.py index f09e1d704e..7c28374790 100644 --- a/libcloud/compute/drivers/openstack.py +++ b/libcloud/compute/drivers/openstack.py @@ -4325,8 +4325,9 @@ def ex_attach_floating_ip_to_node(self, node, ip, port_id=None): return False if not port_id: ports = self.ex_get_node_ports(node) - port_id = ports[0].id - if ports: + if ports: + port_id = ports[0].id + if port_id: # Set to the first node port resp = self.network_connection.request( "/v2.0/floatingips/%s" % ip_id, diff --git a/libcloud/test/compute/test_openstack.py b/libcloud/test/compute/test_openstack.py index b4f8a36b8d..88ad045b58 100644 --- a/libcloud/test/compute/test_openstack.py +++ b/libcloud/test/compute/test_openstack.py @@ -1677,10 +1677,8 @@ def test_ex_attach_floating_ip_to_node(self): node = self.driver.create_node(name="racktest", image=image, size=size) node.id = 4242 ip = "42.42.42.42" - port_id = 'ce531f90-199f-48c0-816c-13e38010b442' self.assertTrue(self.driver.ex_attach_floating_ip_to_node(node, ip)) - self.assertTrue(self.driver.ex_attach_floating_ip_to_node(node, ip, port_id)) def test_detach_floating_ip_from_node(self): image = NodeImage(id=11, name="Ubuntu 8.10 (intrepid)", driver=self.driver) @@ -2549,6 +2547,16 @@ def test_ex_delete_floating_ip(self): ip = OpenStack_1_1_FloatingIpAddress("foo-bar-id", "42.42.42.42", None) self.assertTrue(self.driver.ex_delete_floating_ip(ip)) + def test_ex_attach_floating_ip_to_node(self): + image = NodeImage(id=11, name="Ubuntu 8.10 (intrepid)", driver=self.driver) + size = NodeSize(1, "256 slice", None, None, None, None, driver=self.driver) + node = self.driver.create_node(name="racktest", image=image, size=size) + node.id = 4242 + ip = "42.42.42.42" + port_id = 'ce531f90-199f-48c0-816c-13e38010b442' + + self.assertTrue(self.driver.ex_attach_floating_ip_to_node(node, ip, port_id)) + class OpenStack_1_1_FactoryMethodTests(OpenStack_1_1_Tests): should_list_locations = False From 4029721d2ea9aa68b305c9213146f290df206d98 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 2 Jul 2024 16:09:31 +0200 Subject: [PATCH 3/4] Fix lint --- libcloud/test/compute/test_openstack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcloud/test/compute/test_openstack.py b/libcloud/test/compute/test_openstack.py index 88ad045b58..5c354b9986 100644 --- a/libcloud/test/compute/test_openstack.py +++ b/libcloud/test/compute/test_openstack.py @@ -2553,7 +2553,7 @@ def test_ex_attach_floating_ip_to_node(self): node = self.driver.create_node(name="racktest", image=image, size=size) node.id = 4242 ip = "42.42.42.42" - port_id = 'ce531f90-199f-48c0-816c-13e38010b442' + port_id = "ce531f90-199f-48c0-816c-13e38010b442" self.assertTrue(self.driver.ex_attach_floating_ip_to_node(node, ip, port_id)) From 606b92b0dd65c6500f62962d54462231cf017826 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 2 Jul 2024 16:09:49 +0200 Subject: [PATCH 4/4] Update CHANGES --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index ab6f441741..a24847d330 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -28,7 +28,7 @@ Common Compute ~~~~~~~ -- [OpenStack] Optional node port ID to attach the floating IP in OpenStack +- [OpenStack] Add optional node port ID to attach the floating IP in OpenStack ex_attach_floating_ip_to_node function. (#2028) [Miguel Caballer - @micafer]