Skip to content

Commit

Permalink
Fixes #18768: allow removing secondary MACAddress from interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jnovinger authored and jeremystretch committed Mar 3, 2025
1 parent 3e1d436 commit e86dba8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
5 changes: 4 additions & 1 deletion netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,10 @@ def clean(self, *args, **kwargs):
ct = ObjectType.objects.get_for_id(self._original_assigned_object_type_id)
original_assigned_object = ct.get_object_for_this_type(pk=self._original_assigned_object_id)

if original_assigned_object.primary_mac_address:
if (
original_assigned_object.primary_mac_address
and original_assigned_object.primary_mac_address.pk == self.pk
):
if not assigned_object:
raise ValidationError(
_("Cannot unassign MAC Address while it is designated as the primary MAC for an object")
Expand Down
39 changes: 38 additions & 1 deletion netbox/dcim/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test import tag, TestCase

from circuits.models import *
from core.models import ObjectType
Expand All @@ -12,6 +12,43 @@
from virtualization.models import Cluster, ClusterType


class MACAddressTestCase(TestCase):
@classmethod
def setUpTestData(cls):
site = Site.objects.create(name='Test Site 1', slug='test-site-1')
manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
device_type = DeviceType.objects.create(
manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
)
device_role = DeviceRole.objects.create(name='Test Role 1', slug='test-role-1')
device = Device.objects.create(
name='Device 1', device_type=device_type, role=device_role, site=site,
)
cls.interface = Interface.objects.create(
device=device,
name='Interface 1',
type=InterfaceTypeChoices.TYPE_1GE_FIXED,
mgmt_only=True
)

cls.mac_a = MACAddress.objects.create(mac_address='1234567890ab', assigned_object=cls.interface)
cls.mac_b = MACAddress.objects.create(mac_address='1234567890ba', assigned_object=cls.interface)

cls.interface.primary_mac_address = cls.mac_a
cls.interface.save()

@tag('regression')
def test_clean_will_not_allow_removal_of_assigned_object_if_primary(self):
self.mac_a.assigned_object = None
with self.assertRaisesMessage(ValidationError, 'Cannot unassign MAC Address while'):
self.mac_a.clean()

@tag('regression')
def test_clean_will_allow_removal_of_assigned_object_if_not_primary(self):
self.mac_b.assigned_object = None
self.mac_b.clean()


class LocationTestCase(TestCase):

def test_change_location_site(self):
Expand Down

0 comments on commit e86dba8

Please sign in to comment.