Skip to content

Commit

Permalink
add mysql_full_version and suffix return variable (#115)
Browse files Browse the repository at this point in the history
* add mysql_full_version and suffix return variable

add changelog fragment

* rephrase changelog fragment

* Update plugins/modules/mysql_info.py

Co-authored-by: Andrew Klychkov <[email protected]>

* Add changes as per PR review

* Add tests for new suffix output parameter

Co-authored-by: Andrew Klychkov <[email protected]>
Co-authored-by: Jorge-Rodriguez <[email protected]>
  • Loading branch information
3 people authored Mar 16, 2021
1 parent 2254b29 commit a5ee4b3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
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

0 comments on commit a5ee4b3

Please sign in to comment.