Skip to content

Commit 76fd166

Browse files
committed
Compare Ironic ports by mac address, not by name
1 parent 59243af commit 76fd166

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

python/understack-workflows/understack_workflows/data_center.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class Switch:
2020

2121
@property
2222
def vlan_group_name(self) -> str | None:
23-
# TODO: remove domain
23+
name = self.name.split(".")[0]
2424
for switch_name_suffix, vlan_group_suffix in VLAN_GROUP_SUFFIXES.items():
25-
if self.name.endswith(switch_name_suffix):
26-
cabinet_name = self.name.removesuffix(switch_name_suffix)
25+
if name.endswith(switch_name_suffix):
26+
cabinet_name = name.removesuffix(switch_name_suffix)
2727
return f"{cabinet_name}-{vlan_group_suffix}"
2828
return None
2929

python/understack-workflows/understack_workflows/port_configuration.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class PortConfiguration(BaseModel):
88
address: Annotated[
99
str, StringConstraints(to_lower=True)
1010
] # ironicclient's Port class lowercases this attribute
11-
uuid: str|None # using a str here to due to ironicclient Port attribute
1211
node_uuid: str # using a str here due to ironicclient Port attribute
1312
name: str # port name
1413
pxe_enabled: bool

python/understack-workflows/understack_workflows/sync_interfaces.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,52 @@ def update_ironic_baremetal_ports(
1818
ironic_client = IronicClient()
1919
logger.info("Syncing Interfaces / Ports for Device %s ...", device_uuid)
2020

21-
discovered_ports = dict_by_name(
22-
make_port_infos(discovered_interfaces, pxe_interface_name, device_uuid)
21+
discovered_ports = dict_by_mac_address(
22+
make_port_infos(discovered_interfaces, pxe_interface_name, device_uuid, ironic_node.name)
2323
)
2424
logger.debug("Actual ports according to BMC: %s", discovered_ports)
2525

2626
logger.info("Fetching Ironic Ports ...")
27-
ironic_ports = dict_by_name(
27+
ironic_ports = dict_by_mac_address(
2828
ironic_client.list_ports(device_uuid)
2929
)
3030

31-
for name, interface in ironic_ports.items():
32-
if name not in discovered_ports:
31+
for mac_address, ironic_port in ironic_ports.items():
32+
if mac_address not in discovered_ports:
3333
logger.info(
3434
"Server Interface %s no longer exists, "
3535
"deleting corresponding Ironic Port %s",
36-
interface.name,
37-
interface.uuid,
36+
mac_address,
37+
ironic_port.uuid,
3838
)
39-
response = ironic_client.delete_port(interface.uuid)
39+
response = ironic_client.delete_port(ironic_port.uuid)
4040
logger.debug("Deleted: %s", response)
4141

42-
for name, actual_port in discovered_ports.items():
43-
if name in ironic_ports:
44-
patch = get_patch(actual_port, ironic_ports[name])
42+
for mac_address, actual_port in discovered_ports.items():
43+
ironic_port = ironic_ports.get(mac_address)
44+
if ironic_port:
45+
patch = get_patch(actual_port, ironic_port)
4546
if patch:
46-
logger.info("Updating Ironic Port %s ...", actual_port)
47-
response = ironic_client.update_port(name, patch)
47+
logger.info("Updating Ironic Port %s, setting %s", ironic_port, patch)
48+
response = ironic_client.update_port(ironic_port.uuid, patch)
4849
logger.debug("Updated: %s", response)
4950
else:
50-
logger.debug("No changes required for Ironic Port %s", name)
51+
logger.debug("No changes required for Ironic Port %s", mac_address)
5152
else:
5253
logger.info("Creating Ironic Port %s ...", actual_port)
5354
response = ironic_client.create_port(actual_port.dict())
5455
logger.debug("Created: %s", response)
5556

5657

57-
def dict_by_name(items: list) -> dict:
58-
return {item.name: item for item in items}
58+
def dict_by_mac_address(items: list) -> dict:
59+
return {item.address: item for item in items}
5960

6061

6162
def make_port_infos(
6263
interfaces: list[InterfaceInfo],
6364
pxe_interface_name: str,
6465
device_id: str,
66+
device_name: str,
6567
) -> list[PortConfiguration]:
6668
"""Get Nautobot interfaces for a device.
6769
@@ -71,7 +73,7 @@ def make_port_infos(
7173
7274
"""
7375
return [
74-
port_configuration(interface, pxe_interface_name, device_id)
76+
port_configuration(interface, pxe_interface_name, device_id, device_name)
7577
for interface in interfaces
7678
if (
7779
interface.mac_address and
@@ -82,11 +84,11 @@ def make_port_infos(
8284

8385

8486
def port_configuration(
85-
interface: InterfaceInfo, pxe_interface_name: str, device_id: str
87+
interface: InterfaceInfo, pxe_interface_name: str, device_id: str, device_name: str
8688
) -> PortConfiguration:
8789
# Interface names have device UUID prepended because Ironic wants them
8890
# globally unique across all devices.
89-
name = f"{device_id} {interface.name}"
91+
name = f"{device_name} {interface.name}"
9092
pxe_enabled = interface.name == pxe_interface_name
9193

9294
if interface.remote_switch_mac_address and interface.remote_switch_port_name:
@@ -105,7 +107,6 @@ def port_configuration(
105107
local_link_connection = {}
106108

107109
return PortConfiguration(
108-
uuid=None,
109110
node_uuid=device_id,
110111
address=interface.mac_address.lower(),
111112
name=name,

0 commit comments

Comments
 (0)