Skip to content

Commit

Permalink
Fix IP coupling when zone is renamed or deleted
Browse files Browse the repository at this point in the history
When a zone is renamed, and an (IPAM) IP address is linked to a "A" or
"AAAA" DNS record from this zone, the dns_name field is updated.

When a zone is deleted, and an (IPAM) IP address is linked to a "A" or
"AAAA" DNS record from this zone, the dns_name field and custom field
"name" are emptied.
  • Loading branch information
jean1 committed Oct 5, 2023
1 parent d27ea60 commit 4a36310
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions netbox_dns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from dns.rdtypes.ANY import SOA
from dns.exception import DNSException

from netaddr import IPNetwork, AddrFormatError
from ipam.models import IPAddress
from netaddr import IPNetwork, AddrFormatError, IPAddress
from ipam.models import IPAddress as IP

from django.core.validators import (
MinValueValidator,
Expand Down Expand Up @@ -557,9 +557,9 @@ def save(self, *args, **kwargs):
):
record.update_ptr_record()

# Fix name in IPAddress when zone name is changed
# Fix name in IP Address when zone name is changed
if get_plugin_config("netbox_dns", "feature_ipam_coupling") and name_changed:
for ip in IPAddress.objects.filter(custom_field_data__zone=self.pk):
for ip in IP.objects.filter(custom_field_data__zone=self.pk):
ip.dns_name = f'{ip.custom_field_data["name"]}.{self.name}'
ip.save(update_fields=["dns_name"])

Expand All @@ -579,7 +579,7 @@ def delete(self, *args, **kwargs):

if get_plugin_config("netbox_dns", "feature_ipam_coupling"):
# Remove coupling from IPAddress to DNS record when zone is deleted
for ip in IPAddress.objects.filter(custom_field_data__zone=self.pk):
for ip in IP.objects.filter(custom_field_data__zone=self.pk):
ip.dns_name = ""
ip.custom_field_data["name"] = ""
ip.custom_field_data["dns_record"] = None
Expand Down

0 comments on commit 4a36310

Please sign in to comment.