]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding evaluating configuration
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 9 Nov 2017 16:35:50 +0000 (17:35 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 9 Nov 2017 16:35:50 +0000 (17:35 +0100)
pp_lib/pdns_app.py

index dd827deec39bdd8bf6cc1a41368a8f9ba1e2a7a1..68a8818e7ab1b476f259f4893e8bbd333e78d42f 100644 (file)
@@ -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):
         """