]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Continuing adding support for poweradmin on importing PDNS data
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 27 Jul 2017 12:59:38 +0000 (14:59 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 27 Jul 2017 12:59:38 +0000 (14:59 +0200)
db/create-poweradmin-db-postgresql.sql
pp_lib/import_pdnsdata.py

index 37bef5356301d1405d1a03c3f62efd8675db50c9..468fc6a4a526b1d55dce31e00b94de42ee71720d 100644 (file)
@@ -116,7 +116,8 @@ CREATE TABLE zone_templ (
   id           SERIAL PRIMARY KEY,
   name         varchar(128) NOT NULL,
   descr                text NOT NULL,
-  owner                integer default 0
+  owner                integer default 0,
+  FOREIGN KEY(owner) REFERENCES users(id)
 );
 CREATE UNIQUE INDEX zone_templ_name_index ON zone_templ(name);
 
index b0e0840edb9a167498fe104f742402e96a285d7c..330480cc8f74014001ce739f03593de4f4b7307f 100644 (file)
@@ -30,7 +30,7 @@ from .common import pp, to_bool
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.7.1'
+__version__ = '0.7.2'
 LOG = logging.getLogger(__name__)
 
 # =============================================================================
@@ -409,7 +409,9 @@ class ImportPdnsdataApp(PpConfigApplication):
             self.import_tsigkeys()
             if self.tgt_db_is_poweradmin:
                 self.import_perm_templ()
+                self.import_perm_templ_items()
                 self.import_users()
+                self.import_zone_templ()
         finally:
             self._close_all()
 
@@ -545,6 +547,21 @@ class ImportPdnsdataApp(PpConfigApplication):
                     if not self.simulate:
                         tgt_cursor.execute(sql)
 
+                LOG.debug("Get max. Zone Template Permission Id ...")
+                sql = "SELECT MAX(id) AS max_id FROM perm_templ_items"
+                if self.verbose > 1:
+                    LOG.debug("SQL: {}".format(sql))
+                tgt_cursor.execute(sql)
+                result = tgt_cursor.fetchone()
+                if self.verbose > 3:
+                    LOG.debug("Got max Zone Template Permission Id:\n{}".format(pp(result)))
+                max_id = int(result[0])
+                sql = "SELECT SETVAL('perm_templ_items_id_seq', %s)"
+                LOG.debug("Setting curval of perm_templ_items_id_seq to {} ...".format(max_id))
+                if self.verbose > 1:
+                    LOG.debug("SQL: {}".format(sql))
+                if not self.simulate:
+                    tgt_cursor.execute(sql, (max_id, ))
 
         LOG.debug("Commiting changes ...")
         self.tgt_connection.commit()
@@ -988,6 +1005,156 @@ class ImportPdnsdataApp(PpConfigApplication):
         LOG.debug("Commiting changes ...")
         self.tgt_connection.commit()
 
+    # -------------------------------------------------------------------------
+    def import_perm_templ_items(self):
+
+        LOG.info("Importing all permission template items ...")
+
+        map_sql = textwrap.dedent('SELECT id, name from perm_items')
+        if self.verbose > 1:
+            LOG.debug("Mapping SQL:\n{}".format(map_sql))
+
+        src_sql = textwrap.dedent('''\
+            SELECT templ_id, perm_id
+              FROM perm_templ_items
+             WHERE templ_id != 1
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL:\n{}".format(src_sql))
+
+        tgt_sql = textwrap.dedent('''\
+            INSERT INTO perm_templ_items (templ_id, perm_id)
+                   VALUES (%(templ_id)s, %(perm_id)s)
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
+
+        with self.tgt_connection.cursor() as tgt_cursor:
+            with self.src_connection.cursor() as src_cursor:
+
+                tplid_map = {}
+                tpl_names = {}
+
+                LOG.debug("Getting old permission item Ids ...")
+                src_cursor.execute(map_sql)
+                results = src_cursor.fetchall()
+                if self.verbose > 3:
+                    LOG.debug("Got old permission items:\n{}".format(pp(results)))
+                for result in results:
+                    item_id = int(result['id'])
+                    item_name = result['name'].lower()
+                    tpl_names[item_name] = item_id
+
+                LOG.debug("Getting new permission item Ids ...")
+                tgt_cursor.execute(map_sql)
+                results = tgt_cursor.fetchall()
+                if self.verbose > 3:
+                    LOG.debug("Got new permission items:\n{}".format(pp(results)))
+                for result in results:
+                    if self.tgt_db_type != 'mysql':
+                        item_id = int(result[0])
+                        item_name = result[1].lower()
+                    else:
+                        item_id = int(result['id'])
+                        item_name = result['name'].lower()
+                    if item_name in tpl_names:
+                        old_item_id = tpl_names[item_name]
+                        tplid_map[old_item_id] = item_id
+                del tpl_names
+                if self.verbose > 2:
+                    LOG.debug("Mapping old -> new template item Ids:\n{}".format(pp(tplid_map)))
+
+                i = 0
+                src_cursor.execute(src_sql)
+                results = src_cursor.fetchall()
+
+                if self.verbose > 3:
+                    LOG.debug("Got permission template items:\n{}".format(pp(results)))
+
+                if not results:
+                    LOG.info("No permission template items in source database.")
+                    LOG.debug("Commiting changes ...")
+                    self.tgt_connection.commit()
+                    return
+
+                for result in results:
+                    i += 1
+                    perm_id = int(result['perm_id'])
+                    if perm_id in tplid_map:
+                        result['perm_id'] = tplid_map[perm_id]
+                    if not self.simulate:
+                        tgt_cursor.execute(tgt_sql, result)
+                LOG.info("Imported {} permission template items.".format(i))
+
+        LOG.debug("Commiting changes ...")
+        self.tgt_connection.commit()
+
+    # -------------------------------------------------------------------------
+    def import_zone_templ(self):
+
+        LOG.info("Importing all zone templates ...")
+
+        src_sql = textwrap.dedent('''\
+            SELECT id, name, descr, owner
+              FROM zone_templ
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL:\n{}".format(src_sql))
+
+        tgt_sql = textwrap.dedent('''\
+            INSERT INTO zone_templ (
+                    id, name, descr, owner)
+                 VALUES (
+                    %(id)s, %(name)s, %(descr)s, %(owner)s)
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
+
+        with self.tgt_connection.cursor() as tgt_cursor:
+            with self.src_connection.cursor() as src_cursor:
+
+                i = 0
+                src_cursor.execute(src_sql)
+                results = src_cursor.fetchall()
+
+                if self.verbose > 3:
+                    LOG.debug("Got zone templates:\n{}".format(pp(results)))
+
+                if not results:
+                    LOG.info("No zone templates in source database.")
+                    LOG.debug("Commiting changes ...")
+                    self.tgt_connection.commit()
+                    return
+
+                for result in results:
+                    i += 1
+                    if not self.simulate:
+                        tgt_cursor.execute(tgt_sql, result)
+                LOG.info("Imported {} zone templates.".format(i))
+
+            if self.tgt_db_type != 'mysql':
+                LOG.debug("Get max. Zone Template Id ...")
+                sql = "SELECT MAX(id) AS max_id FROM zone_templ"
+                if self.verbose > 1:
+                    LOG.debug("SQL: {}".format(sql))
+                tgt_cursor.execute(sql)
+                result = tgt_cursor.fetchone()
+                if self.verbose > 3:
+                    LOG.debug("Got max user Id:\n{}".format(pp(result)))
+                if result[0] is None:
+                    max_id = 1
+                else:
+                    max_id = int(result[0])
+                sql = "SELECT SETVAL('zone_templ_id_seq', %s)"
+                LOG.debug("Setting curval of zone_templ_id_seq to {} ...".format(max_id))
+                if self.verbose > 1:
+                    LOG.debug("SQL: {}".format(sql))
+                if not self.simulate:
+                    tgt_cursor.execute(sql, (max_id, ))
+
+        LOG.debug("Commiting changes ...")
+        self.tgt_connection.commit()
+
     # -------------------------------------------------------------------------
     def import_users(self):
 
@@ -1012,7 +1179,6 @@ class ImportPdnsdataApp(PpConfigApplication):
         if self.verbose > 1:
             LOG.debug("Target SQL:\n{}".format(tgt_sql))
 
-
         with self.tgt_connection.cursor() as tgt_cursor:
             with self.src_connection.cursor() as src_cursor: