Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/ironic-understack/ironic_understack/inspected_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
33 changes: 11 additions & 22 deletions python/ironic-understack/ironic_understack/update_baremetal_port.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -73,14 +72,21 @@ 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"]),
)
for name, lldp in plugin_data["parsed_lldp"].items()
]


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)
Expand Down Expand Up @@ -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()


Expand Down
Loading