Commit 34457d9b authored by ale's avatar ale

merge entries properly

parent d569ce35
......@@ -93,6 +93,12 @@ $TTL %(ttl)d
return ''.join(out)
def _mklist(v):
if v is None:
return []
return v if isinstance(v, list) else [v]
class ZoneParser(object):
def __init__(self, config=None):
......@@ -104,9 +110,22 @@ class ZoneParser(object):
for y in objs:
self._merge(y)
def _mergedict(self, a, b):
# 'a' is modified.
for key, value in b.iteritems():
a[key] = self._mergevalues(a.get(key, []), value)
def _merge(self, data):
for zone_name, zone_data in data.iteritems():
self.zones.setdefault(zone_name, {}).update(zone_data)
self._mergedict(
self.zones.setdefault(zone_name, {}),
zone_data)
def _mergevalues(self, va, vb):
result = _mklist(va) + _mklist(vb)
if len(result) == 1:
return result[0]
return result
def render(self):
for zone_name, zone_data in self.zones.iteritems():
......@@ -119,7 +138,7 @@ class ZoneParser(object):
def _resolve_references(self, zone_name, zone_data):
out = {}
bases = ['@default'] if not zone_name.startswith('@') else []
bases.extend(zone_data.get('EXTENDS', []))
bases.extend(_mklist(zone_data.get('EXTENDS', [])))
for base in bases:
try:
......
......@@ -28,7 +28,25 @@ autistici.org:
onion:
- TXT "blahblah.onion."
''',
]
]
TEST_DATA_2 = [
'''
autistici.org:
www:
- 82.94.249.234
- 82.221.99.153
''', '''
autistici.org:
www:
- TXT "web"
''', '''
autistici.org:
www:
- 2a02:f48:2000:201::19
- 2002:b2ff:9023::1
''',
]
TEST_CONFIG = {
'FRONTENDS': ['82.94.249.234', '82.221.99.153'],
......@@ -52,6 +70,19 @@ class ZoneParserTest(unittest.TestCase):
self.assertTrue(result)
self.assertEquals('autistici.org', result[0][0])
def test_merge(self):
self.zp.load(_loadyaml(TEST_DATA_2))
result = list(self.zp.render())
self.assertTrue(result)
self.assertEquals('autistici.org', result[0][0])
# Verify that all 'www' entries have been merged properly.
www = result[0][1]['www']
expected = ['82.94.249.234', '82.221.99.153', 'TXT "web"',
'2a02:f48:2000:201::19', '2002:b2ff:9023::1']
self.assertEquals(expected, www)
class ZoneWriterTestBase(unittest.TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment