From a3b217ab04b31d3f12499b3433fa6b1e3f2e51eb Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Mon, 23 Oct 2023 10:30:27 -0700 Subject: [PATCH] Add list_zones support to enable dynamic config --- CHANGELOG.md | 5 ++++ octodns_cloudflare/__init__.py | 3 ++ tests/test_octodns_provider_cloudflare.py | 36 +++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55a0fa7..56d6678 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v0.0.4 - 2023-??-?? - Know your zones + +* Support for Provider.list_zones to enable dynamic zone config when operating + as a source + ## v0.0.3 - 2023-09-20 - All the commits fit to release * SPF records can no longer be created, diff --git a/octodns_cloudflare/__init__.py b/octodns_cloudflare/__init__.py index 7aa945e..6ff19ff 100644 --- a/octodns_cloudflare/__init__.py +++ b/octodns_cloudflare/__init__.py @@ -440,6 +440,9 @@ def _record_for(self, zone, name, _type, records, lenient): return record + def list_zones(self): + return sorted(self.zones.keys()) + def populate(self, zone, target=False, lenient=False): self.log.debug( 'populate: name=%s, target=%s, lenient=%s', diff --git a/tests/test_octodns_provider_cloudflare.py b/tests/test_octodns_provider_cloudflare.py index 70bcbd3..8f69b02 100644 --- a/tests/test_octodns_provider_cloudflare.py +++ b/tests/test_octodns_provider_cloudflare.py @@ -2108,3 +2108,39 @@ def test_account_id_filter(self): 'account.id': '334234243423aaabb334342aaa343433', }, ) + + def test_list_zones(self): + provider = CloudflareProvider( + 'test', + 'email', + 'token', + account_id='334234243423aaabb334342aaa343433', + ) + + # existing zone with data + with requests_mock() as mock: + base = 'https://api.cloudflare.com/client/v4/zones' + + # zones + with open('tests/fixtures/cloudflare-zones-page-1.json') as fh: + mock.get(f'{base}?page=1', status_code=200, text=fh.read()) + with open('tests/fixtures/cloudflare-zones-page-2.json') as fh: + mock.get(f'{base}?page=2', status_code=200, text=fh.read()) + with open('tests/fixtures/cloudflare-zones-page-3.json') as fh: + mock.get(f'{base}?page=3', status_code=200, text=fh.read()) + mock.get( + f'{base}?page=4', + status_code=200, + json={'result': [], 'result_info': {'count': 0, 'per_page': 0}}, + ) + + self.assertEqual( + [ + 'github.com.', + 'github.io.', + 'githubusercontent.com.', + 'unit.tests.', + 'xn--gthub-zsa.com.', + ], + provider.list_zones(), + )