Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion netbox_agent/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"):
Expand Down
19 changes: 19 additions & 0 deletions tests/network.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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