Skip to content

Commit b64b19a

Browse files
committed
Fixes #11934: Prevent reassignment of an IP address designated as primary for its parent object
1 parent 24a51dd commit b64b19a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

docs/release-notes/version-3.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Bug Fixes
66

7+
* [#11934](https://github.com/netbox-community/netbox/issues/11934) - Prevent reassignment of an IP address designated as primary for its parent object
78
* [#12694](https://github.com/netbox-community/netbox/issues/12694) - Strip leading & trailing whitespace from custom link URL & text
89

910
---

netbox/ipam/forms/model_forms.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,12 @@ def __init__(self, *args, **kwargs):
328328
):
329329
self.initial['primary_for_parent'] = True
330330

331+
# Disable object assignment fields if the IP address is designated as primary
332+
if self.initial.get('primary_for_parent'):
333+
self.fields['interface'].disabled = True
334+
self.fields['vminterface'].disabled = True
335+
self.fields['fhrpgroup'].disabled = True
336+
331337
def clean(self):
332338
super().clean()
333339

@@ -340,7 +346,12 @@ def clean(self):
340346
selected_objects[1]: "An IP address can only be assigned to a single object."
341347
})
342348
elif selected_objects:
343-
self.instance.assigned_object = self.cleaned_data[selected_objects[0]]
349+
assigned_object = self.cleaned_data[selected_objects[0]]
350+
if self.cleaned_data['primary_for_parent'] and assigned_object != self.instance.assigned_object:
351+
raise ValidationError(
352+
"Cannot reassign IP address while it is designated as the primary IP for the parent object"
353+
)
354+
self.instance.assigned_object = assigned_object
344355
else:
345356
self.instance.assigned_object = None
346357

0 commit comments

Comments
 (0)