Skip to content

fix: match interfaces by the primary MAC address object on NetBox >= 4.2 - #585

Merged
semx merged 1 commit into
bb-Ricardo:developmentfrom
tfoks:fix/interface-match-primary-mac
Oct 8, 2026
Merged

semx merged 1 commit into
bb-Ricardo:developmentfrom
tfoks:fix/interface-match-primary-mac

Conversation

@tfoks

@tfoks tfoks commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

On NetBox >= 4.2, map_object_interfaces_to_current_interfaces() now matches interfaces by the MAC address of their primary_mac_address object. Before, it used the legacy mac_address field. That field keeps the value NetBox returned at read time and isn't updated during a run, so a source could match against a MAC address that an earlier source had already replaced. NetBox < 4.2 keeps using mac_address.

With a VMware and a check_redfish source syncing the same ESXi hosts, this merged the Lenovo XCC port into vmk0 on every run, which moved the XCC IP to vmk0 and unset the device's primary IP. Details in #584.

Changes:

  • module/sources/common/source_base.py: read the MAC address from the primary MAC address object on NetBox >= 4.2
  • tests/test_interface_matching_primary_mac.py: a stale legacy MAC no longer matches, and the primary MAC still does. Both tests fail without the fix.

Tested against NetBox 4.7.2 with a vCenter 8.0.3 source and the check_redfish inventory files of HPE and Lenovo hosts: the XCC ports get their own interfaces again, vmk0 keeps its MAC and management IP, and a second run makes no changes. pytest: 259 passed (vcsim installed).

Fixes #584

🤖 Generated with Claude Code

From NetBox 4.2 on, sources only maintain the interface's primary MAC
address object. The legacy 'mac_address' field keeps the value NetBox
returned when the data was read and is never updated during a run.

map_object_interfaces_to_current_interfaces() still matched on that
field. If one source changed an interface's primary MAC address, the
next source still saw the old MAC address. With a VMware and a
check_redfish source syncing the same ESXi hosts, a BMC port (i.e.
Lenovo XCC) whose MAC address had ended up on vmk0 was merged into vmk0
on every run. The BMC IP moved to vmk0, the host's management IP was
removed and the primary IP of the device got unset.

Use the MAC address of the primary MAC address object on NetBox >= 4.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tfoks
tfoks requested review from bb-Ricardo and semx as code owners October 8, 2026 06:49
@semx
semx changed the base branch from main to development October 8, 2026 11:27

@semx semx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced on a NetBox 4.7.2 with vcsim and a check_redfish inventory for the same host, the BMC MAC planted on vmk0 as its primary MAC the way an earlier run leaves it. Without this change the BMC port is merged into vmk0 on every run: vmk0 becomes mgmt_only, gets the BMC IP, loses the host IP and the device loses primary_ip4; the next run flips it back. With it, vmk0 gets its own MAC back, the BMC port gets its own interface with its IP, primary_ip4 stays, and the second run changes nothing. Both new tests fail on development without the fix; the suite is green with it.

Two things, neither blocks:

  • The < 4.2 branch has no test: dropping the version check and always reading the primary MAC object passes the whole suite. One case with inventory.netbox_api_version = "4.1.0" and an interface that only has mac_address would pin it. Fine to add in a follow-up if you prefer.
  • get_object_based_on_macs() in the VMware source still identifies hosts and VMs by the legacy mac_address. Same staleness, different place; I'll track it separately, it is not part of this fix.

Branch is off main again; I'll switch the base to development and merge once CI has run.

@semx
semx merged commit 426958d into bb-Ricardo:development Oct 8, 2026
3 checks passed
@semx semx mentioned this pull request Oct 8, 2026
@tfoks
tfoks deleted the fix/interface-match-primary-mac branch October 12, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interface matching uses stale legacy mac_address on NetBox >= 4.2, BMC port merged into vmk0 on every run

2 participants