From 433c48a1a39312f53c4f131d964e87ba567fb3ad Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 17 Mar 2021 16:44:34 -0400 Subject: [PATCH] Fix IP address interface validation --- netbox/ipam/forms.py | 28 +++++++++++++++------------- netbox/templates/ipam/ipaddress.html | 5 +++-- netbox/virtualization/models.py | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/netbox/ipam/forms.py b/netbox/ipam/forms.py index 0262d85dd..36bef9031 100644 --- a/netbox/ipam/forms.py +++ b/netbox/ipam/forms.py @@ -833,7 +833,7 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel # Initialize primary_for_parent if IP address is already assigned if self.instance.pk and self.instance.assigned_object: - parent = self.instance.assigned_object.parent + parent = self.instance.assigned_object.parent_object if ( self.instance.address.version == 4 and parent.primary_ip4_id == self.instance.pk or self.instance.address.version == 6 and parent.primary_ip6_id == self.instance.pk @@ -860,18 +860,20 @@ class IPAddressForm(BootstrapMixin, TenancyForm, ReturnURLForm, CustomFieldModel # Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine. interface = self.instance.assigned_object - if interface and self.cleaned_data['primary_for_parent']: - if ipaddress.address.version == 4: - interface.parent.primary_ip4 = ipaddress - else: - interface.parent.primary_ip6 = ipaddress - interface.parent.save() - elif interface and ipaddress.address.version == 4 and interface.parent.primary_ip4 == ipaddress: - interface.parent.primary_ip4 = None - interface.parent.save() - elif interface and ipaddress.address.version == 6 and interface.parent.primary_ip6 == ipaddress: - interface.parent.primary_ip6 = None - interface.parent.save() + if interface: + parent = interface.parent_object + if self.cleaned_data['primary_for_parent']: + if ipaddress.address.version == 4: + parent.primary_ip4 = ipaddress + else: + parent.primary_ip6 = ipaddress + parent.save() + elif ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress: + parent.primary_ip4 = None + parent.save() + elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress: + parent.primary_ip6 = None + parent.save() return ipaddress diff --git a/netbox/templates/ipam/ipaddress.html b/netbox/templates/ipam/ipaddress.html index 518507978..f36d498d1 100644 --- a/netbox/templates/ipam/ipaddress.html +++ b/netbox/templates/ipam/ipaddress.html @@ -74,7 +74,8 @@ Assignment {% if object.assigned_object %} - {{ object.assigned_object.parent }} ({{ object.assigned_object }}) + {{ object.assigned_object.parent_object }} / + {{ object.assigned_object }} {% else %} {% endif %} @@ -86,7 +87,7 @@ {% if object.nat_inside %} {{ object.nat_inside }} {% if object.nat_inside.assigned_object %} - ({{ object.nat_inside.assigned_object.parent }}) + ({{ object.nat_inside.assigned_object.parent_object }}) {% endif %} {% else %} None diff --git a/netbox/virtualization/models.py b/netbox/virtualization/models.py index f904b6756..e26fc8fc9 100644 --- a/netbox/virtualization/models.py +++ b/netbox/virtualization/models.py @@ -459,7 +459,7 @@ class VMInterface(PrimaryModel, BaseInterface): return super().to_objectchange(action, related_object=self.virtual_machine) @property - def parent(self): + def parent_object(self): return self.virtual_machine @property