Skip to content

Commit

Permalink
Fixes netbox-community#2012: Fixed deselection of an IP address as th…
Browse files Browse the repository at this point in the history
…e primary IP for its parent device/VM
  • Loading branch information
jeremystretch committed Apr 12, 2018
1 parent ef84889 commit bcb1d9a
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions netbox/ipam/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,22 +508,20 @@ def save(self, *args, **kwargs):

ipaddress = super(IPAddressForm, self).save(*args, **kwargs)

# Assign this IPAddress as the primary for the associated Device.
# Assign/clear this IPAddress as the primary for the associated Device/VirtualMachine.
if self.cleaned_data['primary_for_parent']:
parent = self.cleaned_data['interface'].parent
if ipaddress.address.version == 4:
parent.primary_ip4 = ipaddress
else:
parent.primary_ip6 = ipaddress
parent.save()

# Clear assignment as primary for device if set.
elif self.cleaned_data['interface']:
parent = self.cleaned_data['interface'].parent
if ipaddress.address.version == 4 and parent.primary_ip4 == self:
if ipaddress.address.version == 4 and parent.primary_ip4 == ipaddress:
parent.primary_ip4 = None
parent.save()
elif ipaddress.address.version == 6 and parent.primary_ip6 == self:
elif ipaddress.address.version == 6 and parent.primary_ip6 == ipaddress:
parent.primary_ip6 = None
parent.save()

Expand Down

0 comments on commit bcb1d9a

Please sign in to comment.