Skip to content

Commit

Permalink
implements apache#2027
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Jul 2, 2024
1 parent 1ebd605 commit 44e9945
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions libcloud/compute/drivers/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions libcloud/test/compute/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 44e9945

Please sign in to comment.