From 08af9aaab35b351d20cde17cc059cc8761242e7c Mon Sep 17 00:00:00 2001 From: Adam Smith Date: Sat, 18 Jan 2020 20:24:15 -0800 Subject: [PATCH] ContellixProvider: zone creation and records in one run --- octodns/provider/constellix.py | 13 +++++++++--- tests/test_octodns_provider_constellix.py | 26 +++++++++++------------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/octodns/provider/constellix.py b/octodns/provider/constellix.py index 0600f80..5ca89e1 100644 --- a/octodns/provider/constellix.py +++ b/octodns/provider/constellix.py @@ -88,7 +88,7 @@ class ConstellixClient(object): if self._domains is None: zones = [] - resp = self._request('GET', '/').json() + resp = self._request('GET', '').json() zones += resp self._domains = {'{}.'.format(z['name']): z['id'] for z in zones} @@ -96,11 +96,16 @@ class ConstellixClient(object): return self._domains def domain(self, name): - path = '/{}'.format(self.domains.get(name)) + zone_id = self.domains.get(name, False) + if not zone_id: + raise ConstellixClientNotFound() + path = '/{}'.format(zone_id) return self._request('GET', path).json() def domain_create(self, name): - self._request('POST', '/', data={'names': [name]}) + resp = self._request('POST', '/', data={'names': [name]}) + # Add newly created zone to domain cache + self._domains['{}.'.format(name)] = resp.json()[0]['id'] def _absolutize_value(self, value, zone_name): if value == '': @@ -112,6 +117,8 @@ class ConstellixClient(object): def records(self, zone_name): zone_id = self.domains.get(zone_name, False) + if not zone_id: + raise ConstellixClientNotFound() path = '/{}/records'.format(zone_id) resp = self._request('GET', path).json() diff --git a/tests/test_octodns_provider_constellix.py b/tests/test_octodns_provider_constellix.py index 2c5cf26..151d0d4 100644 --- a/tests/test_octodns_provider_constellix.py +++ b/tests/test_octodns_provider_constellix.py @@ -14,13 +14,11 @@ from six import text_type from unittest import TestCase from octodns.record import Record -from octodns.provider.constellix import ConstellixClientNotFound, \ +from octodns.provider.constellix import \ ConstellixProvider from octodns.provider.yaml import YamlProvider from octodns.zone import Zone -import json - class TestConstellixProvider(TestCase): expected = Zone('unit.tests.', []) @@ -102,7 +100,7 @@ class TestConstellixProvider(TestCase): with requests_mock() as mock: base = 'https://api.dns.constellix.com/v1/domains' with open('tests/fixtures/constellix-domains.json') as fh: - mock.get('{}{}'.format(base, '/'), text=fh.read()) + mock.get('{}{}'.format(base, ''), text=fh.read()) with open('tests/fixtures/constellix-records.json') as fh: mock.get('{}{}'.format(base, '/123123/records'), text=fh.read()) @@ -128,15 +126,15 @@ class TestConstellixProvider(TestCase): resp.json = Mock() provider._client._request = Mock(return_value=resp) - with open('tests/fixtures/constellix-domains.json') as fh: - domains = json.load(fh) - # non-existent domain, create everything resp.json.side_effect = [ - ConstellixClientNotFound, # no zone in populate - ConstellixClientNotFound, # no domain during apply - domains + [], # no domains returned during populate + [{ + 'id': 123123, + 'name': 'unit.tests' + }], # domain created in apply ] + plan = provider.plan(self.expected) # No root NS, no ignored, no excluded, no unsupported @@ -145,10 +143,10 @@ class TestConstellixProvider(TestCase): self.assertEquals(n, provider.apply(plan)) provider._client._request.assert_has_calls([ - # created the domain - call('POST', '/', data={'names': ['unit.tests']}), # get all domains to build the cache - call('GET', '/'), + call('GET', ''), + # created the domain + call('POST', '/', data={'names': ['unit.tests']}) ]) # These two checks are broken up so that ordering doesn't break things. # Python3 doesn't make the calls in a consistent order so different @@ -171,7 +169,7 @@ class TestConstellixProvider(TestCase): }), ]) - self.assertEquals(20, provider._client._request.call_count) + self.assertEquals(18, provider._client._request.call_count) provider._client._request.reset_mock()