Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mysql_full_version and suffix return variable #115

Merged
merged 5 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- mysql_info - add `version.full` and `version.suffix` return values (https:/ansible-collections/community.mysql/issues/114).
38 changes: 33 additions & 5 deletions plugins/modules/mysql_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

author:
- Andrew Klychkov (@Andersson007)
- Sebastian Gumprich (@rndmh3ro)

extends_documentation_fragment:
- community.mysql.mysql
Expand Down Expand Up @@ -121,7 +122,7 @@
description: Database server version.
returned: if not excluded by filter
type: dict
sample: { "version": { "major": 5, "minor": 5, "release": 60 } }
sample: { "version": { "major": 5, "minor": 5, "release": 60, "suffix": "MariaDB", "full": "5.5.60-MariaDB" } }
contains:
major:
description: Major server version.
Expand All @@ -138,6 +139,16 @@
returned: if not excluded by filter
type: int
sample: 60
suffix:
description: Server suffix, for example MySQL, MariaDB, other or none.
returned: if not excluded by filter
type: str
sample: "MariaDB"
full:
description: Full server version.
returned: if not excluded by filter
type: str
sample: "5.5.60-MariaDB"
databases:
description: Information about databases.
returned: if not excluded by filter
Expand Down Expand Up @@ -353,13 +364,30 @@ def __get_global_variables(self):
for var in res:
self.info['settings'][var['Variable_name']] = self.__convert(var['Value'])

ver = self.info['settings']['version'].split('.')
release = ver[2].split('-')[0]
# version = ["5", "5," "60-MariaDB]
version = self.info['settings']['version'].split('.')

# full_version = "5.5.60-MariaDB"
full = self.info['settings']['version']

# release = "60"
release = version[2].split('-')[0]

# check if a suffix exists by counting the length
if len(version[2].split('-')) > 1:
# suffix = "MariaDB"
suffix = version[2].split('-', 1)[1]
else:
suffix = ""

self.info['version'] = dict(
major=int(ver[0]),
minor=int(ver[1]),
# major = "5"
major=int(version[0]),
# minor = "5"
minor=int(version[1]),
release=int(release),
suffix=str(suffix),
full=str(full),
)

def __get_global_status(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/targets/test_mysql_info/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
- assert:
that:
- result.changed == false
- result.version != {}
- "mysql_version in result.version.full"
- result.settings != {}
- result.global_status != {}
- result.databases != {}
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/plugins/modules/test_mysql_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import pytest

try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock

from ansible_collections.community.mysql.plugins.modules.mysql_info import MySQL_Info


@pytest.mark.parametrize(
'suffix,cursor_output',
[
('mysql', '5.5.1-mysql'),
('log', '5.7.31-log'),
('mariadb', '10.5.0-mariadb'),
('', '8.0.22'),
]
)
def test_get_info_suffix(suffix, cursor_output):
def __cursor_return_value(input_parameter):
if input_parameter == "SHOW GLOBAL VARIABLES":
cursor.fetchall.return_value = [{"Variable_name": "version", "Value": cursor_output}]
else:
cursor.fetchall.return_value = MagicMock()

cursor = MagicMock()
cursor.execute.side_effect = __cursor_return_value

info = MySQL_Info(MagicMock(), cursor)

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