from .pdns_zone import PdnsApiZone
from .pdns_record import compare_rrsets
-__version__ = '0.2.2'
+__version__ = '0.2.3'
LOG = logging.getLogger(__name__)
if not zone:
return False
+ new_nameservers = []
+
if self.is_local(zone_name):
LOG.debug("Using local nameservers for substituting.")
+ new_nameservers = sorted(self.new_local_nameservers)
else:
LOG.debug("Using public nameservers for substituting.")
+ new_nameservers = sorted(self.new_public_nameservers)
+ if self.verbose > 1:
+ LOG.debug("Expected nameservers of zone:\n{}".format(pp(new_nameservers)))
soa = zone.get_soa()
if not soa:
if self.verbose > 2:
LOG.debug("SOA of zone {!r}:\n{}".format(zone_name, soa))
+ if not self.ensure_nameservers(zone, new_nameservers):
+ return False
+
+ zone_parts = zone_name.split('.')
+ top_zone_name = '.'.join(zone_parts[1:])
+ LOG.debug("Top zone of {z!r} is {t!r}.".format(z=zone_name, t=top_zone_name))
+
+ have_top_zone = False
+ for t_zone in self.zones:
+ if t_zone.name == top_zone_name:
+ have_top_zone = True
+ break
+
+ if have_top_zone:
+ LOG.info("Setting nameservers for zone {z!r} in zone {t!r}.".format(
+ z=zone_name, t=top_zone_name))
+ top_zone = self.get_api_zone(top_zone_name)
+ return self.ensure_nameservers(top_zone, new_nameservers, zone_name)
+ else:
+ LOG.debug("Top zone {!r} is not in our responsibility.".format(top_zone_name))
+ return True
+
+ # -------------------------------------------------------------------------
+ def ensure_nameservers(self, zone, new_nameservers, for_zone=None):
+
+ current_nameservers = zone.get_zone_nameservers(for_zone=for_zone)
+ LOG.debug("Current nameservers of zone {z!r}:\n{ns}".format(
+ z=zone.name, ns=pp(current_nameservers)))
+
+ ns2remove = []
+ ns2add = []
+
+ for ns in current_nameservers:
+ if ns not in new_nameservers:
+ ns2remove.append(ns)
+ for ns in new_nameservers:
+ if ns not in current_nameservers:
+ ns2add.append(ns)
+
+ if not ns2remove and not ns2add:
+ LOG.info("Zone {!r} has already the expected zones.".format(zone.name))
+ return False
+
+ LOG.debug("Nameservers to remove from zone {z!r}:\n{ns}".format(
+ z=zone.name, ns=pp(ns2remove)))
+ LOG.debug("Nameservers to add to zone {z!r}:\n{ns}".format(
+ z=zone.name, ns=pp(ns2add)))
+
return True
+
# =============================================================================
if __name__ == "__main__":