Merge branch 'main' into lie_tests_using_containers

This commit is contained in:
Laurent Indermuehle 2023-01-31 17:39:58 +01:00
commit 8658f191d9
No known key found for this signature in database
GPG key ID: 93FA944C9F34DD09
6 changed files with 69 additions and 32 deletions

View file

@ -1,5 +1,5 @@
# MySQL collection for Ansible # MySQL collection for Ansible
[![Plugins CI](https://github.com/ansible-collections/community.mysql/workflows/Plugins%20CI/badge.svg?event=push)](https://github.com/ansible-collections/community.mysql/actions?query=workflow%3A"Plugins+CI") [![Roles CI](https://github.com/ansible-collections/community.mysql/workflows/Roles%20CI/badge.svg?event=push)](https://github.com/ansible-collections/community.mysql/actions?query=workflow%3A"Roles+CI") [![Codecov](https://img.shields.io/codecov/c/github/ansible-collections/community.mysql)](https://codecov.io/gh/ansible-collections/community.mysql) ![](https://img.shields.io/matrix/mysql:ansible.com.svg?server_fqdn=ansible-accounts.ems.host&label=Discuss%20on%20Matrix%20%23mysql:ansible.com&logo=matrix) [![Plugins CI](https://github.com/ansible-collections/community.mysql/workflows/Plugins%20CI/badge.svg?event=push)](https://github.com/ansible-collections/community.mysql/actions?query=workflow%3A"Plugins+CI") [![Roles CI](https://github.com/ansible-collections/community.mysql/workflows/Roles%20CI/badge.svg?event=push)](https://github.com/ansible-collections/community.mysql/actions?query=workflow%3A"Roles+CI") [![Codecov](https://img.shields.io/codecov/c/github/ansible-collections/community.mysql)](https://codecov.io/gh/ansible-collections/community.mysql) [![Discuss on Matrix at #mysql:ansible.com](https://img.shields.io/matrix/mysql:ansible.com.svg?server_fqdn=ansible-accounts.ems.host&label=Discuss%20on%20Matrix%20at%20%23mysql:ansible.com&logo=matrix)](https://matrix.to/#/#mysql:ansible.com)
This collection is a part of the Ansible package. This collection is a part of the Ansible package.

View file

@ -0,0 +1,3 @@
---
minor_changes:
- mysql_info - add ``connector_name`` and ``connector_version`` to returned values (https://github.com/ansible-collections/community.mysql/pull/497).

View file

@ -38,46 +38,41 @@ mysql_driver_fail_msg = ('A MySQL module is required: for Python 2.7 either PyMy
from ansible_collections.community.mysql.plugins.module_utils.database import mysql_quote_identifier from ansible_collections.community.mysql.plugins.module_utils.database import mysql_quote_identifier
def get_driver_name(mysql_driver): def get_connector_name(connector):
""" (class) -> str """ (class) -> str
Return the name of the driver (pymysql or mysqlclient (MySQLdb)) Return the name of the connector (pymysql or mysqlclient (MySQLdb))
or 'Unknown' if the driver name is not pymysql or MySQLdb. When adding a or 'Unknown' if not pymysql or MySQLdb. When adding a
connector here, also modify get_driver_version. connector here, also modify get_connector_version.
""" """
if mysql_driver is None or not hasattr(mysql_driver, '__name__'): if connector is None or not hasattr(connector, '__name__'):
return 'Unknown' return 'Unknown'
if mysql_driver.__name__ not in ['pymysql', 'MySQLdb']: return connector.__name__
return 'Unknown'
return mysql_driver.__name__
def get_driver_version(mysql_driver): def get_connector_version(connector):
""" (class) -> str """ (class) -> str
Return the version of pymysql or mysqlclient (MySQLdb). Return the version of pymysql or mysqlclient (MySQLdb).
If the driver name is unknown, this method also return 'Unknown' Return 'Unknown' if the connector name is unknown.
""" """
if mysql_driver is None: if connector is None:
return 'Unknown' return 'Unknown'
driver_name = get_driver_name(mysql_driver) connector_name = get_connector_name(connector)
if driver_name == 'Unknown': if connector_name == 'pymysql':
return 'Unknown'
if driver_name == 'pymysql':
# pymysql has two methods: # pymysql has two methods:
# - __version__ that returns the string: 0.7.11.None # - __version__ that returns the string: 0.7.11.None
# - VERSION that returns the tupple (0, 7, 11, None) # - VERSION that returns the tuple (0, 7, 11, None)
v = mysql_driver.VERSION[:3] v = connector.VERSION[:3]
return '.'.join(map(str, v)) return '.'.join(map(str, v))
elif connector_name == 'MySQLdb':
if driver_name == 'MySQLdb':
# version_info returns the tuple (2, 1, 1, 'final', 0) # version_info returns the tuple (2, 1, 1, 'final', 0)
v = mysql_driver.version_info[:3] v = connector.version_info[:3]
return '.'.join(map(str, v)) return '.'.join(map(str, v))
else:
return 'Unknown'
def parse_from_mysql_config_file(cnf): def parse_from_mysql_config_file(cnf):

View file

@ -58,6 +58,7 @@ seealso:
author: author:
- Andrew Klychkov (@Andersson007) - Andrew Klychkov (@Andersson007)
- Sebastian Gumprich (@rndmh3ro) - Sebastian Gumprich (@rndmh3ro)
- Laurent Indermühle (@laurent-indermuehle)
extends_documentation_fragment: extends_documentation_fragment:
- community.mysql.mysql - community.mysql.mysql
@ -207,18 +208,20 @@ slave_hosts:
sample: sample:
- { "2": { "Host": "", "Master_id": 1, "Port": 3306 } } - { "2": { "Host": "", "Master_id": 1, "Port": 3306 } }
connector_name: connector_name:
description: Name of the python connector used by the plugin. When the driver is not identified, returns C(Unknown). description: Name of the python connector used by the module. When the connector is not identified, returns C(Unknown).
returned: always returned: always
type: str type: str
sample: sample:
- "pymysql" - "pymysql"
- "MySQLdb" - "MySQLdb"
version_added: '3.6.0'
connector_version: connector_version:
description: Version of the python connector used by the plugin. When the driver is not identified, returns C(Unknown). description: Version of the python connector used by the module. When the connector is not identified, returns C(Unknown).
returned: always returned: always
type: str type: str
sample: sample:
- "1.0.2" - "1.0.2"
version_added: '3.6.0'
''' '''
from decimal import Decimal from decimal import Decimal
@ -229,8 +232,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,
get_driver_name, get_connector_name,
get_driver_version, get_connector_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
@ -573,8 +576,8 @@ 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) connector_name = get_connector_name(mysql_driver)
driver_version = get_driver_version(mysql_driver) connector_version = get_connector_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,
@ -584,7 +587,7 @@ def main():
except Exception as e: except Exception as e:
msg = ('unable to connect to database using %s %s, check login_user ' msg = ('unable to connect to database using %s %s, check login_user '
'and login_password are correct or %s has the credentials. ' 'and login_password are correct or %s has the credentials. '
'Exception message: %s' % (driver_name, driver_version, config_file, to_native(e))) 'Exception message: %s' % (connector_name, connector_version, config_file, to_native(e)))
module.fail_json(msg) module.fail_json(msg)
############################### ###############################
@ -593,8 +596,8 @@ def main():
mysql = MySQL_Info(module, cursor) mysql = MySQL_Info(module, cursor)
module.exit_json(changed=False, module.exit_json(changed=False,
connector_name=driver_name, connector_name=connector_name,
connector_version=driver_version, connector_version=connector_version,
**mysql.get_info(filter_, exclude_fields, return_empty_dbs)) **mysql.get_info(filter_, exclude_fields, return_empty_dbs))

View file

@ -0,0 +1,32 @@
---
# Added in 3.6.0 in
# https://github.com/ansible-collections/community.mysql/pull/497
# TODO: Refactor in PR490.
- name: Connector info | Assert connector_name exists and has expected values
ansible.builtin.assert:
that:
- result.connector_name is defined
- result.connector_name is in ['pymysql', 'MySQLdb']
success_msg: >-
Assertions passed, result.connector_name is {{ result.connector_name }}
fail_msg: >-
Assertion failed, result.connector_name is
{{ result.connector_name | d('Unknown')}} which is different than expected
pymysql or MySQLdb
# TODO: Refactor in PR490.
- name: Connector info | Assert connector_version exists and has expected values
ansible.builtin.assert:
that:
- result.connector_version is defined
- >
result.connector_version == 'Unknown'
or result.connector_version is version(connector_ver, '==')
success_msg: >-
Assertions passed, result.connector_version is
{{ result.connector_version }}
fail_msg: >-
Assertion failed, result.connector_version is
{{ result.connector_version }} which is different than expected
{{ connector_ver }}

View file

@ -57,6 +57,10 @@
- result.engines != {} - result.engines != {}
- result.users != {} - result.users != {}
- name: mysql_info - Test connector informations display
ansible.builtin.import_tasks:
file: connector_info.yml
# Access by non-default cred file # Access by non-default cred file
- name: mysql_info - check non-default cred file - name: mysql_info - check non-default cred file
mysql_info: mysql_info: