From 67603b72dafcb548f65fc1108310de468b4b4e6b Mon Sep 17 00:00:00 2001 From: John Date: Fri, 11 Sep 2026 18:57:47 +0100 Subject: [PATCH] Fix primary MAC lookup for shared MAC addresses --- netbox_agent/network.py | 9 ++++++++- tests/network.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index c2706108..9a844ae9 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -309,6 +309,13 @@ def update_interface_macs(self, nic, macs): } ) + def set_netbox_interface_primary_mac(self, interface, mac): + primary_mac = self.nb_net.mac_addresses.get( + interface_id=interface.id, + mac_address=mac, + ) + interface.primary_mac_address = primary_mac.id + def create_netbox_nic(self, nic, mgmt=False): # TODO: add Optic Vendor, PN and Serial nic_type = self.get_netbox_type_for_nic(nic) @@ -551,7 +558,7 @@ def batched(it, n): if version.parse(nb.version) < version.parse("4.2"): interface.mac_address = nic["mac"] else: - interface.primary_mac_address = {"mac_address": nic["mac"]} + self.set_netbox_interface_primary_mac(interface, nic["mac"]) nic_update += 1 if hasattr(interface, "mtu"): diff --git a/tests/network.py b/tests/network.py index 99c42d4c..1d37af8f 100644 --- a/tests/network.py +++ b/tests/network.py @@ -1,4 +1,8 @@ +from types import SimpleNamespace +from unittest.mock import MagicMock + from netbox_agent.lldp import LLDP +from netbox_agent.network import Network from tests.conftest import parametrize_with_fixtures @@ -34,3 +38,18 @@ def test_lldp_parse_with_vlan(fixture): lldp = LLDP(fixture) assert lldp.get_switch_vlan("eth0") == {"300": {"pvid": True}} assert lldp.get_switch_vlan("eth1") == {"300": {}} + + +def test_set_netbox_interface_primary_mac_scopes_lookup_to_interface(): + network = object.__new__(Network) + network.nb_net = SimpleNamespace(mac_addresses=MagicMock()) + network.nb_net.mac_addresses.get.return_value = SimpleNamespace(id=123) + interface = SimpleNamespace(id=456, primary_mac_address=None) + + network.set_netbox_interface_primary_mac(interface, "AA:BB:CC:DD:EE:FF") + + network.nb_net.mac_addresses.get.assert_called_once_with( + interface_id=456, + mac_address="AA:BB:CC:DD:EE:FF", + ) + assert interface.primary_mac_address == 123