Skip to content

Commit 852c19a

Browse files
wfelipewlaurent-indermuehleAndersson007
authored
Using show all slaves status when using MariaDB to be consistent with MySQL (#602)
* Using `show all slaves status` whe using MariaDB to be consistent with the MySQL behaviour. * Fixing lint issues * Fix issue by using dict attribute * Fix unit tests * fix lint test * Add unit tests * Fix unit tests * Adding changlog fragment * Update changelogs/fragments/602-show-all-slaves-status.yaml Co-authored-by: Laurent Indermühle <[email protected]> * Refactoring change by moving common logic to the module_utils * Fix sanity checks * Fix sanity checks * Adding lines to fix sanity checks * Fixing sanity checks * Update changelogs/fragments/602-show-all-slaves-status.yaml Co-authored-by: Andrew Klychkov <[email protected]> * Removing is_mariadb and is_mysql functions --------- Co-authored-by: Laurent Indermühle <[email protected]> Co-authored-by: Andrew Klychkov <[email protected]>
1 parent 051aa48 commit 852c19a

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- mysql_info - the ``slave_status`` filter was returning an empty list on MariaDB with multiple replication channels. It now returns all channels by running ``SHOW ALL SLAVES STATUS`` for MariaDB servers (https://github.com/ansible-collections/community.mysql/issues/603).

plugins/module_utils/mysql.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ def get_server_version(cursor):
207207
return version_str
208208

209209

210+
def get_server_implementation(cursor):
211+
if 'mariadb' in get_server_version(cursor).lower():
212+
return "mariadb"
213+
else:
214+
return "mysql"
215+
216+
210217
def set_session_vars(module, cursor, session_vars):
211218
"""Set session vars."""
212219
for var, value in session_vars.items():

plugins/modules/mysql_info.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
66

77
from __future__ import absolute_import, division, print_function
8+
89
__metaclass__ = type
910

1011
DOCUMENTATION = r'''
@@ -292,6 +293,7 @@
292293
mysql_driver_fail_msg,
293294
get_connector_name,
294295
get_connector_version,
296+
get_server_implementation,
295297
)
296298

297299
from ansible_collections.community.mysql.plugins.module_utils.user import (
@@ -325,9 +327,10 @@ class MySQL_Info(object):
325327
5. add info about the new subset with an example to RETURN block
326328
"""
327329

328-
def __init__(self, module, cursor):
330+
def __init__(self, module, cursor, server_implementation):
329331
self.module = module
330332
self.cursor = cursor
333+
self.server_implementation = server_implementation
331334
self.info = {
332335
'version': {},
333336
'databases': {},
@@ -497,7 +500,10 @@ def __get_master_status(self):
497500

498501
def __get_slave_status(self):
499502
"""Get slave status if the instance is a slave."""
500-
res = self.__exec_sql('SHOW SLAVE STATUS')
503+
if self.server_implementation == "mariadb":
504+
res = self.__exec_sql('SHOW ALL SLAVES STATUS')
505+
else:
506+
res = self.__exec_sql('SHOW SLAVE STATUS')
501507
if res:
502508
for line in res:
503509
host = line['Master_Host']
@@ -738,10 +744,12 @@ def main():
738744
'Exception message: %s' % (connector_name, connector_version, config_file, to_native(e)))
739745
module.fail_json(msg)
740746

747+
server_implementation = get_server_implementation(cursor)
748+
741749
###############################
742750
# Create object and do main job
743751

744-
mysql = MySQL_Info(module, cursor)
752+
mysql = MySQL_Info(module, cursor, server_implementation)
745753

746754
module.exit_json(changed=False,
747755
connector_name=connector_name,

tests/unit/plugins/module_utils/test_mysql.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import (absolute_import, division, print_function)
2+
23
__metaclass__ = type
34

45
import pytest
56

6-
from ansible_collections.community.mysql.plugins.module_utils.mysql import get_server_version
7+
from ansible_collections.community.mysql.plugins.module_utils.mysql import get_server_version, get_server_implementation
78
from ..utils import dummy_cursor_class
89

910

@@ -22,3 +23,21 @@ def test_get_server_version(cursor_return_version, cursor_return_type):
2223
"""
2324
cursor = dummy_cursor_class(cursor_return_version, cursor_return_type)
2425
assert get_server_version(cursor) == cursor_return_version
26+
27+
28+
@pytest.mark.parametrize(
29+
'cursor_return_version,cursor_return_type,server_implementation',
30+
[
31+
('5.7.0-mysql', 'dict', 'mysql'),
32+
('8.0.0-mysql', 'list', 'mysql'),
33+
('10.5.0-mariadb', 'dict', 'mariadb'),
34+
('10.5.1-mariadb', 'list', 'mariadb'),
35+
]
36+
)
37+
def test_get_server_implamentation(cursor_return_version, cursor_return_type, server_implementation):
38+
"""
39+
Test that server implementation are handled properly by get_server_implementation() whether the server version returned as a list or dict.
40+
"""
41+
cursor = dummy_cursor_class(cursor_return_version, cursor_return_type)
42+
43+
assert get_server_implementation(cursor) == server_implementation

tests/unit/plugins/modules/test_mysql_info.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515

1616
@pytest.mark.parametrize(
17-
'suffix,cursor_output',
17+
'suffix,cursor_output,server_implementation',
1818
[
19-
('mysql', '5.5.1-mysql'),
20-
('log', '5.7.31-log'),
21-
('mariadb', '10.5.0-mariadb'),
22-
('', '8.0.22'),
19+
('mysql', '5.5.1-mysql', 'mysql'),
20+
('log', '5.7.31-log', 'mysql'),
21+
('mariadb', '10.5.0-mariadb', 'mariadb'),
22+
('', '8.0.22', 'mysql'),
2323
]
2424
)
25-
def test_get_info_suffix(suffix, cursor_output):
25+
def test_get_info_suffix(suffix, cursor_output, server_implementation):
2626
def __cursor_return_value(input_parameter):
2727
if input_parameter == "SHOW GLOBAL VARIABLES":
2828
cursor.fetchall.return_value = [{"Variable_name": "version", "Value": cursor_output}]
@@ -32,6 +32,6 @@ def __cursor_return_value(input_parameter):
3232
cursor = MagicMock()
3333
cursor.execute.side_effect = __cursor_return_value
3434

35-
info = MySQL_Info(MagicMock(), cursor)
35+
info = MySQL_Info(MagicMock(), cursor, server_implementation)
3636

3737
assert info.get_info([], [], False)['version']['suffix'] == suffix

0 commit comments

Comments
 (0)