From df5ede0077c40da0f850410fd6a0f9fe14dade1f Mon Sep 17 00:00:00 2001 From: Markus Bergholz Date: Thu, 22 Jul 2021 15:08:19 +0200 Subject: [PATCH] Missing required lib (#50) * use missing_required_lib * remove another import * update docs (drop MySQL-Python, add mysqlclient) * fix var name * remove MySQL-python from error message and replace with mysqlclient * add changelog fragment * pymysql or MySQLdb * use library * Update changelogs/fragments/50-refactor-connectors.yaml Co-authored-by: Andrew Klychkov Co-authored-by: Andrew Klychkov --- changelogs/fragments/50-refactor-connectors.yaml | 3 +++ plugins/doc_fragments/proxysql.py | 4 ++-- plugins/module_utils/mysql.py | 11 +++++++++-- plugins/modules/proxysql_backend_servers.py | 5 +---- plugins/modules/proxysql_global_variables.py | 5 +---- plugins/modules/proxysql_manage_config.py | 5 +---- plugins/modules/proxysql_mysql_users.py | 5 +---- plugins/modules/proxysql_query_rules.py | 5 +---- plugins/modules/proxysql_query_rules_fast_routing.py | 5 +---- plugins/modules/proxysql_replication_hostgroups.py | 5 +---- plugins/modules/proxysql_scheduler.py | 5 +---- 11 files changed, 22 insertions(+), 36 deletions(-) create mode 100644 changelogs/fragments/50-refactor-connectors.yaml diff --git a/changelogs/fragments/50-refactor-connectors.yaml b/changelogs/fragments/50-refactor-connectors.yaml new file mode 100644 index 0000000..e76cd97 --- /dev/null +++ b/changelogs/fragments/50-refactor-connectors.yaml @@ -0,0 +1,3 @@ +minor_changes: + - Refactoring of connector presence checking (https://github.com/ansible-collections/community.proxysql/pull/50). + - Replace MySQL-Python with mysqlclient in the import error message (https://github.com/ansible-collections/community.proxysql/pull/50). diff --git a/plugins/doc_fragments/proxysql.py b/plugins/doc_fragments/proxysql.py index ae38b94..703d9d0 100644 --- a/plugins/doc_fragments/proxysql.py +++ b/plugins/doc_fragments/proxysql.py @@ -41,8 +41,8 @@ class ModuleDocFragment(object): type: path default: '' requirements: - - PyMySQL (Python 2.7 and Python 3.X), or - - MySQLdb (Python 2.x) + - PyMySQL + - mysqlclient ''' # Documentation fragment for managing ProxySQL configuration diff --git a/plugins/module_utils/mysql.py b/plugins/module_utils/mysql.py index b5beb02..193c036 100644 --- a/plugins/module_utils/mysql.py +++ b/plugins/module_utils/mysql.py @@ -15,20 +15,24 @@ import os from ansible.module_utils.six.moves import configparser +from ansible.module_utils.basic import missing_required_lib +MYSQL_IMP_ERR = None try: import pymysql as mysql_driver _mysql_cursor_param = 'cursor' + HAS_MYSQL_PACKAGE = True except ImportError: try: import MySQLdb as mysql_driver import MySQLdb.cursors _mysql_cursor_param = 'cursorclass' + HAS_MYSQL_PACKAGE = True except ImportError: + MYSQL_IMP_ERR = 'Cannot find PyMySQL or mysqlclient library.' + HAS_MYSQL_PACKAGE = False mysql_driver = None -mysql_driver_fail_msg = 'The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required.' - def parse_from_mysql_config_file(cnf): cp = configparser.ConfigParser() @@ -41,6 +45,9 @@ def mysql_connect(module, login_user=None, login_password=None, config_file='', connect_timeout=30, autocommit=False, config_overrides_defaults=False): config = {} + if not HAS_MYSQL_PACKAGE: + module.fail_json(msg=missing_required_lib("pymysql or MySQLdb"), exception=MYSQL_IMP_ERR) + if config_file and os.path.exists(config_file): config['read_default_file'] = config_file cp = parse_from_mysql_config_file(config_file) diff --git a/plugins/modules/proxysql_backend_servers.py b/plugins/modules/proxysql_backend_servers.py index 1265dcd..40c79cc 100644 --- a/plugins/modules/proxysql_backend_servers.py +++ b/plugins/modules/proxysql_backend_servers.py @@ -159,7 +159,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -195,9 +195,6 @@ def perform_checks(module): msg="max_replication_lag must be set between 0 and 102400" ) - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def save_config_to_disk(cursor): cursor.execute("SAVE MYSQL SERVERS TO DISK") diff --git a/plugins/modules/proxysql_global_variables.py b/plugins/modules/proxysql_global_variables.py index 2514a42..c68a1bf 100644 --- a/plugins/modules/proxysql_global_variables.py +++ b/plugins/modules/proxysql_global_variables.py @@ -73,7 +73,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils._text import to_native # =========================================== @@ -88,9 +88,6 @@ def perform_checks(module): msg="login_port must be a valid unix port number (0-65535)" ) - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def save_config_to_disk(variable, cursor): if variable.startswith("admin"): diff --git a/plugins/modules/proxysql_manage_config.py b/plugins/modules/proxysql_manage_config.py index a1a122c..2b3f165 100644 --- a/plugins/modules/proxysql_manage_config.py +++ b/plugins/modules/proxysql_manage_config.py @@ -98,7 +98,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils._text import to_native # =========================================== @@ -135,9 +135,6 @@ def perform_checks(module): " with the CONFIG config_layer") module.fail_json(msg=msg_string % module.params["direction"]) - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def manage_config(manage_config_settings, cursor): diff --git a/plugins/modules/proxysql_mysql_users.py b/plugins/modules/proxysql_mysql_users.py index 927052a..c2534ef 100644 --- a/plugins/modules/proxysql_mysql_users.py +++ b/plugins/modules/proxysql_mysql_users.py @@ -160,7 +160,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native, to_bytes from hashlib import sha1 @@ -177,9 +177,6 @@ def perform_checks(module): msg="login_port must be a valid unix port number (0-65535)" ) - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def save_config_to_disk(cursor): cursor.execute("SAVE MYSQL USERS TO DISK") diff --git a/plugins/modules/proxysql_query_rules.py b/plugins/modules/proxysql_query_rules.py index fe8c147..b2b4901 100644 --- a/plugins/modules/proxysql_query_rules.py +++ b/plugins/modules/proxysql_query_rules.py @@ -312,7 +312,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -328,9 +328,6 @@ def perform_checks(module): msg="login_port must be a valid unix port number (0-65535)" ) - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def save_config_to_disk(cursor): cursor.execute("SAVE MYSQL QUERY RULES TO DISK") diff --git a/plugins/modules/proxysql_query_rules_fast_routing.py b/plugins/modules/proxysql_query_rules_fast_routing.py index 716f4b6..2c22762 100644 --- a/plugins/modules/proxysql_query_rules_fast_routing.py +++ b/plugins/modules/proxysql_query_rules_fast_routing.py @@ -106,7 +106,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -119,9 +119,6 @@ def perform_checks(module): if module.params["login_port"] < 0 or module.params["login_port"] > 65535: module.fail_json(msg="login_port must be a valid unix port number (0-65535)") - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def save_config_to_disk(cursor): cursor.execute("SAVE MYSQL QUERY RULES TO DISK") diff --git a/plugins/modules/proxysql_replication_hostgroups.py b/plugins/modules/proxysql_replication_hostgroups.py index bdd412c..afb05c6 100644 --- a/plugins/modules/proxysql_replication_hostgroups.py +++ b/plugins/modules/proxysql_replication_hostgroups.py @@ -97,7 +97,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils._text import to_native # =========================================== @@ -129,9 +129,6 @@ def perform_checks(module): msg="reader_hostgroup cannot equal writer_hostgroup" ) - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def save_config_to_disk(cursor): cursor.execute("SAVE MYSQL SERVERS TO DISK") diff --git a/plugins/modules/proxysql_scheduler.py b/plugins/modules/proxysql_scheduler.py index 1ee380d..c6b5fd7 100644 --- a/plugins/modules/proxysql_scheduler.py +++ b/plugins/modules/proxysql_scheduler.py @@ -135,7 +135,7 @@ ''' from ansible.module_utils.basic import AnsibleModule -from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg +from ansible_collections.community.proxysql.plugins.module_utils.mysql import mysql_connect, mysql_driver from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -157,9 +157,6 @@ def perform_checks(module): msg="interval_ms must between 100ms & 100000000ms" ) - if mysql_driver is None: - module.fail_json(msg=mysql_driver_fail_msg) - def save_config_to_disk(cursor): cursor.execute("SAVE SCHEDULER TO DISK")