From: Frank Brehm Date: Wed, 8 Nov 2017 13:40:27 +0000 (+0100) Subject: Retrieving default TTL and complete zone data X-Git-Tag: 0.1.2~104 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=00ad15522cdcfe6fe1d4d944bf3e1ee278c72779;p=pixelpark%2Fadmin-tools.git Retrieving default TTL and complete zone data --- diff --git a/pp_lib/pdns_show_zone.py b/pp_lib/pdns_show_zone.py index 5e65022..35e7bd9 100644 --- a/pp_lib/pdns_show_zone.py +++ b/pp_lib/pdns_show_zone.py @@ -20,10 +20,10 @@ from functools import cmp_to_key from .common import pp, compare_fqdn, to_str from .common import RE_DOT_AT_END -from .pdns_app import PpPDNSAppError, PpPDNSApplication +from .pdns_app import PpPDNSAppError, PpPDNSApplication, PDNSApiNotFoundError, PDNSApiValidationError from .pdns_zone import PdnsApiZone -__version__ = '0.2.0' +__version__ = '0.3.1' LOG = logging.getLogger(__name__) @@ -52,6 +52,7 @@ class PpPDNSShowZoneApp(PpPDNSApplication): ) self.initialized = True + self.default_ttl = 3600 # ------------------------------------------------------------------------- def init_arg_parser(self): @@ -82,24 +83,65 @@ class PpPDNSShowZoneApp(PpPDNSApplication): zone_idna = zone if 'xn--' not in zone: zone_idna = to_str(zone.encode('idna')) - zone_idna = RE_DOT_AT_END.sub('.', zone_idna) + zone_idna = RE_DOT_AT_END.sub('.', zone_idna).lower() self.zones.append(zone_idna) # ------------------------------------------------------------------------- def _run(self): - for zone in self.zones: + success = True + self.get_default_ttl() - zone_unicode = zone - zout = "{!r}".format(zone) - if 'xn--' in zone: - zone_unicode = zone.encode('idna').decode('idna') - zout = "{!r} ({})".format(zone, zone_unicode) + for zone in self.zones: + if not self.show_zone(zone): + success = False - LOG.info("Show all information about zone {} from PowerDNS environment {!r}.".format( - zout, self.environment)) + if not success: + self.exit(1) + # ------------------------------------------------------------------------- + def get_default_ttl(self): + + LOG.debug("Retrieving defaul TTL from server ...") + path = "/servers/{}/config".format(self.api_servername) + json_response = self.perform_request(path) + ttl = None + + for cfg in json_response: + if cfg['name'] == 'default-ttl': + try: + ttl = int(cfg['value']) + except ValueError as e: + LOG.error("Found invalid TTL {!r} from server: {}".format( + cfg['value'], e)) + break + if ttl: + LOG.debug("Got a default TTL {} from server.".format(ttl)) + self.default_ttl = ttl + # ------------------------------------------------------------------------- + def show_zone(self, zone): + + zone_unicode = zone + json_response = None + zout = "{!r}".format(zone) + if 'xn--' in zone: + zone_unicode = zone.encode('idna').decode('idna') + zout = "{!r} ({})".format(zone, zone_unicode) + + LOG.info("Show all information about zone {} from PowerDNS environment {!r}.".format( + zout, self.environment)) + + path = "/servers/{}/zones/{}".format(self.api_servername, zone) + try: + json_response = self.perform_request(path) + except (PDNSApiNotFoundError, PDNSApiValidationError) as e: + LOG.error("The given zone {} was not found.".format(zout)) + return False + if self.verbose > 2: + LOG.debug("Got a response:\n{}".format(pp(json_response))) + + return True # =============================================================================