Skip to content

Commit

Permalink
Provided a mechanism to turn off optimisation for rebuild_dnssync
Browse files Browse the repository at this point in the history
  • Loading branch information
peteeckel committed Sep 27, 2024
1 parent 8c1dec6 commit 48bc8c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 14 additions & 1 deletion netbox_dns/management/commands/rebuild_dnssync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@
class Command(BaseCommand):
help = "Rebuild DNSsync relationships between IP addresses and records"

def add_arguments(self, parser):
parser.add_argument(
"--force",
action="store_true",
help="Update records even if DNS name was not changed (required for rebuilding filtered views",
)

def handle(self, *model_names, **options):
ip_addresses = IPAddress.objects.all()
for ip_address in ip_addresses:
if options.get("verbosity") >= 2:
self.stdout.write(
f"Updating DNS records for IP Address {ip_address}, VRF {ip_address.vrf}"
)
update_dns_records(ip_address)
if (
update_dns_records(ip_address, force=options.get("force"))
and options.get("verbosity") >= 1
):
self.stdout.write(
f"Updated DNS records for IP Address {ip_address}, VRF {ip_address.vrf}"
)
19 changes: 15 additions & 4 deletions netbox_dns/utilities/ipam_dnssync.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ def check_dns_records(ip_address, zone=None, view=None):
record.clean(new_zone=new_zone)


def update_dns_records(ip_address, view=None):
def update_dns_records(ip_address, view=None, force=False):
from netbox_dns.models import Zone, Record

updated = False

if ip_address.dns_name == "":
delete_dns_records(ip_address)
return
return delete_dns_records(ip_address)

zones = get_zones(ip_address, view=view)

Expand All @@ -178,16 +179,19 @@ def update_dns_records(ip_address, view=None):
"ipaddress_dns_disabled"
):
record.delete()
updated = True
continue

record.update_fqdn()
if not _match_data(ip_address, record):
if not _match_data(ip_address, record) or force:
updated, deleted = record.update_from_ip_address(ip_address)

if deleted:
record.delete()
updated = True
elif updated:
record.save()
updated = True

zones = Zone.objects.filter(pk__in=[zone.pk for zone in zones]).exclude(
pk__in=set(
Expand All @@ -203,9 +207,13 @@ def update_dns_records(ip_address, view=None):

if record is not None:
record.save()
updated = True

return updated


def delete_dns_records(ip_address, view=None):
deleted = False

if view is None:
address_records = ip_address.netbox_dns_records.all()
Expand All @@ -214,6 +222,9 @@ def delete_dns_records(ip_address, view=None):

for record in address_records:
record.delete()
deleted = True

return deleted


def get_views_by_prefix(prefix):
Expand Down

0 comments on commit 48bc8c7

Please sign in to comment.