Browse Source

Add owns method to Zone

pull/1020/head
Ross McFarland 2 years ago
parent
commit
49233e2da6
No known key found for this signature in database GPG Key ID: 943B179E15D3B22A
2 changed files with 41 additions and 0 deletions
  1. +22
    -0
      octodns/zone.py
  2. +19
    -0
      tests/test_octodns_zone.py

+ 22
- 0
octodns/zone.py View File

@ -75,6 +75,28 @@ class Zone(object):
# it has utf8 chars
return self._utf8_name_re.sub('', fqdn)
def owns(self, _type, fqdn):
if fqdn[-1] != '.':
fqdn = f'{fqdn}.'
# if we don't end with the zone's name we aren't owned by it
if not fqdn.endswith(self.name):
return False
hostname = self.hostname_from_fqdn(fqdn)
if hostname in self.sub_zones:
# if our hostname matches a sub-zone exactly we have to be a NS
# record
return _type == 'NS'
for sub_zone in self.sub_zones:
if hostname.endswith(f'.{sub_zone}'):
# this belongs under a sub-zone
return False
# otherwise we own it
return True
def add_record(self, record, replace=False, lenient=False):
if self._origin:
self.hydrate()


+ 19
- 0
tests/test_octodns_zone.py View File

@ -191,6 +191,25 @@ class TestZone(TestCase):
Zone('space not allowed.', [])
self.assertTrue('whitespace not allowed' in str(ctx.exception))
def test_owns(self):
zone = Zone('unit.tests.', set(['sub']))
self.assertTrue(zone.owns('A', 'unit.tests'))
self.assertTrue(zone.owns('A', 'unit.tests.'))
self.assertTrue(zone.owns('A', 'www.unit.tests.'))
self.assertTrue(zone.owns('A', 'www.unit.tests.'))
# we do own our direct sub's delegation NS records
self.assertTrue(zone.owns('NS', 'sub.unit.tests.'))
# we don't own the root of our sub
self.assertFalse(zone.owns('A', 'sub.unit.tests.'))
# of anything under it
self.assertFalse(zone.owns('A', 'www.sub.unit.tests.'))
# including subsequent delegatoin NS records
self.assertFalse(zone.owns('NS', 'below.sub.unit.tests.'))
def test_sub_zones(self):
# NS for exactly the sub is allowed
zone = Zone('unit.tests.', set(['sub', 'barred']))


Loading…
Cancel
Save