From 95c649cce2398f351060b673470c04ea18b28af1 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Mon, 14 Mar 2022 16:41:38 +0300 Subject: [PATCH] Fix the collection to work with mysqlclient connector (#301) --- .github/workflows/ansible-test-plugins.yml | 4 ---- changelogs/fragments/0-mysqlclient.yml | 2 ++ plugins/module_utils/user.py | 8 ++++++-- plugins/modules/mysql_role.py | 2 +- tests/unit/plugins/modules/test_mysql_role.py | 4 ++-- 5 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/0-mysqlclient.yml diff --git a/.github/workflows/ansible-test-plugins.yml b/.github/workflows/ansible-test-plugins.yml index a82a624..c492261 100644 --- a/.github/workflows/ansible-test-plugins.yml +++ b/.github/workflows/ansible-test-plugins.yml @@ -74,10 +74,6 @@ jobs: exclude: - mysql: 8.0.22 connector: pymysql==0.7.10 - - db_engine_version: mariadb_10.3.34 - connector: mysqlclient==2.0.1 - - db_engine_version: mariadb_10.5.9 - connector: mysqlclient==2.0.1 - python: 3.8 ansible: stable-2.9 - python: 3.8 diff --git a/changelogs/fragments/0-mysqlclient.yml b/changelogs/fragments/0-mysqlclient.yml new file mode 100644 index 0000000..8c058d1 --- /dev/null +++ b/changelogs/fragments/0-mysqlclient.yml @@ -0,0 +1,2 @@ +bugfixes: +- Collection core functions - fixes related to the mysqlclient Python connector (https://github.com/ansible-collections/community.mysql/issues/292). diff --git a/plugins/module_utils/user.py b/plugins/module_utils/user.py index 0532de9..6dac57f 100644 --- a/plugins/module_utils/user.py +++ b/plugins/module_utils/user.py @@ -449,7 +449,7 @@ def privileges_get(cursor, user, host, maria_role=False): if not maria_role: cursor.execute("SHOW GRANTS FOR %s@%s", (user, host)) else: - cursor.execute("SHOW GRANTS FOR %s", (user)) + cursor.execute("SHOW GRANTS FOR %s", (user,)) grants = cursor.fetchall() def pick(x): @@ -673,7 +673,7 @@ def privileges_revoke(cursor, user, host, db_table, priv, grant_option, maria_ro params = (user, host) else: query.append("FROM %s") - params = (user) + params = (user,) query = ' '.join(query) cursor.execute(query, params) @@ -699,6 +699,10 @@ def privileges_grant(cursor, user, host, db_table, priv, tls_requires, maria_rol if 'GRANT' in priv: query.append("WITH GRANT OPTION") query = ' '.join(query) + + if isinstance(params, str): + params = (params,) + cursor.execute(query, params) diff --git a/plugins/modules/mysql_role.py b/plugins/modules/mysql_role.py index cd24c27..367692f 100644 --- a/plugins/modules/mysql_role.py +++ b/plugins/modules/mysql_role.py @@ -482,7 +482,7 @@ class MariaDBQueryBuilder(): Returns: tuple: (query_string, tuple_containing_parameters). """ - return "SELECT count(*) FROM mysql.user WHERE user = %s AND is_role = 'Y'", (self.name) + return "SELECT count(*) FROM mysql.user WHERE user = %s AND is_role = 'Y'", (self.name,) def role_grant(self, user): """Return a query to grant a role to a user or role. diff --git a/tests/unit/plugins/modules/test_mysql_role.py b/tests/unit/plugins/modules/test_mysql_role.py index 2ed5a2c..3c24719 100644 --- a/tests/unit/plugins/modules/test_mysql_role.py +++ b/tests/unit/plugins/modules/test_mysql_role.py @@ -28,9 +28,9 @@ module = Module() @pytest.mark.parametrize( 'builder,output', [ - (MariaDBQueryBuilder('role0'), ("SELECT count(*) FROM mysql.user WHERE user = %s AND is_role = 'Y'", ('role0'))), + (MariaDBQueryBuilder('role0'), ("SELECT count(*) FROM mysql.user WHERE user = %s AND is_role = 'Y'", ('role0',))), (MySQLQueryBuilder('role0', '%'), ('SELECT count(*) FROM mysql.user WHERE user = %s AND host = %s', ('role0', '%'))), - (MariaDBQueryBuilder('role1'), ("SELECT count(*) FROM mysql.user WHERE user = %s AND is_role = 'Y'", ('role1'))), + (MariaDBQueryBuilder('role1'), ("SELECT count(*) FROM mysql.user WHERE user = %s AND is_role = 'Y'", ('role1',))), (MySQLQueryBuilder('role1', 'fake'), ('SELECT count(*) FROM mysql.user WHERE user = %s AND host = %s', ('role1', 'fake'))), ] )