]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Decreasing complexity of method import_records()
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 6 Feb 2018 14:37:26 +0000 (15:37 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 6 Feb 2018 14:37:26 +0000 (15:37 +0100)
pp_lib/import_pdnsdata.py

index 9294768c1fce9fdde932b46c17581f9b2d349036..ff9c064f4ae26d032948be3113d7da517a6296b6 100644 (file)
@@ -31,7 +31,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication
 
 from .pdns_record import PdnsSoaData
 
-__version__ = '0.10.2'
+__version__ = '0.10.3'
 LOG = logging.getLogger(__name__)
 
 # =============================================================================
@@ -71,6 +71,15 @@ class ImportPdnsdataApp(PpConfigApplication):
             VALUES (%(domain_id)s, %(kind)s, %(content)s)
         ''').strip()
 
+    sql_insert_record = textwrap.dedent('''\
+        INSERT INTO records (id, domain_id, name, type, content,
+                             ttl, prio, change_date, disabled,
+                             ordername, auth)
+             VALUES (%(id)s, %(domain_id)s, %(name)s, %(type)s, %(content)s,
+                     %(ttl)s, %(prio)s, %(change_date)s, %(disabled)s,
+                     %(ordername)s, %(auth)s)
+        ''').strip()
+
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, version=__version__):
 
@@ -757,6 +766,49 @@ class ImportPdnsdataApp(PpConfigApplication):
         LOG.debug("Commiting changes ...")
         self.tgt_connection.commit()
 
+    # -------------------------------------------------------------------------
+    def _import_record(self, record, tgt_cursor):
+
+        if self.tgt_db_type == 'mysql':
+            record['disabled'] = 0
+        else:
+            record['disabled'] = False
+            if record['auth'] is None:
+                record['auth'] = True
+            else:
+                if record['auth']:
+                    record['auth'] = True
+                else:
+                    record['auth'] = False
+            if record['ordername'] is None:
+                dom_id = record['domain_id']
+                if dom_id in self.domain_ids:
+                    dom_name = self.domain_ids[dom_id]
+                    if record['name'] == dom_name:
+                        record['ordername'] = ''
+                    else:
+                        idx = record['name'].rfind('.' + dom_name)
+                        if idx >= 0:
+                            record['ordername'] = record['name'][:idx]
+                        else:
+                            record['ordername'] = ''
+                else:
+                    record['ordername'] = ''
+            if record['type'] in ('NS', 'MX'):
+                record['content'] = RE_DOT_AT_END.sub('', record['content'])
+            elif record['type'] == 'SOA':
+                soa = PdnsSoaData.init_from_data(
+                    record['content'], appname=self.appname,
+                    verbose=self.verbose, base_dir=self.base_dir)
+                soa.primary = RE_DOT_AT_END.sub('', soa.primary)
+                soa.email = RE_DOT_AT_END.sub('', soa.email)
+                record['content'] = soa.data
+        if self.verbose > 3:
+            LOG.debug("SQL for insert record:\n{}".format(
+                to_str(tgt_cursor.mogrify(self.sql_insert_record, record))))
+        if not self.simulate:
+            tgt_cursor.execute(self.sql_insert_record, record)
+
     # -------------------------------------------------------------------------
     def import_records(self):
 
@@ -773,16 +825,8 @@ class ImportPdnsdataApp(PpConfigApplication):
         if self.verbose > 1:
             LOG.debug("Source SQL:\n{}".format(src_sql))
 
-        tgt_sql = textwrap.dedent('''\
-            INSERT INTO records (id, domain_id, name, type, content,
-                                 ttl, prio, change_date, disabled,
-                                 ordername, auth)
-                 VALUES (%(id)s, %(domain_id)s, %(name)s, %(type)s, %(content)s,
-                         %(ttl)s, %(prio)s, %(change_date)s, %(disabled)s,
-                         %(ordername)s, %(auth)s)
-            ''').strip()
         if self.verbose > 1:
-            LOG.debug("Target SQL:\n{}".format(tgt_sql))
+            LOG.debug("Target SQL:\n{}".format(self.sql_insert_record))
 
         with self.tgt_connection.cursor() as tgt_cursor:
             with self.src_connection.cursor() as src_cursor:
@@ -802,42 +846,8 @@ class ImportPdnsdataApp(PpConfigApplication):
 
                 for result in results:
                     i += 1
-                    if self.tgt_db_type == 'mysql':
-                        result['disabled'] = 0
-                    else:
-                        result['disabled'] = False
-                        if result['auth'] is None:
-                            result['auth'] = True
-                        else:
-                            if result['auth']:
-                                result['auth'] = True
-                            else:
-                                result['auth'] = False
-                        if result['ordername'] is None:
-                            dom_id = result['domain_id']
-                            if dom_id in self.domain_ids:
-                                dom_name = self.domain_ids[dom_id]
-                                if result['name'] == dom_name:
-                                    result['ordername'] = ''
-                                else:
-                                    idx = result['name'].rfind('.' + dom_name)
-                                    if idx >= 0:
-                                        result['ordername'] = result['name'][:idx]
-                                    else:
-                                        result['ordername'] = ''
-                            else:
-                                result['ordername'] = ''
-                        if result['type'] in ('NS', 'MX'):
-                            result['content'] = RE_DOT_AT_END.sub('', result['content'])
-                        elif result['type'] == 'SOA':
-                            soa = PdnsSoaData.init_from_data(
-                                result['content'], appname=self.appname,
-                                verbose=self.verbose, base_dir=self.base_dir)
-                            soa.primary = RE_DOT_AT_END.sub('', soa.primary)
-                            soa.email = RE_DOT_AT_END.sub('', soa.email)
-                            result['content'] = soa.data
-                    if not self.simulate:
-                        tgt_cursor.execute(tgt_sql, result)
+                    self._import_record(result, tgt_cursor)
+
                 LOG.info("Imported {} records.".format(i))
 
             if self.tgt_db_type != 'mysql':
@@ -853,7 +863,7 @@ class ImportPdnsdataApp(PpConfigApplication):
                 sql = "SELECT SETVAL('records_id_seq', %s)"
                 LOG.debug("Setting curval of records_id_seq to {} ...".format(max_id))
                 if self.verbose > 1:
-                    LOG.debug("SQL: {}".format(sql))
+                    LOG.debug("SQL: {}".format(to_str(tgt_cursor.mogrify(sql, (max_id, )))))
                 if not self.simulate:
                     tgt_cursor.execute(sql, (max_id, ))