Add auto A/AAAA creating when ptr modified & fix bug w/ prefix serial

This commit is contained in:
rdujardin 2016-07-27 16:09:38 +02:00
parent ee78bf5455
commit ff5d770766
2 changed files with 33 additions and 4 deletions

View File

@ -493,6 +493,7 @@ class IPAddress(CreatedUpdatedModel):
raise ValidationError("Duplicate IP address found in global table: {}".format(duplicate_ips.first()))
def save(self, *args, **kwargs):
self.update_dns()
if self.address:
# Infer address family from IPAddress object
self.family = self.address.version
@ -501,6 +502,31 @@ class IPAddress(CreatedUpdatedModel):
r.save()
super(IPAddress, self).save(*args, **kwargs)
def update_dns(self):
"""Auto-create a corresponding A/AAAA DNS record (if possible) whenever the PTR field is modified"""
if self.ptr:
which_zone = None
zones = dns.models.Zone.objects.all()
for zone in zones:
if self.ptr.endswith(zone.name):
which_zone = zone
break
if which_zone:
zone_name = which_zone.name
record_name = self.ptr[:-len(zone_name)]
if record_name.endswith('.'):
record_name = record_name[:-1]
record_type = 'A' if self.family==4 else 'AAAA'
dns.models.Record.objects.get_or_create(
name = record_name,
record_type = record_type,
zone = which_zone,
address = self,
description = 'gen by netbox'
)
def to_csv(self):
# Determine if this IP is primary for a Device

View File

@ -354,11 +354,14 @@ class PrefixBulkEditView(PermissionRequiredMixin, BulkEditView):
fields_to_update['vrf'] = form.cleaned_data['vrf']
elif form.cleaned_data['vrf_global']:
fields_to_update['vrf'] = None
for field in ['site', 'status', 'role', 'description']:
for field in ['site', 'status', 'role', 'description', 'ttl', 'soa_name', 'soa_contact', 'soa_refresh', 'soa_retry', 'soa_expire', 'soa_minimum']:
if form.cleaned_data[field]:
fields_to_update[field] = form.cleaned_data[field]
return self.cls.objects.filter(pk__in=pk_list).update(**fields_to_update)
plist = self.cls.objects.filter(pk__in=pk_list)
for p in plist:
p.save()
return plist.update(**fields_to_update)
class PrefixBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
@ -484,13 +487,13 @@ class IPAddressBulkEditView(PermissionRequiredMixin, BulkEditView):
fields_to_update['vrf'] = form.cleaned_data['vrf']
elif form.cleaned_data['vrf_global']:
fields_to_update['vrf'] = None
for field in ['description']:
for field in ['ptr', 'description']:
if form.cleaned_data[field]:
fields_to_update[field] = form.cleaned_data[field]
iplist = self.cls.objects.filter(pk__in=pk_list)
for ip in iplist:
ip.save() # in order to update dns zones serials
ip.save()
return iplist.update(**fields_to_update)