From 51b1da660aba8e7a97eab6eed6d569b41333ab86 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 15 Aug 2017 14:05:02 -0400 Subject: [PATCH] Fixes #1330: Raise validation error when assigning an unrelated IP as the primary IP for a device --- netbox/dcim/models.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 3719c7c25..9f72fc83e 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -890,6 +890,18 @@ class Device(CreatedUpdatedModel, CustomFieldModel): except DeviceType.DoesNotExist: pass + # Validate primary IPv4 address + if self.primary_ip4 and (self.primary_ip4.interface is None or self.primary_ip4.interface.device != self): + raise ValidationError({ + 'primary_ip4': "The specified IP address ({}) is not assigned to this device.".format(self.primary_ip4), + }) + + # Validate primary IPv6 address + if self.primary_ip6 and (self.primary_ip6.interface is None or self.primary_ip6.interface.device != self): + raise ValidationError({ + 'primary_ip6': "The specified IP address ({}) is not assigned to this device.".format(self.primary_ip6), + }) + def save(self, *args, **kwargs): is_new = not bool(self.pk)