Skip to content

Commit

Permalink
Teach esi.lib.nodes how to detach all ports
Browse files Browse the repository at this point in the history
  • Loading branch information
larsks committed Dec 13, 2024
1 parent c7a57fa commit 84a4084
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions esi/lib/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
# under the License.

import concurrent.futures
import logging
import warnings

from openstack import exceptions

from esi.lib import networks


OPENSTACK_IRONIC_API_VERSION = '1.69'

LOG = logging.getLogger(__name__)

def node_and_port_list(connection, filter_node=None):
"""Get lists baremetal nodes and ports
Expand Down Expand Up @@ -242,18 +244,24 @@ def network_attach(connection, node, attach_info):
}


def network_detach(connection, node, port=None):
def network_detach(connection, node_name=None, port=None, port_names=None, all_ports=False):
"""Detaches a node's bare metal port from a network port
:param port: The name or ID of a network port
:returns: ``True`` if the VIF was detached, otherwise ``False``
:returns: A list of ``(port_uuid, bool)`` tuples, where ``bool`` is ``True`` if the port was removed
successfully, ``False`` otherwise.
"""

node = connection.baremetal.get_node(node)
node= connection.baremetal.get_node(node_name)
ports = []

if port:
port = connection.network.find_port(port, ignore_missing=False)
port_names = [port]
warnings.warn("The 'port' parameter is deprecated and will be removed in a future release.", DeprecationWarning, stacklevel=2)

if port_names:
ports = [connection.network.find_port(port, ignore_missing=False) for port in ports]
else:
bm_ports = connection.baremetal.ports(details=True, node=node.id)

Expand All @@ -263,12 +271,16 @@ def network_detach(connection, node, port=None):
if len(mapped_node_port_list) == 0:
raise exceptions.ResourceFailure(
'Node {0} is not associated with any port'.format(node.name))

if all_ports:
ports = [connection.network.find_port(bmport.internal_info['tenant_vif_port_id'], ignore_missing=False)
for bmport in mapped_node_port_list]
elif len(mapped_node_port_list) > 1:
raise exceptions.ResourceFailure(
"Node {0} is associated with multiple ports. \
Port must be specified".format(node.name))
"Node {0} is associated with multiple ports. Port must be specified".format(node.name))
elif len(mapped_node_port_list) == 1:
port = mapped_node_port_list[0].internal_info["tenant_vif_port_id"]
port = connection.network.find_port(port, ignore_missing=False)
vif = mapped_node_port_list[0].internal_info["tenant_vif_port_id"]
port = connection.network.find_port(vif, ignore_missing=False)
ports = [port]

return connection.baremetal.detach_vif_from_node(node, port.id)
return [(port.id, connection.baremetal.detach_vif_from_node(node, port.id)) for port in ports]

0 comments on commit 84a4084

Please sign in to comment.