1
0
mirror of https://github.com/github/octodns.git synced 2024-05-11 05:55:00 +00:00

Add meta record support with provider id to zone

Support replace=True in zone.add_record
This commit is contained in:
Ross McFarland
2017-06-24 17:14:48 -07:00
parent 3ca64c4e08
commit 5e4d68094f
4 changed files with 31 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ import logging
from .provider.base import BaseProvider
from .provider.yaml import YamlProvider
from .record import Record
from .yaml import safe_load
from .zone import Zone
@@ -59,7 +60,7 @@ class MainThreadExecutor(object):
class Manager(object):
log = logging.getLogger('Manager')
def __init__(self, config_file, max_workers=None):
def __init__(self, config_file, max_workers=None, include_meta=False):
self.log.info('__init__: config_file=%s', config_file)
# Read our config file
@@ -75,6 +76,10 @@ class Manager(object):
else:
self._executor = MainThreadExecutor()
self.include_meta = include_meta or manager_config.get('include_meta',
False)
self.log.info('__init__: max_workers=%s', self.include_meta)
self.log.debug('__init__: configuring providers')
self.providers = {}
for provider_name, provider_config in self.config['providers'].items():
@@ -176,6 +181,13 @@ class Manager(object):
plans = []
for target in targets:
if self.include_meta:
meta = Record.new(zone, 'octodns-meta', {
'type': 'TXT',
'ttl': 60,
'value': 'provider={}'.format(target.id)
})
zone.add_record(meta, replace=True)
plan = target.plan(zone)
if plan:
plans.append((target, plan))

View File

@@ -49,9 +49,13 @@ class Zone(object):
def hostname_from_fqdn(self, fqdn):
return self._name_re.sub('', fqdn)
def add_record(self, record):
def add_record(self, record, replace=False):
name = record.name
last = name.split('.')[-1]
if replace and record in self.records:
self.records.remove(record)
if last in self.sub_zones:
if name != last:
# it's a record for something under a sub-zone
@@ -63,6 +67,7 @@ class Zone(object):
raise SubzoneRecordException('Record {} a managed sub-zone '
'and not of type NS'
.format(record.fqdn))
# TODO: this is pretty inefficent
for existing in self.records:
if record == existing:
raise DuplicateRecordException('Duplicate record {}, type {}'

View File

@@ -128,6 +128,12 @@ class TestManager(TestCase):
.sync(dry_run=False, force=True)
self.assertEquals(19, tc)
# Include meta
tc = Manager(get_config_filename('simple.yaml'), max_workers=1,
include_meta=True) \
.sync(dry_run=False, force=True)
self.assertEquals(23, tc)
def test_eligible_targets(self):
with TemporaryDirectory() as tmpdir:
environ['YAML_TMP_DIR'] = tmpdir.dirname

View File

@@ -39,6 +39,7 @@ class TestZone(TestCase):
a = ARecord(zone, 'a', {'ttl': 42, 'value': '1.1.1.1'})
b = ARecord(zone, 'b', {'ttl': 42, 'value': '1.1.1.1'})
c = ARecord(zone, 'a', {'ttl': 43, 'value': '2.2.2.2'})
zone.add_record(a)
self.assertEquals(zone.records, set([a]))
@@ -48,6 +49,11 @@ class TestZone(TestCase):
self.assertEquals('Duplicate record a.unit.tests., type A',
ctx.exception.message)
self.assertEquals(zone.records, set([a]))
# can add duplicate with replace=True
zone.add_record(c, replace=True)
self.assertEquals('2.2.2.2', list(zone.records)[0].values[0])
# Can add dup name, with different type
zone.add_record(b)
self.assertEquals(zone.records, set([a, b]))