]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Changing constants, adding configs for valid PDNS instances and PDNS API user agent
authorFrank Brehm <frank@brehm-online.com>
Tue, 29 Mar 2022 16:47:26 +0000 (18:47 +0200)
committerFrank Brehm <frank@brehm-online.com>
Tue, 29 Mar 2022 16:47:26 +0000 (18:47 +0200)
lib/pp_admintools/pdns_config.py

index 276e2a52111f16af9b4e178d136f11496338891d..8052d0bc96d2284e8d33d7a931017f841f9645a5 100644 (file)
@@ -24,14 +24,17 @@ from fb_tools.common import is_sequence, pp
 # from .config import ConfigError, BaseConfiguration
 from fb_tools.multi_config import DEFAULT_ENCODING
 
+from . import __version__ as GLOBAL_VERSION
+from . import MAX_TIMEOUT, MAX_PORT_NUMBER
+
 from .mail_config import MailConfigError, MailConfiguration
-from .mail_config import DEFAULT_CONFIG_DIR, MAX_PORT_NUMBER
+from .mail_config import DEFAULT_CONFIG_DIR
 
 from .xlate import XLATOR
 
-MAX_PDNS_API_TIMEOUT = 3600
+LIBRARY_NAME = "pp-pdns-api-client"
 
-__version__ = '0.1.2'
+__version__ = '0.1.3'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -52,6 +55,8 @@ class PdnsConfiguration(MailConfiguration):
     and methods to read it from configuration files.
     """
 
+    valid_pdns_api_instances = ('global', 'public', 'local')
+
     default_pdns_api_instances = {
         'global': {
             'host': "dnsmaster.pp-dns.com",
@@ -77,6 +82,8 @@ class PdnsConfiguration(MailConfiguration):
             additional_config_file=None, additional_cfgdirs=None, encoding=DEFAULT_ENCODING,
             ensure_privacy=True, use_chardet=True, initialized=False):
 
+        self.api_user_agent = '{}/{}'.format(LIBRARY_NAME, GLOBAL_VERSION)
+
         self.pdns_api_instances = {}
         for inst_name in self.default_pdns_api_instances.keys():
 
@@ -136,6 +143,7 @@ class PdnsConfiguration(MailConfiguration):
         res['default_pdns_api_servername'] = self.default_pdns_api_servername
         res['default_pdns_timeout'] = self.default_pdns_timeout
         res['default_pdns_instance'] = self.default_pdns_instance
+        res{'valid_pdns_api_instances'] = self.valid_pdns_api_instances
 
         res['pdns_api_instances'] = {}
         for iname in self.pdns_api_instances.keys():
@@ -164,9 +172,23 @@ 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)
+
+        for key in section.keys():
+            if not re_agent.search(key):
+                continue
+
+            val = section[key].strip()
+            if val:
+                self.api_user_agent = val
+
     # -------------------------------------------------------------------------
     def _eval_pdns_timeout(self, section_name, section):
 
@@ -179,9 +201,9 @@ class PdnsConfiguration(MailConfiguration):
             val = section[key]
             try:
                 timeout = int(val)
-                if timeout <= 0 or timeout > MAX_PDNS_API_TIMEOUT:
+                if timeout <= 0 or timeout > MAX_TIMEOUT:
                     msg = _("A timeout has to be between 1 and {} seconds.")
-                    msg = msg.format(MAX_PDNS_API_TIMEOUT)
+                    msg = msg.format(MAX_TIMEOUT)
                     raise ValueError(msg)
             except (ValueError, TypeError) as e:
                 msg = _("Value {!r} for PowerDNS API timeout is invalid:").format(val)