From e86dba8fc8addb73215721492f3072c84f3c76e8 Mon Sep 17 00:00:00 2001 From: Jason Novinger Date: Fri, 28 Feb 2025 15:42:38 -0600 Subject: [PATCH] Fixes #18768: allow removing secondary MACAddress from interface --- netbox/dcim/models/devices.py | 5 +++- netbox/dcim/tests/test_models.py | 39 +++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index a4da28803..78ffe6b66 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -1550,7 +1550,10 @@ class MACAddress(PrimaryModel): 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") diff --git a/netbox/dcim/tests/test_models.py b/netbox/dcim/tests/test_models.py index c8c84dafb..398945f93 100644 --- a/netbox/dcim/tests/test_models.py +++ b/netbox/dcim/tests/test_models.py @@ -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 @@ -12,6 +12,43 @@ from utilities.data import drange 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):