Skip to content

Commit f95e510

Browse files
committed
Fixes #8102: Raise validation error when attempting to assign an IP address to multiple objects
1 parent 82932ae commit f95e510

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

docs/release-notes/version-3.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* [#8088](https://github.com/netbox-community/netbox/issues/8088) - Improve legibility of text in labels with light-colored backgrounds
2323
* [#8092](https://github.com/netbox-community/netbox/issues/8092) - Rack elevations should not include device asset tags
2424
* [#8096](https://github.com/netbox-community/netbox/issues/8096) - Fix DataError during change logging of objects with very long string representations
25-
* [#8102](https://github.com/netbox-community/netbox/issues/8102) - Cause validation error when editing IPAddress when more than one object is selected for assignment
25+
* [#8102](https://github.com/netbox-community/netbox/issues/8102) - Raise validation error when attempting to assign an IP address to multiple objects
2626

2727
---
2828

netbox/ipam/forms/models.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -461,27 +461,16 @@ def __init__(self, *args, **kwargs):
461461
def clean(self):
462462
super().clean()
463463

464-
if self.cleaned_data['interface'] and self.cleaned_data['vminterface'] and self.cleaned_data['fhrpgroup']:
465-
self.add_error('interface', "Can only assign an interface, VM interface or FHRP group")
466-
self.add_error('vminterface', "Can only assign an interface, VM interface or FHRP group")
467-
self.add_error('fhrpgroup', "Can only assign an interface, VM interface or FHRP group")
468-
elif self.cleaned_data['interface'] and self.cleaned_data['vminterface']:
469-
self.add_error('interface', "Can only assign an interface or VM interface")
470-
self.add_error('vminterface', "Can only assign an interface or VM interface")
471-
elif self.cleaned_data['interface'] and self.cleaned_data['fhrpgroup']:
472-
self.add_error('interface', "Can only assign an interface or FHRP group")
473-
self.add_error('fhrpgroup', "Can only assign an interface or FHRP group")
474-
elif self.cleaned_data['vminterface'] and self.cleaned_data['fhrpgroup']:
475-
self.add_error('vminterface', "Can only assign an VM interface or FHRP group")
476-
self.add_error('fhrpgroup', "Can only assign an VM interface or FHRP group")
477-
478464
# Handle object assignment
479-
if self.cleaned_data['interface']:
480-
self.instance.assigned_object = self.cleaned_data['interface']
481-
elif self.cleaned_data['vminterface']:
482-
self.instance.assigned_object = self.cleaned_data['vminterface']
483-
elif self.cleaned_data['fhrpgroup']:
484-
self.instance.assigned_object = self.cleaned_data['fhrpgroup']
465+
selected_objects = [
466+
field for field in ('interface', 'vminterface', 'fhrpgroup') if self.cleaned_data[field]
467+
]
468+
if len(selected_objects) > 1:
469+
raise forms.ValidationError({
470+
selected_objects[1]: "An IP address can only be assigned to a single object."
471+
})
472+
elif selected_objects:
473+
self.instance.assigned_object = self.cleaned_data[selected_objects[0]]
485474

486475
# Primary IP assignment is only available if an interface has been assigned.
487476
interface = self.cleaned_data.get('interface') or self.cleaned_data.get('vminterface')

0 commit comments

Comments
 (0)