From d80a6dea89cba7de82dee1ec094b23d5c01fd1da Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 9 Nov 2017 17:35:50 +0100 Subject: [PATCH] Adding evaluating configuration --- pp_lib/pdns_app.py | 89 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/pp_lib/pdns_app.py b/pp_lib/pdns_app.py index dd827de..68a8818 100644 --- a/pp_lib/pdns_app.py +++ b/pp_lib/pdns_app.py @@ -14,6 +14,7 @@ import logging.config import re import copy import json +import os # Third party modules import requests @@ -24,7 +25,7 @@ from .common import pp, to_bool from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.2.2' +__version__ = '0.3.1' LOG = logging.getLogger(__name__) _LIBRARY_NAME = "pp-pdns-api-client" @@ -347,6 +348,92 @@ class PpPDNSApplication(PpConfigApplication): if self.args.timeout: self.timeout = self.args.timeout + # ------------------------------------------------------------------------- + def perform_config(self): + + super(PpPDNSApplication, self).perform_config() + + for section_name in self.cfg.keys(): + + if self.verbose > 3: + LOG.debug("Checking config section {!r} ...".format(section_name)) + + section = self.cfg[section_name] + + if section_name.lower() in ( + 'powerdns-api', 'powerdns_api', 'powerdnsapi', + 'pdns-api', 'pdns_api', 'pdnsapi' ): + self.set_cfg_api_options(section, section_name) + + # ------------------------------------------------------------------------- + def set_cfg_api_options(self, section, section_name): + + if self.verbose > 2: + LOG.debug("Evaluating config section {n!r}:\n{s}".format( + n=section_name, s=pp(section))) + + if 'environment' in section: + v = section['environment'].strip().lower() + if v not in self.api_hosts: + LOG.error("Wrong environment {!r} found in configuration.".format( + section['environment'])) + self.config_has_errors = True + else: + self.environment = v + + if 'host' in section: + v = section['host'] + host = v.lower().strip() + if host: + self.api_host = host + + if 'port' in section: + try: + port = int(section['port']) + if port <= 0 or port > 2**16: + raise ValueError( + "a port must be greater than 0 and less than {}.".format(2**16)) + except (TypeError, ValueError) as e: + LOG.error("Wrong port number {!r} in configuration section {!r}: {}".format( + section['port'], section_name, e)) + self.config_has_errors = True + else: + self.api_port = port + + if 'server_id' in section and section['server_id'].strip(): + self.api_servername = section['server_id'].strip().lower() + + if 'key' in section: + key = section['key'].strip() + self.api_key = key + + # ------------------------------------------------------------------------- + def _check_path_config(self, section, section_name, key, class_prop, absolute=True, desc=None): + + if key not in section: + return + + d = '' + if desc: + d = ' ' + str(desc).strip() + + path = section[key].strip() + if not path: + msg = "No path given for{} [{}]/{} in configuration.".format( + d, section_name, key) + LOG.error(msg) + self.config_has_errors = True + return + + if absolute and not os.path.isabs(path): + msg = "Path {!r} for{} [{}]/{} in configuration must be an absolute path.".format( + path, d, section_name, key) + LOG.error(msg) + self.config_has_errors = True + return + + setattr(self, class_prop, path) + # ------------------------------------------------------------------------- def pre_run(self): """ -- 2.39.5