From ff7bd042ef686472137ce9120f27b34161c24ac4 Mon Sep 17 00:00:00 2001 From: Charlie-Root Date: Wed, 25 Feb 2026 07:53:33 +0100 Subject: [PATCH 1/3] fix --- netbox_agent/network.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index bb1d1610..5d7196e2 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -337,10 +337,11 @@ def _update_interface_macs_legacy(self, nic, macs): ) def _update_interface_macs_new(self, nic, macs): - if hasattr(self.nb_net, 'virtual_machines'): - object_type = 'virtualization.vminterface' - else: - object_type = 'dcim.interface' + object_type = ( + "virtualization.vminterface" + if self.assigned_object_type == "virtualization.vminterface" + else "dcim.interface" + ) try: nb_macs = list(self.nb.dcim.mac_addresses.filter( assigned_object_type=object_type, @@ -546,6 +547,7 @@ def create_or_update_netbox_network_cards(self): if config.update_all is None or config.update_network is None: return None logging.debug("Creating/Updating NIC...") + is_netbox_42_plus = version.parse(nb.version) >= version.parse("4.2") # delete unknown interface nb_nics = list(self.get_netbox_network_cards()) @@ -602,12 +604,6 @@ def batched(it, n): interface.name = nic['name'] nic_update += 1 - if nic['mac'] != interface.mac_address: - logging.info('Updating interface {interface} mac_address to: {mac}'.format( - interface=interface, mac=nic['mac'])) - interface.mac_address = nic['mac'] - nic_update += 1 - ret, interface = self.reset_vlan_on_interface(nic, interface) nic_update += ret @@ -620,21 +616,16 @@ def batched(it, n): interface.name = nic["name"] nic_update += 1 - if version.parse(nb.version) >= version.parse("4.2"): - # Create MAC objects + if is_netbox_42_plus: if nic["mac"]: self.update_interface_macs(interface, [nic["mac"]]) - - if nic["mac"] and nic["mac"] != interface.mac_address: + elif nic["mac"] and nic["mac"] != interface.mac_address: logging.info( - "Updating interface {interface} mac to: {mac}".format( + "Updating interface {interface} mac_address to: {mac}".format( interface=interface, mac=nic["mac"] ) ) - if version.parse(nb.version) < version.parse("4.2"): - interface.mac_address = nic["mac"] - else: - interface.primary_mac_address = {"mac_address": nic["mac"]} + interface.mac_address = nic["mac"] nic_update += 1 if hasattr(interface, "mtu"): From 72f197569f80bc6a24f11dedd7a8c4e81fddda5c Mon Sep 17 00:00:00 2001 From: Charlie-Root Date: Wed, 25 Feb 2026 08:05:34 +0100 Subject: [PATCH 2/3] fix --- netbox_agent/network.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index 5d7196e2..8f968149 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -619,6 +619,8 @@ def batched(it, n): if is_netbox_42_plus: if nic["mac"]: self.update_interface_macs(interface, [nic["mac"]]) + # Refresh interface so future saves don't include stale primary MAC diffs + interface = self.nb_net.interfaces.get(id=interface.id) elif nic["mac"] and nic["mac"] != interface.mac_address: logging.info( "Updating interface {interface} mac_address to: {mac}".format( From 8ed4857b6b811944aa9e83cd8d2e482edb94c383 Mon Sep 17 00:00:00 2001 From: Charlie-Root Date: Wed, 25 Feb 2026 08:47:15 +0100 Subject: [PATCH 3/3] fix --- netbox_agent/network.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index 8f968149..7ceb7173 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -684,6 +684,15 @@ def batched(it, n): # sync local IPs for ip in nic["ip"]: self.create_or_update_netbox_ip_on_interface(ip, interface) + + if is_netbox_42_plus: + primary_obj = getattr(interface, "primary_mac_address", None) + if primary_obj and not isinstance(primary_obj, int): + primary_id = getattr(primary_obj, "id", None) + if primary_id is None and isinstance(primary_obj, dict): + primary_id = primary_obj.get("id") + if primary_id: + interface.primary_mac_address = primary_id if nic_update > 0: interface.save()