Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to turn off optimisation for rebuild_dnssync #411

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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