diff --git a/CHANGES.rst b/CHANGES.rst index 37d847781f..a24847d330 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -28,6 +28,11 @@ Common Compute ~~~~~~~ +- [OpenStack] Add 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..7c28374790 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,16 @@ 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 ports: + if not port_id: + ports = self.ex_get_node_ports(node) + 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, 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..5c354b9986 100644 --- a/libcloud/test/compute/test_openstack.py +++ b/libcloud/test/compute/test_openstack.py @@ -2547,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