13130 dont allow reassigning ipaddress assigned object if primary ip (#13893)

* 13130 dont allow reassigning ipaddress assigned object if primary ip

* 13130 add tests fix parent check

* Misc cleanup

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Arthur Hanson
2023-09-26 12:16:02 -07:00
committed by GitHub
parent f65744faee
commit db40119faa
2 changed files with 83 additions and 0 deletions

View File

@@ -782,6 +782,13 @@ class IPAddress(PrimaryModel):
def __str__(self):
return str(self.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 get_absolute_url(self):
return reverse('ipam:ipaddress', args=[self.pk])
@@ -843,6 +850,26 @@ class IPAddress(PrimaryModel):
)
})
if self._original_assigned_object_id and self._original_assigned_object_type_id:
parent = getattr(self.assigned_object, 'parent_object', None)
ct = ContentType.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)
original_parent = getattr(original_assigned_object, 'parent_object', None)
# can't use is_primary_ip as self.assigned_object might be changed
is_primary = False
if self.family == 4 and hasattr(original_parent, 'primary_ip4') and original_parent.primary_ip4_id == self.pk:
is_primary = True
if self.family == 6 and hasattr(original_parent, 'primary_ip6') and original_parent.primary_ip6_id == self.pk:
is_primary = True
if is_primary and (parent != original_parent):
raise ValidationError({
'assigned_object': _(
"Cannot reassign IP address while it is designated as the primary IP for the parent object"
)
})
# Validate IP status selection
if self.status == IPAddressStatusChoices.STATUS_SLAAC and self.family != 6:
raise ValidationError({