Skip to content

Commit

Permalink
Merge pull request #120 from octodns/zone-id-fallback
Browse files Browse the repository at this point in the history
zone_id lookup fallback for deletes
  • Loading branch information
ross authored Feb 2, 2025
2 parents b27c0a6 + 5d68476 commit 468bf9a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v0.0.8 - 20??-??-?? - ???

* Add support for optionally retrying requests that hit 403 errors
* Add a zone_id lookup fallback when deleting records

## v0.0.7 - 2024-08-20 - DS always come second

Expand Down
12 changes: 10 additions & 2 deletions octodns_cloudflare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,14 +1176,14 @@ def _apply_Delete(self, change):
existing_name = existing.fqdn[:-1]
# Make sure to map ALIAS to CNAME when looking for the target to delete
existing_type = 'CNAME' if existing._type == 'ALIAS' else existing._type
zone_id = self.zones[existing.zone.name]
for record in self.zone_records(existing.zone):
if 'targets' in record and self.pagerules:
uri = record['targets'][0]['constraint']['value']
uri = '//' + uri if not uri.startswith('http') else uri
parsed_uri = urlsplit(uri)
record_name = parsed_uri.netloc
record_type = 'URLFWD'
zone_id = self.zones.get(existing.zone.name, False)
if (
existing_name == record_name
and existing_type == record_type
Expand All @@ -1195,8 +1195,16 @@ def _apply_Delete(self, change):
existing_name == record['name']
and existing_type == record['type']
):
record_zone_id = record.get('zone_id')
if record_zone_id is None:
self.log.warning(
'_apply_Delete: record "%s", %s is missing "zone_id", falling back to lookup',
record['name'],
record['type'],
)
record_zone_id = zone_id
path = (
f'/zones/{record["zone_id"]}/dns_records/'
f'/zones/{record_zone_id}/dns_records/'
f'{record["id"]}'
)
self._try_request('DELETE', path)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_octodns_provider_cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ def test_apply(self):
"proxied": False,
"ttl": 300,
"locked": False,
"zone_id": "ff12ab34cd5611334422ab3322997650",
"zone_name": "unit.tests",
"modified_on": "2017-03-11T18:01:43.420689Z",
"created_on": "2017-03-11T18:01:43.420689Z",
Expand Down Expand Up @@ -533,10 +532,10 @@ def test_apply(self):
'DELETE',
'/zones/42/pagerules/2a9141b18ffb0e6aed826050eec970b8',
),
# this one used the zone_id lookup fallback, thus 42
call(
'DELETE',
'/zones/ff12ab34cd5611334422ab3322997650/'
'dns_records/fc12ab34cd5611334422ab3322997653',
'/zones/42/dns_records/fc12ab34cd5611334422ab3322997653',
),
call(
'DELETE',
Expand Down

0 comments on commit 468bf9a

Please sign in to comment.