]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Extending lib/pp_admintools/pdns_config.py
authorFrank Brehm <frank@brehm-online.com>
Wed, 30 Mar 2022 13:29:45 +0000 (15:29 +0200)
committerFrank Brehm <frank@brehm-online.com>
Wed, 30 Mar 2022 13:29:45 +0000 (15:29 +0200)
lib/pp_admintools/pdns_config.py

index e12365dbf2d603d725e121777707ed9182f91f24..d31137f3bbfcc135f92c05a0a9e62db1cfef0aaf 100644 (file)
@@ -34,7 +34,7 @@ from .xlate import XLATOR
 
 LIBRARY_NAME = "pp-pdns-api-client"
 
-__version__ = '0.1.3'
+__version__ = '0.2.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -99,6 +99,12 @@ class PdnsConfiguration(MailConfiguration):
 
         self.pdns_timeout = self.default_pdns_timeout
 
+        self.pdns_instance = self.default_pdns_instance
+        self.pdns_host = None
+        self.pdns_port = None
+        self.pdns_key = None
+        self.pdns_servername = None
+
         add_stems = []
         if additional_stems:
             if is_sequence(additional_stems):
@@ -145,6 +151,13 @@ class PdnsConfiguration(MailConfiguration):
         res['default_pdns_instance'] = self.default_pdns_instance
         res['valid_pdns_api_instances'] = self.valid_pdns_api_instances
 
+        res['pdns_key'] = None
+        if self.pdns_key:
+            if self.verbose <= 4:
+                res['pdns_key'] = '******'
+            else:
+                res['pdns_key'] = self.pdns_key
+
         res['pdns_api_instances'] = {}
         for iname in self.pdns_api_instances.keys():
             inst = self.pdns_api_instances[iname]
@@ -161,7 +174,9 @@ class PdnsConfiguration(MailConfiguration):
         super(PdnsConfiguration, self).eval_section(section_name)
         sn = section_name.lower()
 
-        if sn == 'pdns' or sn == 'powerdns':
+        re_pdns = re.compile(r'^(?:pdns|powerdns)(?:[_-]?api)?$')
+
+        if re_pdns.search(sn):
             section = self.cfg[section_name]
             return self._eval_pdns(section_name, section)
 
@@ -172,58 +187,161 @@ class PdnsConfiguration(MailConfiguration):
             msg = _("Evaluating config section {!r}:").format(section_name)
             LOG.debug(msg + '\n' + pp(section))
 
-        self._eval_api_user_agent(section_name, section)
-        self._eval_pdns_timeout(section_name, section)
-        self._eval_pdns_instances(section_name, section)
-
-    # -------------------------------------------------------------------------
-    def _eval_api_user_agent(self, section_name, section):
-
         re_agent = re.compile(r'^\s*(?:api[_-]?)?user[_-]?agent\s*$', re.IGNORECASE)
+        re_timeout = re.compile(r'^\s*timeout\s*$', re.IGNORECASE)
+        re_inst = re.compile(r'^\s*instances\s*$', re.IGNORECASE)
+        re_env = re.compile(r'^\s*(?:env(?:ironment)?|inst(?:ance)?)\s*$', re.IGNORECASE)
+        re_host = re.compile(r'^\s*(?:api[_-]?)?host\s*$', re.IGNORECASE)
+        re_port = re.compile(r'^\s*(?:api[_-]?)?port\s*$', re.IGNORECASE)
+        re_key = re.compile(r'^\s*(?:api[_-]?)?key\s*$', re.IGNORECASE)
+        re_servername = re.compile(r'^\s*(?:api[_-]?)?servername\s*$', re.IGNORECASE)
 
         for key in section.keys():
-            if not re_agent.search(key):
+
+            if re_agent.search(key):
+                self._eval_api_user_agent(section_name, key, section)
                 continue
 
-            val = section[key].strip()
-            if val:
-                self.api_user_agent = val
+            if re_timeout.search(key):
+                self._eval_pdns_timeout(section_name, key, section)
+                continue
 
-    # -------------------------------------------------------------------------
-    def _eval_pdns_timeout(self, section_name, section):
+            if re_env.search(key):
+                self._eval_pdns_environment(section_name, key, section)
+                continue
 
-        re_timeout = re.compile(r'^\s*timeout\s*$', re.IGNORECASE)
+            if re_host.search(key):
+                self._eval_pdns_host(section_name, key, section)
+                continue
 
-        for key in section.keys():
-            if not re_timeout.search(key):
+            if re_port.search(key):
+                self._eval_pdns_port(section_name, key, section)
                 continue
 
-            val = section[key]
-            try:
-                timeout = int(val)
-                if timeout <= 0 or timeout > MAX_TIMEOUT:
-                    msg = _("A timeout has to be between 1 and {} seconds.")
-                    msg = msg.format(MAX_TIMEOUT)
-                    raise ValueError(msg)
-            except (ValueError, TypeError) as e:
-                msg = _("Value {!r} for PowerDNS API timeout is invalid:").format(val)
-                msg += " " + str(e)
-                LOG.error(msg)
+            if re_key.search(key):
+                self._eval_pdns_key(section_name, key, section)
+                continue
+
+            if re_servername.search(key):
+                self._eval_pdns_re_servername(section_name, key, section)
                 continue
 
-            self.pdns_timeout = timeout
+            if re_inst.search(key):
+                self._eval_pdns_instances(section_name, key, section)
+                continue
 
     # -------------------------------------------------------------------------
-    def _eval_pdns_instances(self, section_name, section):
+    def _eval_api_user_agent(self, section_name, key, section):
 
-        re_inst = re.compile(r'^\s*instances?\s*$', re.IGNORECASE)
+        val = section[key].strip()
+        if val:
+            self.api_user_agent = val
 
-        for key in section.keys():
-            if not re_inst.search(key):
-                continue
+    # -------------------------------------------------------------------------
+    def _eval_pdns_timeout(self, section_name, key, section):
+
+        val = section[key]
+        try:
+            timeout = int(val)
+            if timeout <= 0 or timeout > MAX_TIMEOUT:
+                msg = _("A timeout has to be between 1 and {} seconds.")
+                msg = msg.format(MAX_TIMEOUT)
+                raise ValueError(msg)
+        except (ValueError, TypeError) as e:
+            msg = _("Value {!r} for PowerDNS API timeout is invalid:").format(val)
+            msg += " " + str(e)
+            LOG.error(msg)
+            continue
+
+        self.pdns_timeout = timeout
+
+    # -------------------------------------------------------------------------
+    def _eval_pdns_environment(self, section_name, key, section):
+
+        env = section[key].strip().lower()
 
-            for instance_name in section[key].keys():
-                self._eval_pdns_instance(self, instance_name, section[key][instance_name])
+        if not env:
+            return
+
+        if env not in self.pdns_api_instances:
+            msg = _("Found invalid PDNS environment/instance {!r} in configuration.")
+            msg = msg.format(section[key])
+            if self.raise_on_error:
+                raise PdnsConfigError(msg)
+            LOG.error(msg)
+            return
+
+        self.pdns_instance = env
+
+    # -------------------------------------------------------------------------
+    def _eval_pdns_host(self, section_name, key, section):
+
+        val = section[key].strip().lower()
+        if val:
+            if self.verbose > 2:
+                msg = _("Found PDNS host: {!r}.").format(val)
+                LOG.debug(msg)
+
+            self.pdns_host = val
+
+    # -------------------------------------------------------------------------
+    def _eval_pdns_port(self, section_name, key, section):
+
+        val = section[key]
+        if not val:
+            return
+
+        port = None
+        try:
+            port = int(val)
+            if port <= 0 or port > MAX_PORT_NUMBER:
+                msg = _("A port must be greater than 0 and less than {}.")
+                raise ValueError(msg.format(MAX_PORT_NUMBER))
+        except (TypeError, ValueError) as e:
+            msg = _("Wrong PDNS port number {p!r} found: {e}").format(p=val, e=e)
+            if self.raise_on_error:
+                raise PdnsConfigError(msg)
+            else:
+                LOG.error(msg)
+                port = None
+
+        if port:
+            if self.verbose > 2:
+                msg = _("Found port number for PDNS: {}.").format(port)
+                LOG.debug(msg)
+
+            self.pdns_port = port
+
+    # -------------------------------------------------------------------------
+    def _eval_pdns_key(self, section_name, key, section):
+
+        val = section[key].strip()
+        if val:
+            if self.verbose > 2:
+                key_show = '******'
+                self.verbose > 4:
+                    key_show = val
+                msg = _("Found API key for PDNS: {!r}.").format(key_show)
+                LOG.debug(msg)
+
+            self.pdns_key = val
+
+    # -------------------------------------------------------------------------
+    def _eval_pdns_servername(self, section_name, key, section):
+
+        val = section[key].strip()
+        if val:
+            if self.verbose > 2:
+                msg = _("Found PDNS API servername: {!r}.").format(val)
+                LOG.debug(msg)
+
+            self.pdns_servername = val
+
+    # -------------------------------------------------------------------------
+    def _eval_pdns_instances(self, section_name, key, section):
+
+        for instance_name in section[key].keys():
+            self._eval_pdns_instance(self, instance_name, section[key][instance_name])
 
     # -------------------------------------------------------------------------
     def _eval_pdns_instance(self, instance_name, section):
@@ -324,6 +442,25 @@ class PdnsConfiguration(MailConfiguration):
                         LOG.debug(msg.format(inst=iname, key=key_show))
                     self.pdns_api_instances[iname]['key'] = api_key
 
+    # -------------------------------------------------------------------------
+    def eval(self):
+
+        super(PdnsConfiguration, self).eval()
+
+        inst = self.pdns_instance
+
+        if not self.pdns_host:
+            self.pdns_host = self.pdns_api_instances[inst]['host']
+
+        if not self.pdns_port:
+            self.pdns_port = self.pdns_api_instances[inst]['port']
+
+        if not self.pdns_key:
+            self.pdns_key = self.pdns_api_instances[inst]['key']
+
+        if not self.pdns_servername:
+            self.pdns_servername = self.pdns_api_instances[inst]['servername']
+
 
 # =============================================================================
 if __name__ == "__main__":