Skip to content

Commit

Permalink
Fixes netbox-community#1330: Raise validation error when assigning an…
Browse files Browse the repository at this point in the history
… unrelated IP as the primary IP for a device
  • Loading branch information
jeremystretch committed Aug 15, 2017
1 parent 52e0f89 commit 033f182
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,18 @@ def clean(self):
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)
Expand Down

0 comments on commit 033f182

Please sign in to comment.