From 5d9952c2e224ea156755f83c959d52054f616c48 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Fri, 20 Jan 2023 19:40:50 +0100 Subject: [PATCH] Refactor get_driver_version to display name while passing sanity tests --- plugins/module_utils/mysql.py | 15 ++++++++------- plugins/modules/mysql_info.py | 33 ++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/plugins/module_utils/mysql.py b/plugins/module_utils/mysql.py index 39dd9f7..16db517 100644 --- a/plugins/module_utils/mysql.py +++ b/plugins/module_utils/mysql.py @@ -36,8 +36,15 @@ mysql_driver_fail_msg = ('A MySQL module is required: for Python 2.7 either PyMy 'the intended Python version.') +def get_driver_name(mysql_driver): + """ (class) -> str + Return the name of the driver (pymysql or mysqlclient (MySQLdb)). + """ + return mysql_driver.__name__ + + def get_driver_version(mysql_driver): - """(class) -> str + """ (class) -> str Return the version of pymysql or mysqlclient (MySQLdb). """ @@ -57,12 +64,6 @@ def get_driver_version(mysql_driver): return '.'.join(map(str, v)) -mysql_driver_info = { - "name": mysql_driver.__name__, - "version": get_driver_version(mysql_driver) -} - - def parse_from_mysql_config_file(cnf): # Default values of comment_prefix is '#' and ';'. # '!' added to prevent a parsing error diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index d9c2b2f..7978cb3 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -206,12 +206,18 @@ slave_hosts: type: dict sample: - { "2": { "Host": "", "Master_id": 1, "Port": 3306 } } -connector: - description: The python connector used by the plugins +connector_name: + description: The python connector name used by the plugins returned: always - type: dict + type: str sample: - - { "connector: { "name": "pymysql", "version": "1.0.2" } } + - "pymysql" +connector_version: + description: The python connector version used by the plugins + returned: always + type: str + sample: + - "1.0.2" ''' from decimal import Decimal @@ -222,7 +228,8 @@ from ansible_collections.community.mysql.plugins.module_utils.mysql import ( mysql_common_argument_spec, mysql_driver, mysql_driver_fail_msg, - mysql_driver_info, + get_driver_name, + get_driver_version, ) from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -565,16 +572,19 @@ def main(): if mysql_driver is None: module.fail_json(msg=mysql_driver_fail_msg) + driver_name = get_driver_name(mysql_driver) + driver_version = get_driver_version(mysql_driver) + try: cursor, db_conn = mysql_connect(module, login_user, login_password, config_file, ssl_cert, ssl_key, ssl_ca, db, check_hostname=check_hostname, connect_timeout=connect_timeout, cursor_class='DictCursor') except Exception as e: - module.fail_json(msg=f"unable to connect to database using \ - {mysql_driver_info.get('name')} {mysql_driver_info.get('version')}, \ - check login_user and login_password are correct or {config_file} has \ - the credentials. Exception message: {to_native(e)}") + msg = ('unable to connect to database using %s %s, check login_user ' + 'and login_password are correct or %s has the credentials. ' + 'Exception message: %s' % (driver_name, driver_version, config_file, to_native(e))) + module.fail_json(msg) ############################### # Create object and do main job @@ -582,8 +592,9 @@ def main(): mysql = MySQL_Info(module, cursor) module.exit_json(changed=False, - **mysql.get_info(filter_, exclude_fields, return_empty_dbs), - connector=mysql_driver_info) + connector_name=driver_name, + connector_version=driver_version, + **mysql.get_info(filter_, exclude_fields, return_empty_dbs)) if __name__ == '__main__':