From 9201995ee9bc0c4a9c2f76d347d584bdaaef53d4 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 6 Feb 2018 15:25:57 +0100 Subject: [PATCH] Decreasing complexity of method import_domains() --- pp_lib/import_pdnsdata.py | 112 ++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 53 deletions(-) diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index f0e7c9e..9294768 100644 --- a/pp_lib/import_pdnsdata.py +++ b/pp_lib/import_pdnsdata.py @@ -31,7 +31,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication from .pdns_record import PdnsSoaData -__version__ = '0.10.1' +__version__ = '0.10.2' LOG = logging.getLogger(__name__) # ============================================================================= @@ -60,6 +60,17 @@ class ImportPdnsdataApp(PpConfigApplication): re_is_local_account = re.compile(r'(lo[ck]al|privat|intern)', re.IGNORECASE) + sql_insert_domain = textwrap.dedent('''\ + INSERT INTO domains (id, name, master, last_check, type, notified_serial, account) + VALUES (%(id)s, %(name)s, %(master)s, %(last_check)s, + %(type)s, %(notified_serial)s, %(account)s) + ''').strip() + + sql_insert_dom_meta = textwrap.dedent('''\ + INSERT INTO domainmetadata (domain_id, kind, content) + VALUES (%(domain_id)s, %(kind)s, %(content)s) + ''').strip() + # ------------------------------------------------------------------------- def __init__(self, appname=None, version=__version__): @@ -530,6 +541,48 @@ class ImportPdnsdataApp(PpConfigApplication): LOG.debug("Commiting changes ...") self.tgt_connection.commit() + # ------------------------------------------------------------------------- + def _import_domain(self, dom_data, tgt_cursor): + + dom_id = dom_data['id'] + dom_name = dom_data['name'] + self.domain_ids[dom_id] = dom_name + + if self.is_local_domain(dom_name): + LOG.debug("Setting zone {!r} to a local only zone.".format(dom_name)) + cur_account = dom_data['account'] + if cur_account is None: + cur_account = '' + else: + cur_account = cur_account.strip() + if not self.re_is_local_account.search(cur_account): + if cur_account == '': + cur_account = 'local' + else: + cur_account += ', local' + if self.verbose > 1: + LOG.debug( + "Setting account information of zone {!r} to {!r}.".format( + dom_name, cur_account)) + dom_data['account'] = cur_account + if self.verbose > 1: + LOG.debug("SQL for insert domain:\n{}".format( + to_str(tgt_cursor.mogrify(self.sql_insert_domain, dom_data)))) + if not self.simulate: + tgt_cursor.execute(self.sql_insert_domain, dom_data) + + # Inserting domain metadata for SOA-EDIT-API + params = { + 'domain_id': dom_id, + 'kind': 'SOA-EDIT-API', + 'content': 'INCEPTION-INCREMENT', + } + if self.verbose > 1: + LOG.debug("SQL for insert domain metadata:\n{}".format( + to_str(tgt_cursor.mogrify(self.sql_insert_dom_meta, params)))) + if not self.simulate: + tgt_cursor.execute(self.sql_insert_dom_meta, params) + # ------------------------------------------------------------------------- def import_domains(self): @@ -545,21 +598,6 @@ class ImportPdnsdataApp(PpConfigApplication): if self.verbose > 1: LOG.debug("Source SQL:\n{}".format(src_sql)) - tgt_sql = textwrap.dedent('''\ - INSERT INTO domains (id, name, master, last_check, type, notified_serial, account) - VALUES (%(id)s, %(name)s, %(master)s, %(last_check)s, - %(type)s, %(notified_serial)s, %(account)s) - ''').strip() - if self.verbose > 1: - LOG.debug("Target SQL:\n{}".format(tgt_sql)) - - tgt_sql_metadata = textwrap.dedent('''\ - INSERT INTO domainmetadata (domain_id, kind, content) - VALUES (%(domain_id)s, %(kind)s, %(content)s) - ''').strip() - if self.verbose > 1: - LOG.debug("Target SQL for domain metadata:\n{}".format(tgt_sql)) - with self.tgt_connection.cursor() as tgt_cursor: with self.src_connection.cursor() as src_cursor: @@ -572,46 +610,12 @@ class ImportPdnsdataApp(PpConfigApplication): for result in results: i += 1 - dom_id = result['id'] - self.domain_ids[dom_id] = result['name'] - if self.is_local_domain(result['name']): - LOG.debug("Setting zone {!r} to a local only zone.".format(result['name'])) - cur_account = result['account'] - if cur_account is None: - cur_account = '' - else: - cur_account = cur_account.strip() - if not self.re_is_local_account.search(cur_account): - if cur_account == '': - cur_account = 'local' - else: - cur_account += ', local' - if self.verbose > 1: - LOG.debug( - "Setting account information of zone {!r} to {!r}.".format( - result['name'], cur_account)) - result['account'] = cur_account - if self.verbose > 1: - LOG.debug("SQL for insert domain:\n{}".format( - to_str(tgt_cursor.mogrify(tgt_sql, result)))) - if not self.simulate: - tgt_cursor.execute(tgt_sql, result) - - # Inserting domain metadata for SOA-EDIT-API - params = { - 'domain_id': dom_id, - 'kind': 'SOA-EDIT-API', - 'content': 'INCEPTION-INCREMENT', - } - if self.verbose > 1: - LOG.debug("SQL for insert domain metadata:\n{}".format( - to_str(tgt_cursor.mogrify(tgt_sql_metadata, params)))) - if not self.simulate: - tgt_cursor.execute(tgt_sql_metadata, params) + self._import_domain(result, tgt_cursor) LOG.info("Imported {} domains.".format(i)) if self.tgt_db_type != 'mysql': + # Get current max domain Id LOG.debug("Get max. Domain Id ...") sql = "SELECT MAX(id) AS max_id FROM domains" if self.verbose > 1: @@ -621,10 +625,12 @@ class ImportPdnsdataApp(PpConfigApplication): if self.verbose > 2: LOG.debug("Got max domain Id:\n{}".format(pp(result))) max_id = int(result[0]) + + # Setting this as new value for sequence sql = "SELECT SETVAL('domains_id_seq', %s)" LOG.debug("Setting curval of domains_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, )) -- 2.39.5