Skip to content

Commit

Permalink
Fixes #1279: Fix primary_ip assignment during IP address import
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jun 16, 2017
1 parent ea869d4 commit afdf575
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
17 changes: 12 additions & 5 deletions netbox/ipam/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,16 +641,23 @@ def save(self, *args, **kwargs):

# Set interface
if self.cleaned_data['device'] and self.cleaned_data['interface_name']:
self.instance.interface = Interface.objects.get(device=self.cleaned_data['device'],
name=self.cleaned_data['interface_name'])
self.instance.interface = Interface.objects.get(
device=self.cleaned_data['device'],
name=self.cleaned_data['interface_name']
)

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

# Set as primary for device
if self.cleaned_data['is_primary']:
device = self.cleaned_data['device']
if self.instance.address.version == 4:
self.instance.primary_ip4_for = self.cleaned_data['device']
device.primary_ip4 = ipaddress
elif self.instance.address.version == 6:
self.instance.primary_ip6_for = self.cleaned_data['device']
device.primary_ip6 = ipaddress
device.save()

return super(IPAddressCSVForm, self).save(*args, **kwargs)
return ipaddress


class IPAddressBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
Expand Down
13 changes: 0 additions & 13 deletions netbox/ipam/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,19 +665,6 @@ class IPAddressBulkImportView(PermissionRequiredMixin, BulkImportView):
table = tables.IPAddressTable
default_return_url = 'ipam:ipaddress_list'

def save_obj(self, obj):
obj.save()

# Update primary IP for device if needed. The Device must be updated directly in the database; otherwise we risk
# overwriting a previous IP assignment from the same import (see #861).
try:
if obj.family == 4 and obj.primary_ip4_for:
Device.objects.filter(pk=obj.primary_ip4_for.pk).update(primary_ip4=obj)
elif obj.family == 6 and obj.primary_ip6_for:
Device.objects.filter(pk=obj.primary_ip6_for.pk).update(primary_ip6=obj)
except Device.DoesNotExist:
pass


class IPAddressBulkEditView(PermissionRequiredMixin, BulkEditView):
permission_required = 'ipam.change_ipaddress'
Expand Down

0 comments on commit afdf575

Please sign in to comment.