Skip to content

Commit

Permalink
Fixes: #18436 - Prevent unassigning mac address when primary on an in…
Browse files Browse the repository at this point in the history
…terface
  • Loading branch information
DanSheps committed Jan 21, 2025
1 parent d11deb6 commit 277acd3
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

from core.models import ObjectType
from dcim.choices import *
from dcim.constants import *
from dcim.fields import MACAddressField
Expand Down Expand Up @@ -1522,3 +1523,26 @@ class Meta:

def __str__(self):
return str(self.mac_address)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# Denote the original assigned object (if any) for validation in clean()
self._original_assigned_object_id = self.__dict__.get('assigned_object_id')
self._original_assigned_object_type_id = self.__dict__.get('assigned_object_type_id')

def clean(self, *args, **kwargs):
super().clean()
if self._original_assigned_object_id and self._original_assigned_object_type_id:
assigned_object = getattr(self.assigned_object, 'parent_object', None)
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 and not assigned_object:
raise ValidationError(
_("Cannot unassign MAC Address while it is designated as the primary MAC for an object")
)
elif original_assigned_object and original_assigned_object != assigned_object:
raise ValidationError(
_("Cannot reassign MAC Address while it is designated as the primary MAC for an object")
)

0 comments on commit 277acd3

Please sign in to comment.