From fda93452cae9c52435a4a2ef6deaf819779d714f Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Thu, 17 Oct 2024 14:13:59 -0700 Subject: [PATCH] Zone.apply(changes) --- octodns/zone.py | 10 ++++++++++ tests/test_octodns_zone.py | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/octodns/zone.py b/octodns/zone.py index 5f23267..a94cda9 100644 --- a/octodns/zone.py +++ b/octodns/zone.py @@ -340,6 +340,16 @@ class Zone(object): return changes + def apply(self, changes): + ''' + Apply the provided changes to the zone. + ''' + for change in changes: + if isinstance(change, Delete): + self.remove_record(change.existing) + else: + self.add_record(change.new, replace=True, lenient=True) + def hydrate(self): ''' Take a shallow copy Zone and make it a deeper copy holding its own diff --git a/tests/test_octodns_zone.py b/tests/test_octodns_zone.py index 4008210..16789d2 100644 --- a/tests/test_octodns_zone.py +++ b/tests/test_octodns_zone.py @@ -179,6 +179,9 @@ class TestZone(TestCase): # before == after -> no changes self.assertFalse(before.changes(after, target)) + copy = before.copy() + copy.apply([]) + self.assertEqual(copy.records, before.records) # add a record, delete a record -> [Delete, Create] c = ARecord(before, 'c', {'ttl': 42, 'value': '1.1.1.1'}) @@ -199,6 +202,15 @@ class TestZone(TestCase): delete.__repr__() create.__repr__() + # make a copy of before + copy = before.copy() + # apply the changes to it + copy.apply(changes) + # copy should not match it's origin any longer + self.assertNotEqual(copy.records, before.records) + # and it should now match the target + self.assertEqual(copy.records, after.records) + after = Zone('unit.tests.', []) changed = ARecord(before, 'a', {'ttl': 42, 'value': '2.2.2.2'}) after.add_record(changed)