Skip to content

Commit e86dba8

Browse files
jnovingerjeremystretch
authored andcommitted
Fixes #18768: allow removing secondary MACAddress from interface
1 parent 3e1d436 commit e86dba8

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

netbox/dcim/models/devices.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,10 @@ def clean(self, *args, **kwargs):
15501550
ct = ObjectType.objects.get_for_id(self._original_assigned_object_type_id)
15511551
original_assigned_object = ct.get_object_for_this_type(pk=self._original_assigned_object_id)
15521552

1553-
if original_assigned_object.primary_mac_address:
1553+
if (
1554+
original_assigned_object.primary_mac_address
1555+
and original_assigned_object.primary_mac_address.pk == self.pk
1556+
):
15541557
if not assigned_object:
15551558
raise ValidationError(
15561559
_("Cannot unassign MAC Address while it is designated as the primary MAC for an object")

netbox/dcim/tests/test_models.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.core.exceptions import ValidationError
2-
from django.test import TestCase
2+
from django.test import tag, TestCase
33

44
from circuits.models import *
55
from core.models import ObjectType
@@ -12,6 +12,43 @@
1212
from virtualization.models import Cluster, ClusterType
1313

1414

15+
class MACAddressTestCase(TestCase):
16+
@classmethod
17+
def setUpTestData(cls):
18+
site = Site.objects.create(name='Test Site 1', slug='test-site-1')
19+
manufacturer = Manufacturer.objects.create(name='Test Manufacturer 1', slug='test-manufacturer-1')
20+
device_type = DeviceType.objects.create(
21+
manufacturer=manufacturer, model='Test Device Type 1', slug='test-device-type-1'
22+
)
23+
device_role = DeviceRole.objects.create(name='Test Role 1', slug='test-role-1')
24+
device = Device.objects.create(
25+
name='Device 1', device_type=device_type, role=device_role, site=site,
26+
)
27+
cls.interface = Interface.objects.create(
28+
device=device,
29+
name='Interface 1',
30+
type=InterfaceTypeChoices.TYPE_1GE_FIXED,
31+
mgmt_only=True
32+
)
33+
34+
cls.mac_a = MACAddress.objects.create(mac_address='1234567890ab', assigned_object=cls.interface)
35+
cls.mac_b = MACAddress.objects.create(mac_address='1234567890ba', assigned_object=cls.interface)
36+
37+
cls.interface.primary_mac_address = cls.mac_a
38+
cls.interface.save()
39+
40+
@tag('regression')
41+
def test_clean_will_not_allow_removal_of_assigned_object_if_primary(self):
42+
self.mac_a.assigned_object = None
43+
with self.assertRaisesMessage(ValidationError, 'Cannot unassign MAC Address while'):
44+
self.mac_a.clean()
45+
46+
@tag('regression')
47+
def test_clean_will_allow_removal_of_assigned_object_if_not_primary(self):
48+
self.mac_b.assigned_object = None
49+
self.mac_b.clean()
50+
51+
1552
class LocationTestCase(TestCase):
1653

1754
def test_change_location_site(self):

0 commit comments

Comments
 (0)