Skip to content

Commit a938ac8

Browse files
authored
Merge pull request octodns#1298 from octodns/ownership-on-alias
Add OwnershipProcessor.should_replace to support ownership on alias zones
2 parents 470df43 + 8c22040 commit a938ac8

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
type: minor
3+
---
4+
Add OwnershipProcessor.should_replace to support ownership on alias zones

octodns/processor/ownership.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@
1414
# and thus "own" them going forward.
1515
class OwnershipProcessor(BaseProcessor):
1616
def __init__(
17-
self, name, txt_name='_owner', txt_value='*octodns*', txt_ttl=60
17+
self,
18+
name,
19+
txt_name='_owner',
20+
txt_value='*octodns*',
21+
txt_ttl=60,
22+
should_replace=False,
1823
):
1924
super().__init__(name)
2025
self.txt_name = txt_name
2126
self.txt_value = txt_value
2227
self.txt_ttl = txt_ttl
2328
self._txt_values = [txt_value]
29+
self.should_replace = should_replace
2430

2531
def process_source_zone(self, desired, *args, **kwargs):
2632
for record in desired.records:
33+
if self._is_ownership(record):
34+
# don't apply ownership to existing ownership recorcs, most
35+
# likely to see this in an alias zone that will be proccessed
36+
# once as the original and a 2nd time as the alias
37+
continue
2738
# Then create and add an ownership TXT for each of them
2839
record_name = record.name.replace('*', '_wildcard')
2940
if record.name:
@@ -37,7 +48,7 @@ def process_source_zone(self, desired, *args, **kwargs):
3748
)
3849
# add these w/lenient to cover the case when the ownership record
3950
# for a NS delegation record should technically live in the subzone
40-
desired.add_record(txt, lenient=True)
51+
desired.add_record(txt, lenient=True, replace=self.should_replace)
4152

4253
return desired
4354

tests/test_octodns_processor_ownership.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from octodns.processor.ownership import OwnershipProcessor
1010
from octodns.provider.plan import Plan
1111
from octodns.record import Delete, Record
12-
from octodns.zone import Zone
12+
from octodns.zone import DuplicateRecordException, Zone
1313

1414
zone = Zone('unit.tests.', [])
1515
records = {}
@@ -150,3 +150,30 @@ def test_remove_last_change(self):
150150
self.assertEqual(1, len(plan.changes))
151151
plan = ownership.process_plan(plan)
152152
self.assertFalse(plan)
153+
154+
def test_should_replace(self):
155+
ownership = OwnershipProcessor('ownership')
156+
self.assertFalse(ownership.should_replace)
157+
158+
zone = Zone('unit.tests.', [])
159+
record = Record.new(
160+
zone, 'a', {'ttl': 30, 'type': 'A', 'value': '4.4.4.4'}
161+
)
162+
zone.add_record(record)
163+
164+
got = ownership.process_source_zone(zone.copy())
165+
self.assertEqual(
166+
['_owner.a.a', 'a'], sorted([r.name for r in got.records])
167+
)
168+
169+
# will fail w/a duplicate
170+
with self.assertRaises(DuplicateRecordException):
171+
ownership.process_source_zone(got.copy())
172+
173+
# enable should_replace, will replace instead of failing
174+
ownership.should_replace = True
175+
got = ownership.process_source_zone(got.copy())
176+
# same expected result
177+
self.assertEqual(
178+
['_owner.a.a', 'a'], sorted([r.name for r in got.records])
179+
)

0 commit comments

Comments
 (0)