From 35a6c85bbb5cfb304112dee83468d0752847610d Mon Sep 17 00:00:00 2001 From: Ross McFarland Date: Thu, 11 Aug 2022 06:07:03 -0700 Subject: [PATCH] Rework Manager.zone_tree into a property --- octodns/manager.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/octodns/manager.py b/octodns/manager.py index 0faa0bf..f9542aa 100644 --- a/octodns/manager.py +++ b/octodns/manager.py @@ -185,25 +185,6 @@ class Manager(object): 'Incorrect processor config for ' + processor_name ) - zone_tree = {} - # Sort so we iterate on the deepest nodes first, ensuring if a parent - # zone exists it will be seen after the subzone, thus we can easily - # reparent children to their parent zone from the tree root. - for name in sorted( - self.config['zones'].keys(), key=lambda s: 0 - s.count('.') - ): - # Trim the trailing dot from FQDN - name = name[:-1] - this = {} - for sz in [k for k in zone_tree.keys() if k.endswith(name)]: - # Found a zone in tree root that is our child, slice the - # name and move its tree under ours. - this[sz[: -(len(name) + 1)]] = zone_tree.pop(sz) - # Add to tree root where it will be reparented as we iterate up - # the tree. - zone_tree[name] = this - self.zone_tree = zone_tree - self.plan_outputs = {} plan_outputs = manager_config.get( 'plan_outputs', @@ -244,6 +225,32 @@ class Manager(object): 'Incorrect plan_output config for ' + plan_output_name ) + self._zone_tree = None + + @property + def zone_tree(self): + if self._zone_tree is None: + zone_tree = {} + # Sort so we iterate on the deepest nodes first, ensuring if a parent + # zone exists it will be seen after the subzone, thus we can easily + # reparent children to their parent zone from the tree root. + for name in sorted( + self.config['zones'].keys(), key=lambda s: 0 - s.count('.') + ): + # Trim the trailing dot from FQDN + name = name[:-1] + this = {} + for sz in [k for k in zone_tree.keys() if k.endswith(name)]: + # Found a zone in tree root that is our child, slice the + # name and move its tree under ours. + this[sz[: -(len(name) + 1)]] = zone_tree.pop(sz) + # Add to tree root where it will be reparented as we iterate up + # the tree. + zone_tree[name] = this + self._zone_tree = zone_tree + + return self._zone_tree + def _try_version(self, module_name, module=None, version=None): try: # Always try and use the official lookup first