From b64b19a3f4011be1fc3889d2668b26e3135cadf2 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Thu, 25 May 2023 16:42:24 -0400 Subject: [PATCH] Fixes #11934: Prevent reassignment of an IP address designated as primary for its parent object --- docs/release-notes/version-3.5.md | 1 + netbox/ipam/forms/model_forms.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/release-notes/version-3.5.md b/docs/release-notes/version-3.5.md index 1154d4fcd8..816fe124c5 100644 --- a/docs/release-notes/version-3.5.md +++ b/docs/release-notes/version-3.5.md @@ -4,6 +4,7 @@ ### Bug Fixes +* [#11934](https://github.com/netbox-community/netbox/issues/11934) - Prevent reassignment of an IP address designated as primary for its parent object * [#12694](https://github.com/netbox-community/netbox/issues/12694) - Strip leading & trailing whitespace from custom link URL & text --- diff --git a/netbox/ipam/forms/model_forms.py b/netbox/ipam/forms/model_forms.py index ac75e2cc30..eb6dbe598e 100644 --- a/netbox/ipam/forms/model_forms.py +++ b/netbox/ipam/forms/model_forms.py @@ -328,6 +328,12 @@ def __init__(self, *args, **kwargs): ): self.initial['primary_for_parent'] = True + # Disable object assignment fields if the IP address is designated as primary + if self.initial.get('primary_for_parent'): + self.fields['interface'].disabled = True + self.fields['vminterface'].disabled = True + self.fields['fhrpgroup'].disabled = True + def clean(self): super().clean() @@ -340,7 +346,12 @@ def clean(self): selected_objects[1]: "An IP address can only be assigned to a single object." }) elif selected_objects: - self.instance.assigned_object = self.cleaned_data[selected_objects[0]] + assigned_object = self.cleaned_data[selected_objects[0]] + if self.cleaned_data['primary_for_parent'] and assigned_object != self.instance.assigned_object: + raise ValidationError( + "Cannot reassign IP address while it is designated as the primary IP for the parent object" + ) + self.instance.assigned_object = assigned_object else: self.instance.assigned_object = None