Skip to content

Commit

Permalink
Merge pull request #72 from PITC-DNS/account-id-on-zone-creation
Browse files Browse the repository at this point in the history
Support creating zones in a specific account
  • Loading branch information
ross authored Dec 6, 2023
2 parents 699ac56 + 517f0f5 commit cf555c5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions octodns_cloudflare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ def _apply(self, plan):
if name not in self.zones:
self.log.debug('_apply: no matching zone, creating')
data = {'name': name[:-1], 'jump_start': False}
if self.account_id is not None:
data['account'] = {'id': self.account_id}
resp = self._try_request('POST', '/zones', data=data)
zone_id = resp['result']['id']
self.zones[name] = zone_id
Expand Down
92 changes: 92 additions & 0 deletions tests/test_octodns_provider_cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,98 @@ def test_apply(self):
]
)

# Run the basic apply tests but with an account_id
provider = CloudflareProvider(
'test',
'email',
'token',
account_id='334234243423aaabb334342aaa343433',
retry_period=0,
strict_supports=False,
)

provider._request = Mock()

provider._request.side_effect = [
self.empty, # no zones
{'result': {'id': 42}}, # zone create
] + [
None
] * 30 # individual record creates

# non-existent zone, create everything
plan = provider.plan(self.expected)
self.assertEqual(18, len(plan.changes))
self.assertEqual(18, provider.apply(plan))
self.assertFalse(plan.exists)

provider._request.assert_has_calls(
[
# created the domain
call(
'POST',
'/zones',
data={
'jump_start': False,
'name': 'unit.tests',
'account': {'id': '334234243423aaabb334342aaa343433'},
},
),
# created at least one of the record with expected data
call(
'POST',
'/zones/42/dns_records',
data={
'content': 'ns1.unit.tests.',
'type': 'NS',
'name': 'under.unit.tests',
'ttl': 3600,
},
),
# make sure semicolons are not escaped when sending data
call(
'POST',
'/zones/42/dns_records',
data={
'content': 'v=DKIM1;k=rsa;s=email;h=sha256;'
'p=A/kinda+of/long/string+with+numb3rs',
'type': 'TXT',
'name': 'txt.unit.tests',
'ttl': 600,
},
),
# create at least one pagerules
call(
'POST',
'/zones/42/pagerules',
data={
'targets': [
{
'target': 'url',
'constraint': {
'operator': 'matches',
'value': 'urlfwd.unit.tests/',
},
}
],
'actions': [
{
'id': 'forwarding_url',
'value': {
'url': 'http://www.unit.tests',
'status_code': 302,
},
}
],
'status': 'active',
},
),
],
True,
)
# expected number of total calls
self.assertEqual(32, provider._request.call_count)

def test_update_add_swap(self):
provider = CloudflareProvider('test', 'email', 'token', retry_period=0)

Expand Down

0 comments on commit cf555c5

Please sign in to comment.