import re
import copy
import json
+import os
# Third party modules
import requests
from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.2.2'
+__version__ = '0.3.1'
LOG = logging.getLogger(__name__)
_LIBRARY_NAME = "pp-pdns-api-client"
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):
"""