From 9e172ed303ba82ba57d7f1dcb6a08066c30f40ea Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sun, 28 May 2017 07:21:53 -0700 Subject: [PATCH 1/7] Add AliasRecord & tests --- tests/test_octodns_record.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/test_octodns_record.py b/tests/test_octodns_record.py index 491b278..f28e39d 100644 --- a/tests/test_octodns_record.py +++ b/tests/test_octodns_record.py @@ -7,9 +7,9 @@ from __future__ import absolute_import, division, print_function, \ from unittest import TestCase -from octodns.record import ARecord, AaaaRecord, CnameRecord, Create, Delete, \ - GeoValue, MxRecord, NaptrRecord, NaptrValue, NsRecord, PtrRecord, Record, \ - SshfpRecord, SpfRecord, SrvRecord, TxtRecord, Update +from octodns.record import ARecord, AaaaRecord, AliasRecord, CnameRecord, \ + Create, Delete, GeoValue, MxRecord, NaptrRecord, NaptrValue, NsRecord, \ + PtrRecord, Record, SshfpRecord, SpfRecord, SrvRecord, TxtRecord, Update from octodns.zone import Zone from helpers import GeoProvider, SimpleProvider @@ -242,6 +242,17 @@ class TestRecord(TestCase): # __repr__ doesn't blow up a.__repr__() + def test_alias(self): + self.assertSingleValue(AliasRecord, 'foo.unit.tests.', + 'other.unit.tests.') + + with self.assertRaises(Exception) as ctx: + AliasRecord(self.zone, '', { + 'ttl': 31, + 'value': 'foo.bar.com.' + }) + self.assertTrue('in same zone' in ctx.exception.message) + def test_cname(self): self.assertSingleValue(CnameRecord, 'target.foo.com.', 'other.foo.com.') From f2b3e9e3f47aadbda01bb188adb42bac564d4ba3 Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sun, 28 May 2017 07:26:47 -0700 Subject: [PATCH 2/7] Add missing class --- octodns/record.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/octodns/record.py b/octodns/record.py index 570988b..c5404f3 100644 --- a/octodns/record.py +++ b/octodns/record.py @@ -311,6 +311,16 @@ class _ValueMixin(object): self.fqdn, self.value) +class AliasRecord(_ValueMixin, Record): + _type = 'ALIAS' + + def _process_value(self, value): + if not value.endswith(self.zone.name): + raise Exception('Invalid record {}, value ({}) must be in ' + 'same zone.'.format(self.fqdn, value)) + return value.lower() + + class CnameRecord(_ValueMixin, Record): _type = 'CNAME' From 9dbfe7c839e3a754a9c04ee1400e14dcb5112afc Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sun, 28 May 2017 17:05:23 -0700 Subject: [PATCH 3/7] AliasValue, name & type, improved Record KeyError handling --- octodns/record.py | 62 ++++++++++++++++++++++++++------- tests/test_octodns_record.py | 67 ++++++++++++++++++++++++++++++++---- 2 files changed, 110 insertions(+), 19 deletions(-) diff --git a/octodns/record.py b/octodns/record.py index c5404f3..8394782 100644 --- a/octodns/record.py +++ b/octodns/record.py @@ -71,7 +71,7 @@ class Record(object): _type = { 'A': ARecord, 'AAAA': AaaaRecord, - # alias + 'ALIAS': AliasRecord, # cert 'CNAME': CnameRecord, # dhcid @@ -185,13 +185,14 @@ class _ValuesMixin(object): def __init__(self, zone, name, data, source=None): super(_ValuesMixin, self).__init__(zone, name, data, source=source) try: - self.values = sorted(self._process_values(data['values'])) + values = data['values'] except KeyError: try: - self.values = self._process_values([data['value']]) + values = [data['value']] except KeyError: raise Exception('Invalid record {}, missing value(s)' .format(self.fqdn)) + self.values = sorted(self._process_values(values)) def changes(self, other, target): if self.values != other.values: @@ -290,10 +291,11 @@ class _ValueMixin(object): def __init__(self, zone, name, data, source=None): super(_ValueMixin, self).__init__(zone, name, data, source=source) try: - self.value = self._process_value(data['value']) + value = data['value'] except KeyError: raise Exception('Invalid record {}, missing value' .format(self.fqdn)) + self.value = self._process_value(value) def changes(self, other, target): if self.value != other.value: @@ -311,14 +313,50 @@ class _ValueMixin(object): self.fqdn, self.value) -class AliasRecord(_ValueMixin, Record): +class AliasValue(object): + + def __init__(self, value): + self.name = value['name'].lower() + self._type = value['type'] + + @property + def data(self): + return { + 'name': self.name, + 'type': self._type, + } + + def __cmp__(self, other): + if self.name == other.name: + return cmp(self._type, other._type) + return cmp(self.name, other.name) + + def __repr__(self): + return "'{} {}'".format(self.name, self._type) + + +class AliasRecord(_ValuesMixin, Record): _type = 'ALIAS' - def _process_value(self, value): - if not value.endswith(self.zone.name): - raise Exception('Invalid record {}, value ({}) must be in ' - 'same zone.'.format(self.fqdn, value)) - return value.lower() + def __init__(self, zone, name, data, source=None): + data = dict(data) + # TODO: this is an ugly way to fake the lack of ttl :-( + data['ttl'] = 0 + super(AliasRecord, self).__init__(zone, name, data, source) + + def _process_values(self, values): + ret = [] + for value in values: + try: + value = AliasValue(value) + except KeyError as e: + raise Exception('Invalid value in record {}, missing {}' + .format(self.fqdn, e.args[0])) + if not value.name.endswith(self.zone.name): + raise Exception('Invalid value in record {}, name must be in ' + 'same zone.'.format(self.fqdn)) + ret.append(value) + return ret class CnameRecord(_ValueMixin, Record): @@ -326,7 +364,7 @@ class CnameRecord(_ValueMixin, Record): def _process_value(self, value): if not value.endswith('.'): - raise Exception('Invalid record {}, value {} missing trailing .' + raise Exception('Invalid record {}, value ({}) missing trailing .' .format(self.fqdn, value)) return value.lower() @@ -444,7 +482,7 @@ class PtrRecord(_ValueMixin, Record): def _process_value(self, value): if not value.endswith('.'): - raise Exception('Invalid record {}, value {} missing trailing .' + raise Exception('Invalid record {}, value ({}) missing trailing .' .format(self.fqdn, value)) return value.lower() diff --git a/tests/test_octodns_record.py b/tests/test_octodns_record.py index f28e39d..80d4253 100644 --- a/tests/test_octodns_record.py +++ b/tests/test_octodns_record.py @@ -243,15 +243,68 @@ class TestRecord(TestCase): a.__repr__() def test_alias(self): - self.assertSingleValue(AliasRecord, 'foo.unit.tests.', - 'other.unit.tests.') + a_values = [{ + 'name': 'www.unit.tests.', + 'type': 'A' + }, { + 'name': 'www.unit.tests.', + 'type': 'AAAA' + }] + a_data = {'ttl': 0, 'values': a_values} + a = AliasRecord(self.zone, '', a_data) + self.assertEquals('', a.name) + self.assertEquals('unit.tests.', a.fqdn) + self.assertEquals(0, a.ttl) + self.assertEquals(a_values[0]['name'], a.values[0].name) + self.assertEquals(a_values[0]['type'], a.values[0]._type) + self.assertEquals(a_values[1]['name'], a.values[1].name) + self.assertEquals(a_values[1]['type'], a.values[1]._type) + self.assertEquals(a_data, a.data) + + b_value = { + 'name': 'www.unit.tests.', + 'type': 'A', + } + b_data = {'ttl': 0, 'value': b_value} + b = AliasRecord(self.zone, 'b', b_data) + self.assertEquals(b_value['name'], b.values[0].name) + self.assertEquals(b_value['type'], b.values[0]._type) + self.assertEquals(b_data, b.data) + # missing value with self.assertRaises(Exception) as ctx: - AliasRecord(self.zone, '', { - 'ttl': 31, - 'value': 'foo.bar.com.' - }) - self.assertTrue('in same zone' in ctx.exception.message) + AliasRecord(self.zone, None, {'ttl': 0}) + self.assertTrue('missing value(s)' in ctx.exception.message) + # invalid value + with self.assertRaises(Exception) as ctx: + AliasRecord(self.zone, None, {'ttl': 0, 'value': {}}) + self.assertTrue('Invalid value' in ctx.exception.message) + # bad name + with self.assertRaises(Exception) as ctx: + AliasRecord(self.zone, None, {'ttl': 0, 'value': { + 'name': 'foo.bar.com.', + 'type': 'A' + }}) + self.assertTrue('Invalid value' in ctx.exception.message) + + target = SimpleProvider() + # No changes with self + self.assertFalse(a.changes(a, target)) + # Diff in priority causes change + other = AliasRecord(self.zone, 'a', {'ttl': 30, 'values': a_values}) + other.values[0].name = 'foo.unit.tests.' + change = a.changes(other, target) + self.assertEqual(change.existing, a) + self.assertEqual(change.new, other) + # Diff in value causes change + other.values[0].name = a.values[0].name + other.values[0]._type = 'MX' + change = a.changes(other, target) + self.assertEqual(change.existing, a) + self.assertEqual(change.new, other) + + # __repr__ doesn't blow up + a.__repr__() def test_cname(self): self.assertSingleValue(CnameRecord, 'target.foo.com.', From 756f017854648da12d27a80f3e2dcd028c88985e Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sat, 3 Jun 2017 08:47:01 -0700 Subject: [PATCH 4/7] Go back to simple/standard ALIAS value --- octodns/record.py | 42 +++++--------------------------- tests/test_octodns_record.py | 47 ++++++------------------------------ 2 files changed, 13 insertions(+), 76 deletions(-) diff --git a/octodns/record.py b/octodns/record.py index 8394782..34e4bb9 100644 --- a/octodns/record.py +++ b/octodns/record.py @@ -313,29 +313,7 @@ class _ValueMixin(object): self.fqdn, self.value) -class AliasValue(object): - - def __init__(self, value): - self.name = value['name'].lower() - self._type = value['type'] - - @property - def data(self): - return { - 'name': self.name, - 'type': self._type, - } - - def __cmp__(self, other): - if self.name == other.name: - return cmp(self._type, other._type) - return cmp(self.name, other.name) - - def __repr__(self): - return "'{} {}'".format(self.name, self._type) - - -class AliasRecord(_ValuesMixin, Record): +class AliasRecord(_ValueMixin, Record): _type = 'ALIAS' def __init__(self, zone, name, data, source=None): @@ -344,19 +322,11 @@ class AliasRecord(_ValuesMixin, Record): data['ttl'] = 0 super(AliasRecord, self).__init__(zone, name, data, source) - def _process_values(self, values): - ret = [] - for value in values: - try: - value = AliasValue(value) - except KeyError as e: - raise Exception('Invalid value in record {}, missing {}' - .format(self.fqdn, e.args[0])) - if not value.name.endswith(self.zone.name): - raise Exception('Invalid value in record {}, name must be in ' - 'same zone.'.format(self.fqdn)) - ret.append(value) - return ret + def _process_value(self, value): + if not value.endswith('.'): + raise Exception('Invalid record {}, value ({}) missing trailing .' + .format(self.fqdn, value)) + return value class CnameRecord(_ValueMixin, Record): diff --git a/tests/test_octodns_record.py b/tests/test_octodns_record.py index 80d4253..52505cb 100644 --- a/tests/test_octodns_record.py +++ b/tests/test_octodns_record.py @@ -243,62 +243,29 @@ class TestRecord(TestCase): a.__repr__() def test_alias(self): - a_values = [{ - 'name': 'www.unit.tests.', - 'type': 'A' - }, { - 'name': 'www.unit.tests.', - 'type': 'AAAA' - }] - a_data = {'ttl': 0, 'values': a_values} + a_data = {'ttl': 0, 'value': 'www.unit.tests.'} a = AliasRecord(self.zone, '', a_data) self.assertEquals('', a.name) self.assertEquals('unit.tests.', a.fqdn) self.assertEquals(0, a.ttl) - self.assertEquals(a_values[0]['name'], a.values[0].name) - self.assertEquals(a_values[0]['type'], a.values[0]._type) - self.assertEquals(a_values[1]['name'], a.values[1].name) - self.assertEquals(a_values[1]['type'], a.values[1]._type) + self.assertEquals(a_data['value'], a.value) self.assertEquals(a_data, a.data) - b_value = { - 'name': 'www.unit.tests.', - 'type': 'A', - } - b_data = {'ttl': 0, 'value': b_value} - b = AliasRecord(self.zone, 'b', b_data) - self.assertEquals(b_value['name'], b.values[0].name) - self.assertEquals(b_value['type'], b.values[0]._type) - self.assertEquals(b_data, b.data) - # missing value with self.assertRaises(Exception) as ctx: AliasRecord(self.zone, None, {'ttl': 0}) - self.assertTrue('missing value(s)' in ctx.exception.message) - # invalid value - with self.assertRaises(Exception) as ctx: - AliasRecord(self.zone, None, {'ttl': 0, 'value': {}}) - self.assertTrue('Invalid value' in ctx.exception.message) + self.assertTrue('missing value' in ctx.exception.message) # bad name with self.assertRaises(Exception) as ctx: - AliasRecord(self.zone, None, {'ttl': 0, 'value': { - 'name': 'foo.bar.com.', - 'type': 'A' - }}) - self.assertTrue('Invalid value' in ctx.exception.message) + AliasRecord(self.zone, None, {'ttl': 0, 'value': 'www.unit.tests'}) + self.assertTrue('missing trailing .' in ctx.exception.message) target = SimpleProvider() # No changes with self self.assertFalse(a.changes(a, target)) - # Diff in priority causes change - other = AliasRecord(self.zone, 'a', {'ttl': 30, 'values': a_values}) - other.values[0].name = 'foo.unit.tests.' - change = a.changes(other, target) - self.assertEqual(change.existing, a) - self.assertEqual(change.new, other) # Diff in value causes change - other.values[0].name = a.values[0].name - other.values[0]._type = 'MX' + other = AliasRecord(self.zone, 'a', a_data) + other.value = 'foo.unit.tests.' change = a.changes(other, target) self.assertEqual(change.existing, a) self.assertEqual(change.new, other) From 11cf1554778e47f3c2784c7889389795075f8b45 Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sat, 3 Jun 2017 09:44:05 -0700 Subject: [PATCH 5/7] Pass of ALIAS support across supported providers. Allow ALIAS ttl Supports ALIAS for Dnsimple, Dyn, Ns1, and PowerDNS. Notes added to readme about some of the quirks found while working with them. TTL seems to mostly be accepted on ALIAS records so it has been added back, what it means seems to vary across providers, thus notes. --- README.md | 6 ++++++ octodns/provider/dnsimple.py | 7 +++++++ octodns/provider/dyn.py | 34 +++++++++++++++++++++------------- octodns/provider/ns1.py | 2 ++ octodns/provider/powerdns.py | 2 ++ octodns/record.py | 6 ------ 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a58c290..52370c5 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,12 @@ The above command pulled the existing data out of Route53 and placed the results | [TinyDNSSource](/octodns/source/tinydns.py) | A, CNAME, MX, NS, PTR | No | read-only | | [YamlProvider](/octodns/provider/yaml.py) | All | Yes | config | +#### Notes + +* ALIAS support varies a lot fromm provider to provider care should be taken to verify that your needs are met in detail. + * Dyn's UI doesn't allow editing or view of TTL, but the API accepts and stores the value provided, this value does not appear to be used when served + * Dnsimple's API throws errors when TTL is modified, but it can be edited in the UI and seems to be used when served, there's also a secondary TXT record created alongside the ALIAS that octoDNS ignores + ## Custom Sources and Providers You can check out the [source](/octodns/source/) and [provider](/octodns/provider/) directory to see what's currently supported. Sources act as a source of record information. TinyDnsProvider is currently the only OSS source, though we have several others internally that are specific to our environment. These include something to pull host data from [gPanel](https://githubengineering.com/githubs-metal-cloud/) and a similar provider that sources information about our network gear to create both `A` & `PTR` records for their interfaces. Things that might make good OSS sources might include an `ElbSource` that pulls information about [AWS Elastic Load Balancers](https://aws.amazon.com/elasticloadbalancing/) and dynamically creates `CNAME`s for them, or `Ec2Source` that pulls instance information so that records can be created for hosts similar to how our `GPanelProvider` works. An `AxfrSource` could be really interesting as well. Another case where a source may make sense is if you'd like to export data from a legacy service that you have no plans to push changes back into. diff --git a/octodns/provider/dnsimple.py b/octodns/provider/dnsimple.py index 7ed4fe7..dd16c6d 100644 --- a/octodns/provider/dnsimple.py +++ b/octodns/provider/dnsimple.py @@ -120,6 +120,8 @@ class DnsimpleProvider(BaseProvider): 'value': '{}.'.format(record['content']) } + _data_for_ALIAS = _data_for_CNAME + def _data_for_MX(self, _type, records): values = [] for record in records: @@ -238,6 +240,10 @@ class DnsimpleProvider(BaseProvider): _type = record['type'] if _type == 'SOA': continue + elif _type == 'TXT' and record['content'].startswith('ALIAS for'): + # ALIAS has a "ride along" TXT record with 'ALIAS for XXXX', + # we're ignoring it + continue values[record['name']][record['type']].append(record) before = len(zone.records) @@ -273,6 +279,7 @@ class DnsimpleProvider(BaseProvider): 'type': record._type } + _params_for_ALIAS = _params_for_single _params_for_CNAME = _params_for_single _params_for_PTR = _params_for_single diff --git a/octodns/provider/dyn.py b/octodns/provider/dyn.py index 98414a4..ac0e21b 100644 --- a/octodns/provider/dyn.py +++ b/octodns/provider/dyn.py @@ -109,6 +109,7 @@ class DynProvider(BaseProvider): RECORDS_TO_TYPE = { 'a_records': 'A', 'aaaa_records': 'AAAA', + 'alias_records': 'ALIAS', 'cname_records': 'CNAME', 'mx_records': 'MX', 'naptr_records': 'NAPTR', @@ -119,19 +120,7 @@ class DynProvider(BaseProvider): 'srv_records': 'SRV', 'txt_records': 'TXT', } - TYPE_TO_RECORDS = { - 'A': 'a_records', - 'AAAA': 'aaaa_records', - 'CNAME': 'cname_records', - 'MX': 'mx_records', - 'NAPTR': 'naptr_records', - 'NS': 'ns_records', - 'PTR': 'ptr_records', - 'SSHFP': 'sshfp_records', - 'SPF': 'spf_records', - 'SRV': 'srv_records', - 'TXT': 'txt_records', - } + TYPE_TO_RECORDS = {v: k for k, v in RECORDS_TO_TYPE.items()} # https://help.dyn.com/predefined-geotm-regions-groups/ REGION_CODES = { @@ -194,6 +183,15 @@ class DynProvider(BaseProvider): _data_for_AAAA = _data_for_A + def _data_for_ALIAS(self, _type, records): + # See note on ttl in _kwargs_for_ALIAS + record = records[0] + return { + 'type': _type, + 'ttl': record.ttl, + 'value': record.alias + } + def _data_for_CNAME(self, _type, records): record = records[0] return { @@ -385,6 +383,16 @@ class DynProvider(BaseProvider): 'ttl': record.ttl, }] + def _kwargs_for_ALIAS(self, record): + # NOTE: Dyn's UI doesn't allow editing of ALIAS ttl, but the API seems + # to accept and store the values we send it just fine. No clue if they + # do anything with them. I'd assume they just obey the TTL of the + # record that we're pointed at which makes sense. + return [{ + 'alias': record.value, + 'ttl': record.ttl, + }] + def _kwargs_for_MX(self, record): return [{ 'preference': v.priority, diff --git a/octodns/provider/ns1.py b/octodns/provider/ns1.py index 69e8da7..93f5d0c 100644 --- a/octodns/provider/ns1.py +++ b/octodns/provider/ns1.py @@ -48,6 +48,7 @@ class Ns1Provider(BaseProvider): 'value': record['short_answers'][0], } + _data_for_ALIAS = _data_for_CNAME _data_for_PTR = _data_for_CNAME def _data_for_MX(self, _type, record): @@ -140,6 +141,7 @@ class Ns1Provider(BaseProvider): def _params_for_CNAME(self, record): return {'answers': [record.value], 'ttl': record.ttl} + _params_for_ALIAS = _params_for_CNAME _params_for_PTR = _params_for_CNAME def _params_for_MX(self, record): diff --git a/octodns/provider/powerdns.py b/octodns/provider/powerdns.py index 0f9190b..4ff2568 100644 --- a/octodns/provider/powerdns.py +++ b/octodns/provider/powerdns.py @@ -64,6 +64,7 @@ class PowerDnsBaseProvider(BaseProvider): 'ttl': rrset['ttl'] } + _data_for_ALIAS = _data_for_single _data_for_CNAME = _data_for_single _data_for_PTR = _data_for_single @@ -191,6 +192,7 @@ class PowerDnsBaseProvider(BaseProvider): def _records_for_single(self, record): return [{'content': record.value, 'disabled': False}] + _records_for_ALIAS = _records_for_single _records_for_CNAME = _records_for_single _records_for_PTR = _records_for_single diff --git a/octodns/record.py b/octodns/record.py index 34e4bb9..f3abd3f 100644 --- a/octodns/record.py +++ b/octodns/record.py @@ -316,12 +316,6 @@ class _ValueMixin(object): class AliasRecord(_ValueMixin, Record): _type = 'ALIAS' - def __init__(self, zone, name, data, source=None): - data = dict(data) - # TODO: this is an ugly way to fake the lack of ttl :-( - data['ttl'] = 0 - super(AliasRecord, self).__init__(zone, name, data, source) - def _process_value(self, value): if not value.endswith('.'): raise Exception('Invalid record {}, value ({}) missing trailing .' From 8ed727803277ef283f7e95ebb5db522450cf5f7c Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Sat, 3 Jun 2017 17:21:08 -0700 Subject: [PATCH 6/7] DynProvider and DnsimpleProvider ALIAS tests --- tests/fixtures/dnsimple-page-1.json | 2 +- tests/fixtures/dnsimple-page-2.json | 18 ++++- tests/test_octodns_provider_dyn.py | 106 ++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/tests/fixtures/dnsimple-page-1.json b/tests/fixtures/dnsimple-page-1.json index 3fa3257..fca2111 100644 --- a/tests/fixtures/dnsimple-page-1.json +++ b/tests/fixtures/dnsimple-page-1.json @@ -308,7 +308,7 @@ "pagination": { "current_page": 1, "per_page": 20, - "total_entries": 28, + "total_entries": 29, "total_pages": 2 } } diff --git a/tests/fixtures/dnsimple-page-2.json b/tests/fixtures/dnsimple-page-2.json index dbc5cd3..f50704b 100644 --- a/tests/fixtures/dnsimple-page-2.json +++ b/tests/fixtures/dnsimple-page-2.json @@ -143,12 +143,28 @@ "system_record": false, "created_at": "2017-03-09T15:55:09Z", "updated_at": "2017-03-09T15:55:09Z" + }, + { + "id": 11188802, + "zone_id": "unit.tests", + "parent_id": null, + "name": "txt", + "content": "ALIAS for www.unit.tests.", + "ttl": 600, + "priority": null, + "type": "TXT", + "regions": [ + "global" + ], + "system_record": false, + "created_at": "2017-03-09T15:55:09Z", + "updated_at": "2017-03-09T15:55:09Z" } ], "pagination": { "current_page": 2, "per_page": 20, - "total_entries": 28, + "total_entries": 29, "total_pages": 2 } } diff --git a/tests/test_octodns_provider_dyn.py b/tests/test_octodns_provider_dyn.py index 41c8b2e..307e640 100644 --- a/tests/test_octodns_provider_dyn.py +++ b/tests/test_octodns_provider_dyn.py @@ -1154,3 +1154,109 @@ class TestDynProviderGeo(TestCase): # old ruleset ruleset should be deleted, it's pool will have been # reused ruleset_mock.delete.assert_called_once() + + +class TestDynProviderAlias(TestCase): + expected = Zone('unit.tests.', []) + for name, data in ( + ('', { + 'type': 'ALIAS', + 'ttl': 300, + 'value': 'www.unit.tests.' + }), + ('www', { + 'type': 'A', + 'ttl': 300, + 'values': ['1.2.3.4'] + })): + expected.add_record(Record.new(expected, name, data)) + + def setUp(self): + # Flush our zone to ensure we start fresh + _CachingDynZone.flush_zone(self.expected.name[:-1]) + + @patch('dyn.core.SessionEngine.execute') + def test_populate(self, execute_mock): + provider = DynProvider('test', 'cust', 'user', 'pass') + + # Test Zone create + execute_mock.side_effect = [ + # get Zone + {'data': {}}, + # get_all_records + {'data': { + 'a_records': [{ + 'fqdn': 'www.unit.tests', + 'rdata': {'address': '1.2.3.4'}, + 'record_id': 1, + 'record_type': 'A', + 'ttl': 300, + 'zone': 'unit.tests', + }], + 'alias_records': [{ + 'fqdn': 'unit.tests', + 'rdata': {'alias': 'www.unit.tests.'}, + 'record_id': 2, + 'record_type': 'ALIAS', + 'ttl': 300, + 'zone': 'unit.tests', + }], + }} + ] + got = Zone('unit.tests.', []) + provider.populate(got) + execute_mock.assert_has_calls([ + call('/Zone/unit.tests/', 'GET', {}), + call('/AllRecord/unit.tests/unit.tests./', 'GET', {'detail': 'Y'}) + ]) + changes = self.expected.changes(got, SimpleProvider()) + self.assertEquals([], changes) + + @patch('dyn.core.SessionEngine.execute') + def test_sync(self, execute_mock): + provider = DynProvider('test', 'cust', 'user', 'pass') + + # Test Zone create + execute_mock.side_effect = [ + # No such zone, during populate + DynectGetError('foo'), + # No such zone, during sync + DynectGetError('foo'), + # get empty Zone + {'data': {}}, + # get zone we can modify & delete with + {'data': { + # A top-level to delete + 'a_records': [{ + 'fqdn': 'www.unit.tests', + 'rdata': {'address': '1.2.3.4'}, + 'record_id': 1, + 'record_type': 'A', + 'ttl': 300, + 'zone': 'unit.tests', + }], + # A node to delete + 'alias_records': [{ + 'fqdn': 'unit.tests', + 'rdata': {'alias': 'www.unit.tests.'}, + 'record_id': 2, + 'record_type': 'ALIAS', + 'ttl': 300, + 'zone': 'unit.tests', + }], + }} + ] + + # No existing records, create all + with patch('dyn.tm.zones.Zone.add_record') as add_mock: + with patch('dyn.tm.zones.Zone._update') as update_mock: + plan = provider.plan(self.expected) + update_mock.assert_not_called() + provider.apply(plan) + update_mock.assert_called() + add_mock.assert_called() + # Once for each dyn record + self.assertEquals(2, len(add_mock.call_args_list)) + execute_mock.assert_has_calls([call('/Zone/unit.tests/', 'GET', {}), + call('/Zone/unit.tests/', 'GET', {})]) + self.assertEquals(2, len(plan.changes)) From e87462380f13f96ca944ea9fe0de474c6480081a Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Mon, 12 Jun 2017 14:06:43 -0700 Subject: [PATCH 7/7] Update comment about DNSimple's ALIAS support, no errors are thrown --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 52370c5..50a6933 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ The above command pulled the existing data out of Route53 and placed the results * ALIAS support varies a lot fromm provider to provider care should be taken to verify that your needs are met in detail. * Dyn's UI doesn't allow editing or view of TTL, but the API accepts and stores the value provided, this value does not appear to be used when served - * Dnsimple's API throws errors when TTL is modified, but it can be edited in the UI and seems to be used when served, there's also a secondary TXT record created alongside the ALIAS that octoDNS ignores + * Dnsimple's uses the configured TTL when serving things through the ALIAS, there's also a secondary TXT record created alongside the ALIAS that octoDNS ignores ## Custom Sources and Providers