From c8a1043b9b28ed40e4f26ab58969ffc6fdbd4939 Mon Sep 17 00:00:00 2001 From: Toma Tomov Date: Sat, 23 May 2026 18:53:52 +0300 Subject: [PATCH] network: resolve primary_mac_address by MAC object id On NetBox 4.2+ MAC addresses are first-class objects and the same MAC string can legitimately exist on multiple interfaces (VLAN/VXLAN stacks inherit the lower device's MAC, bonded slaves share the bond MAC, IPMI sometimes overlaps with a regular NIC, etc.). Sending `primary_mac_address = {"mac_address": ""}` makes NetBox resolve the FK by string, which fails as soon as more than one MAC object matches: 400 Bad Request: {'primary_mac_address': ["Multiple objects match the provided attributes: {'mac_address': 'A6:73:11:EE:F7:64'}"]} Resolve the MAC object scoped to the current interface (which is always unique) and assign primary_mac_address by its numeric id instead. Fall back to the previous behaviour if the object can't be found. Fixes: #371 --- netbox_agent/network.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index c2706108..4e935c6a 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -551,7 +551,16 @@ 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"]} + # Resolve the MAC object scoped to THIS interface so the + # lookup is unambiguous when the same MAC string exists on + # multiple interfaces (e.g. vlan/vxlan stacks, bonds, ipmi). + nb_mac_obj = self.nb_net.mac_addresses.get( + interface_id=interface.id, mac_address=nic["mac"] + ) + if nb_mac_obj: + interface.primary_mac_address = nb_mac_obj.id + else: + interface.primary_mac_address = {"mac_address": nic["mac"]} nic_update += 1 if hasattr(interface, "mtu"):