Refactor get_driver_version to display name while passing sanity tests

This commit is contained in:
Laurent Indermuehle 2023-01-20 19:40:50 +01:00
parent 7a0956fdcb
commit 5d9952c2e2
No known key found for this signature in database
GPG key ID: 93FA944C9F34DD09
2 changed files with 30 additions and 18 deletions

View file

@ -36,8 +36,15 @@ mysql_driver_fail_msg = ('A MySQL module is required: for Python 2.7 either PyMy
'the intended Python version.') '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): def get_driver_version(mysql_driver):
"""(class) -> str """ (class) -> str
Return the version of pymysql or mysqlclient (MySQLdb). Return the version of pymysql or mysqlclient (MySQLdb).
""" """
@ -57,12 +64,6 @@ def get_driver_version(mysql_driver):
return '.'.join(map(str, v)) 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): def parse_from_mysql_config_file(cnf):
# Default values of comment_prefix is '#' and ';'. # Default values of comment_prefix is '#' and ';'.
# '!' added to prevent a parsing error # '!' added to prevent a parsing error

View file

@ -206,12 +206,18 @@ slave_hosts:
type: dict type: dict
sample: sample:
- { "2": { "Host": "", "Master_id": 1, "Port": 3306 } } - { "2": { "Host": "", "Master_id": 1, "Port": 3306 } }
connector: connector_name:
description: The python connector used by the plugins description: The python connector name used by the plugins
returned: always returned: always
type: dict type: str
sample: 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 from decimal import Decimal
@ -222,7 +228,8 @@ from ansible_collections.community.mysql.plugins.module_utils.mysql import (
mysql_common_argument_spec, mysql_common_argument_spec,
mysql_driver, mysql_driver,
mysql_driver_fail_msg, mysql_driver_fail_msg,
mysql_driver_info, get_driver_name,
get_driver_version,
) )
from ansible.module_utils.six import iteritems from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
@ -565,16 +572,19 @@ def main():
if mysql_driver is None: if mysql_driver is None:
module.fail_json(msg=mysql_driver_fail_msg) module.fail_json(msg=mysql_driver_fail_msg)
driver_name = get_driver_name(mysql_driver)
driver_version = get_driver_version(mysql_driver)
try: try:
cursor, db_conn = mysql_connect(module, login_user, login_password, cursor, db_conn = mysql_connect(module, login_user, login_password,
config_file, ssl_cert, ssl_key, ssl_ca, db, config_file, ssl_cert, ssl_key, ssl_ca, db,
check_hostname=check_hostname, check_hostname=check_hostname,
connect_timeout=connect_timeout, cursor_class='DictCursor') connect_timeout=connect_timeout, cursor_class='DictCursor')
except Exception as e: except Exception as e:
module.fail_json(msg=f"unable to connect to database using \ msg = ('unable to connect to database using %s %s, check login_user '
{mysql_driver_info.get('name')} {mysql_driver_info.get('version')}, \ 'and login_password are correct or %s has the credentials. '
check login_user and login_password are correct or {config_file} has \ 'Exception message: %s' % (driver_name, driver_version, config_file, to_native(e)))
the credentials. Exception message: {to_native(e)}") module.fail_json(msg)
############################### ###############################
# Create object and do main job # Create object and do main job
@ -582,8 +592,9 @@ def main():
mysql = MySQL_Info(module, cursor) mysql = MySQL_Info(module, cursor)
module.exit_json(changed=False, module.exit_json(changed=False,
**mysql.get_info(filter_, exclude_fields, return_empty_dbs), connector_name=driver_name,
connector=mysql_driver_info) connector_version=driver_version,
**mysql.get_info(filter_, exclude_fields, return_empty_dbs))
if __name__ == '__main__': if __name__ == '__main__':