Skip to content

Commit

Permalink
Merge pull request #99 from peteeckel/fix/delete-ptr-records
Browse files Browse the repository at this point in the history
Fixed address record deletion when coupling fields are cleared
  • Loading branch information
peteeckel authored Nov 16, 2023
2 parents 53f401a + cc1d0f4 commit 1fae4bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
3 changes: 2 additions & 1 deletion netbox_dns/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ def post_save(self, sender, **kwargs):
#
# Name/Zone CF data has been removed: Remove the DNS address record
#
ip_address.netbox_dns_records.all().delete()
for record in ip_address.netbox_dns_records.all():
record.delete()

else:
#
Expand Down
55 changes: 30 additions & 25 deletions netbox_dns/tests/coupling/test_ip_dns_coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ class IPAddressDNSRecordCouplingTest(APITestCase):
def setUpTestData(cls):
# Test data
cls.nameserver = NameServer.objects.create(name=cls.ns)
cls.zone = Zone.objects.create(
name="zone1.example.com", **cls.zone_data, soa_mname=cls.nameserver
)
cls.zone2 = Zone.objects.create(
name="zone2.example.com", **cls.zone_data, soa_mname=cls.nameserver
cls.zones = (
Zone(name="zone1.example.com", **cls.zone_data, soa_mname=cls.nameserver),
Zone(name="zone2.example.com", **cls.zone_data, soa_mname=cls.nameserver),
Zone(name="0.0.10.in-addr.arpa", **cls.zone_data, soa_mname=cls.nameserver),
)
for zone in cls.zones:
zone.save()

#
# Add the required custom fields
Expand All @@ -42,7 +43,7 @@ def setUpTestData(cls):

@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_create_ip(self):
zone = self.zone
zone = self.zones[0]
name = "test-create"
addr = "10.0.0.25/24"

Expand Down Expand Up @@ -74,7 +75,7 @@ def test_create_ip(self):

@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_create_ip_existing_dns_record(self):
zone = self.zone
zone = self.zones[0]
name = "test-create-ip-existing-dns-record"
addr = "10.0.0.25/24"

Expand Down Expand Up @@ -113,7 +114,7 @@ def test_create_ip_existing_dns_record(self):

@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_create_ip_missing_dns_permission(self):
zone = self.zone
zone = self.zones[0]
name = "test-create-ip-missing-dns-perm"
addr = "10.0.0.26/24"

Expand All @@ -140,7 +141,7 @@ def test_create_ip_missing_dns_permission(self):

@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_delete_ip(self):
zone = self.zone
zone = self.zones[0]
name = "test-delete-ip"
addr = IPNetwork("10.0.0.27/24")
# Grant delete on both IP address and DNS record
Expand All @@ -163,6 +164,7 @@ def test_delete_ip(self):
value=str(addr.ip),
ipam_ip_address=ip_address,
)
ptr_record_id = record.ptr_record.pk

# Delete address
url = reverse("ipam-api:ipaddress-list") + str(ip_address.id) + "/"
Expand All @@ -171,16 +173,17 @@ def test_delete_ip(self):
self.assertTrue(status.is_success(response.status_code))
# Check if DNS record has been deleted
self.assertFalse(Record.objects.filter(id=record.id).exists())
self.assertFalse(Record.objects.filter(id=ptr_record_id).exists())
# Check if IP address has been deleted
self.assertFalse(IPAddress.objects.filter(id=ip_address.id).exists())

@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_modify_name_existing_ip(self):
addr = IPNetwork("10.0.0.27/24")
zone = self.zone
zone1 = self.zones[0]
zone2 = self.zones[1]
name = "test-modify-name-existing-ip"
newname = "newname"
zone2 = self.zone2

# Grant permissions to user
self.add_permissions("ipam.change_ipaddress")
Expand All @@ -189,16 +192,16 @@ def test_modify_name_existing_ip(self):
# Create IP Address
ip_address = IPAddress.objects.create(
address=addr,
dns_name=f"{name}.{zone.name}",
dns_name=f"{name}.{zone1.name}",
custom_field_data={
"ipaddress_dns_record_name": name,
"ipaddress_dns_zone_id": zone.id,
"ipaddress_dns_zone_id": zone1.id,
},
)
# Create DNS record
Record.objects.create(
name=name,
zone=zone,
zone=zone1,
type=RecordTypeChoices.A,
value=str(addr.ip),
ipam_ip_address=ip_address,
Expand Down Expand Up @@ -229,28 +232,28 @@ def test_modify_name_existing_ip(self):
@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_modify_name_existing_ip_missing_dns_permission(self):
addr = IPNetwork("10.0.0.27/24")
zone = self.zone
zone1 = self.zones[0]
zone2 = self.zones[1]
name = "test-modify-name-existing-ip-no-perm"

newname = "newname"
zone2 = self.zone2

# Grant permissions to user
self.add_permissions("ipam.change_ipaddress")

# Create IP Address
ip_address = IPAddress.objects.create(
address=addr,
dns_name=f"{name}.{zone.name}",
dns_name=f"{name}.{zone1.name}",
custom_field_data={
"ipaddress_dns_record_name": name,
"ipaddress_dns_zone_id": zone.id,
"ipaddress_dns_zone_id": zone1.id,
},
)
# Create DNS record
Record.objects.create(
name=name,
zone=zone,
zone=zone1,
type=RecordTypeChoices.A,
value=str(addr.ip),
ipam_ip_address=ip_address,
Expand All @@ -274,14 +277,14 @@ def test_modify_name_existing_ip_missing_dns_permission(self):
record_query = Record.objects.filter(ipam_ip_address=ip_address)
self.assertEqual(record_query.count(), 1)
self.assertEqual(record_query[0].name, name)
self.assertEqual(record_query[0].zone, zone)
self.assertEqual(record_query[0].zone, zone1)
# Check value of dns_name
self.assertEqual(ip_address.dns_name, f"{name}.{zone.name}")
self.assertEqual(ip_address.dns_name, f"{name}.{zone1.name}")

@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_clear_name_existing_ip(self):
addr = IPNetwork("10.0.0.28/24")
zone = self.zone
zone = self.zones[0]
name = "test-clear-name-existing-ip"

# Grant permissions to user
Expand All @@ -298,13 +301,14 @@ def test_clear_name_existing_ip(self):
},
)
# Create DNS record
Record.objects.create(
record = Record.objects.create(
name=name,
zone=zone,
type=RecordTypeChoices.A,
value=str(addr.ip),
ipam_ip_address=ip_address,
)
ptr_record_id = record.ptr_record.pk

url = reverse("ipam-api:ipaddress-list") + str(ip_address.id) + "/"
data = {"custom_fields": {"ipaddress_dns_zone_id": None}}
Expand All @@ -314,6 +318,7 @@ def test_clear_name_existing_ip(self):
self.assertTrue(status.is_success(response.status_code))
# Check if record has been deleted
self.assertFalse(Record.objects.filter(ipam_ip_address=ip_address).exists())
self.assertFalse(Record.objects.filter(pk=ptr_record_id).exists())
# Re-read IPAddress object
ip_address = IPAddress.objects.get(id=ip_address.id)
# Check if dns_name is empty
Expand All @@ -324,7 +329,7 @@ def test_clear_name_existing_ip(self):
@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_rename_zone_existing_ip(self):
addr = IPNetwork("10.0.0.29/24")
zone = self.zone
zone = self.zones[0]
name = "test-rename-zone-existing-ip"
new_zone_name = "newzone.example.com"

Expand Down Expand Up @@ -368,7 +373,7 @@ def test_rename_zone_existing_ip(self):
@override_settings(PLUGINS_CONFIG={"netbox_dns": {"feature_ipam_coupling": True}})
def test_delete_zone_existing_ip(self):
addr = IPNetwork("10.0.0.30/24")
zone = self.zone
zone = self.zones[0]
name = "test-delete-zone-existing-ip"

# Create IP Address
Expand Down

0 comments on commit 1fae4bc

Please sign in to comment.