diff --git a/python/ironic-understack/ironic_understack/inspected_port.py b/python/ironic-understack/ironic_understack/inspected_port.py index fedaed395..fa85537db 100644 --- a/python/ironic-understack/ironic_understack/inspected_port.py +++ b/python/ironic-understack/ironic_understack/inspected_port.py @@ -21,12 +21,12 @@ def local_link_connection(self) -> dict: @property def parsed_name(self) -> dict[str, str]: - parts = self.switch_system_name.split(".", maxsplit=1) - if len(parts) != 2: + parts = self.switch_system_name.split(".") + if len(parts) < 2: raise ValueError( "Failed to parse switch hostname - expecting name.dc in %s", self ) - switch_name, data_center_name = parts + switch_name, data_center_name = parts[0:2] parts = switch_name.rsplit("-", maxsplit=1) if len(parts) != 2: diff --git a/python/ironic-understack/ironic_understack/tests/test_update_baremetal_port.py b/python/ironic-understack/ironic_understack/tests/test_update_baremetal_port.py index 640dadb09..a5ae366c7 100644 --- a/python/ironic-understack/ironic_understack/tests/test_update_baremetal_port.py +++ b/python/ironic-understack/ironic_understack/tests/test_update_baremetal_port.py @@ -3,7 +3,6 @@ import ironic.objects from oslo_utils import uuidutils -import ironic_understack from ironic_understack.update_baremetal_port import UpdateBaremetalPortsHook # load some metaprgramming normally taken care of during Ironic initialization: @@ -95,14 +94,8 @@ def test_with_valid_data(mocker, caplog): assert mock_port.local_link_connection == { "port_id": "Ethernet1/18", "switch_id": "88:5a:92:ec:54:59", - "switch_info": "f20-3-1.iad3", + "switch_info": "f20-3-1.iad3.rackspace.net", } assert mock_port.physical_network == "f20-3-network" mock_port.save.assert_called() - - mock_traits.get_trait_names.assert_called_once() - mock_traits.destroy.assert_called_once_with("CUSTOM_BMC_SWITCH") - ironic_understack.update_baremetal_port.objects.TraitList.create.assert_called_once_with( - mock_context, 1234, ["CUSTOM_NETWORK_SWITCH", "CUSTOM_STORAGE_SWITCH"] - ) mock_node.save.assert_called_once() diff --git a/python/ironic-understack/ironic_understack/update_baremetal_port.py b/python/ironic-understack/ironic_understack/update_baremetal_port.py index 73e473ee0..ecdad36ce 100644 --- a/python/ironic-understack/ironic_understack/update_baremetal_port.py +++ b/python/ironic-understack/ironic_understack/update_baremetal_port.py @@ -1,6 +1,5 @@ from typing import Any -import openstack from ironic import objects from ironic.common import exception from ironic.drivers.modules.inspector.hooks import base @@ -35,7 +34,7 @@ def __call__(self, task, inventory, plugin_data): - local_link_info.port_id (e.g. "Ethernet1/1") - local_link_info.switch_id (e.g. "aa:bb:cc:dd:ee:ff") - - local_link_info.switch_info (e.g. "a1-1-1.ord1") + - local_link_info.switch_info (e.g. "a1-1-1.ord1.rackspace.net") - physical_network (e.g. "a1-1-network") We also add or remove node "traits" based on the inventory data. We @@ -73,7 +72,7 @@ def _parse_plugin_data(plugin_data: dict) -> list[InspectedPort]: InspectedPort( mac_address=mac[name], name=name, - switch_system_name=str(lldp["switch_system_name"]).lower(), + switch_system_name=_normalise_switch_name(lldp["switch_system_name"]), switch_chassis_id=str(lldp["switch_chassis_id"]).lower(), switch_port_id=str(lldp["switch_port_id"]), ) @@ -81,6 +80,13 @@ def _parse_plugin_data(plugin_data: dict) -> list[InspectedPort]: ] +def _normalise_switch_name(name: str) -> str: + suffix = ".rackspace.net" + name = str(name).lower() + name = name if name.endswith(suffix) else name + suffix + return name + + def _update_port_attrs(task, ports_by_mac, vlan_groups, node_uuid): for baremetal_port in ironic_ports_for_node(task.context, task.node.id): inspected_port = ports_by_mac.get(baremetal_port.address) @@ -174,31 +180,14 @@ def _set_node_traits(task, vlan_groups: set[str]): required_traits = {_trait_name(x) for x in vlan_groups if x} existing_traits = set(node.traits.get_trait_names()).intersection(our_traits) - traits_to_remove = sorted(existing_traits.difference(required_traits)) - traits_to_add = sorted(required_traits.difference(existing_traits)) - LOG.debug( "Checking traits for node %s: existing=%s required=%s", node.uuid, existing_traits, required_traits, ) - - for trait in traits_to_remove: - LOG.debug("Removing trait %s from node %s", trait, node.uuid) - try: - node.traits.destroy(trait) - except openstack.exceptions.NotFoundException: - pass - - if traits_to_add: - LOG.debug("Adding traits %s to node %s", traits_to_add, node.uuid) - - node.traits = objects.TraitList.create( - task.context, node.id, list(traits_to_add) - ) - - if traits_to_add or traits_to_remove: + if existing_traits != required_traits: + objects.TraitList.create(task.context, task.node.id, required_traits) node.save()