octodns / octodns-cloudflare

Cloudflare DNS provider for octoDNS
MIT License
24 stars 18 forks source link

Error re-adding recently removed domains #113

Open cbrnit opened 1 month ago

cbrnit commented 1 month ago

octodns 1.10 octodns-cloudflare 0.0.7 On Windows Server 2022 with Python 3.12.4

When I try to sync a domain to Cloudflare that was previously in Cloudflare and was recently removed I get an error:

[4540] INFO  CloudflareProvider[cloudflare] apply: making 2 changes to exampledomain.com.
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\Applications\Executables\OctoDNS\env\Scripts\octodns-sync.exe\__main__.py", line 7, in <module>
  File "D:\Applications\Executables\OctoDNS\env\Lib\site-packages\octodns\cmds\sync.py", line 62, in main
    manager.sync(
  File "D:\Applications\Executables\OctoDNS\env\Lib\site-packages\octodns\manager.py", line 856, in sync
    total_changes += target.apply(plan)
                     ^^^^^^^^^^^^^^^^^^
  File "D:\Applications\Executables\OctoDNS\env\Lib\site-packages\octodns\provider\base.py", line 298, in apply
    self._apply(plan)
  File "D:\Applications\Executables\OctoDNS\env\Lib\site-packages\octodns_cloudflare\__init__.py", line 1107, in _apply
    getattr(self, f'_apply_{class_name}')(change)
  File "D:\Applications\Executables\OctoDNS\env\Lib\site-packages\octodns_cloudflare\__init__.py", line 920, in _apply_Create
    self._try_request('POST', path, data=content)
  File "D:\Applications\Executables\OctoDNS\env\Lib\site-packages\octodns_cloudflare\__init__.py", line 131, in _try_request
    return self._request(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Applications\Executables\OctoDNS\env\Lib\site-packages\octodns_cloudflare\__init__.py", line 154, in _request
    raise CloudflareError(resp.json())
octodns_cloudflare.CloudflareError: A record with the same settings already exists.

It seems that when a recently removed domain is re-added to Cloudflare it is put back with its records intact. I confirmed that by manually re-adding a domain in the Cloudflare Dash to find previously entered DNS records. When OctoDNS tries to sync a recently removed zone it errors because the recreated zone already contains records that OctoDNS is trying to add.

ross commented 1 month ago

It seems that when a recently removed domain is re-added to Cloudflare it is put back with its records intact.

Weird. I'm not sure how the provider could handle that situation since it would have no clue what state the previous zone was in or whether there even was a previous state or not.

It would require another plan cycle after (re)creating the zone to see what actually needs to happen which would violate the whole idea of planning and applying being separate steps.

Only thing I can think of that would work cleanly would be to list the zone's records immediately after creating it and then go through and delete them all before doing the apply. That would get to the right end state and conceptually follow the plan, but it's pretty ugly and could involve A LOT of churn on a large zone.

Short of that it could check to see if the zone has any records after the create and throw an error if it does saying what's up and that another plan and apply should be done.

None of the options seems great. Would be nice if CF supported a create option that created things empty.