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

Do not clear DNS name if no coupled DNS record has been set #191

Merged
merged 2 commits into from
Mar 1, 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
14 changes: 13 additions & 1 deletion netbox_dns/signals/ipam_coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,27 @@ def ip_address_update_dns_information(instance, **kwargs):

name, ttl, disable_ptr, zone_id = ipaddress_cf_data(instance)

previous_zone_id = None
if instance.pk is not None:
try:
old_instance = IPAddress.objects.get(pk=instance.pk)
previous_zone_id = old_instance.custom_field_data.get(
"ipaddress_dns_zone_id"
)
except IPAddress.DoesNotExist:
pass

if zone_id is not None:
instance.dns_name = f"{name}.{Zone.objects.get(pk=zone_id).name}"
else:
instance.dns_name = ""
instance.custom_field_data["ipaddress_dns_record_name"] = None
instance.custom_field_data["ipaddress_dns_record_ttl"] = None
instance.custom_field_data["ipaddress_dns_record_disable_ptr"] = False
instance.custom_field_data["ipaddress_dns_zone_id"] = None

if previous_zone_id is not None:
instance.dns_name = ""


#
# Handle DNS record operation after IPAddress has been created or modified
Expand Down
19 changes: 19 additions & 0 deletions netbox_dns/tests/ipam_coupling/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,22 @@ def test_clear_disable_ptr_cf(self):
"ipaddress_dns_record_disable_ptr": False,
},
)

@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_dont_clear_existing_dns_name(self):
address = IPNetwork("10.0.0.27/24")
dns_name = "test.example.com"

ip_address = IPAddress.objects.create(
address=address,
dns_name=dns_name,
custom_field_data={
"ipaddress_dns_zone_id": None,
"ipaddress_dns_record_name": None,
"ipaddress_dns_record_ttl": None,
"ipaddress_dns_record_disable_ptr": True,
},
)
record_query = Record.objects.filter(ipam_ip_address=ip_address)
self.assertFalse(record_query.exists())
self.assertEqual(ip_address.dns_name, dns_name)