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

Zabbix: Fix to be support for zabbix 4.4 or more and python3 #67693

Merged
merged 1 commit into from
Feb 26, 2020
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
2 changes: 2 additions & 0 deletions changelogs/fragments/67693-zabbix_mediatype.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- zabbix_mediatype - Fixed to support zabbix 4.4 or more and python3 (https://github.com/ansible/ansible/pull/67693)
35 changes: 26 additions & 9 deletions lib/ansible/modules/monitoring/zabbix/zabbix_mediatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
type: 'str'
description:
- Type of the media type.
- Media types I(jabber) and I(ez_texting) workable only with Zabbix 4.2 or less.
choices:
- email
- script
Expand Down Expand Up @@ -223,7 +224,7 @@


from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import container_to_bytes
from distutils.version import LooseVersion


try:
Expand Down Expand Up @@ -350,7 +351,7 @@ def construct_parameters(**kwargs):
attempt_interval=str(kwargs['attempt_interval']),
gsm_modem=kwargs['gsm_modem']
)
elif kwargs['transport_type'] == 'jabber':
elif kwargs['transport_type'] == 'jabber' and LooseVersion(kwargs['zbx_api_version']) <= LooseVersion('4.2'):
return dict(
description=kwargs['name'],
status=to_numeric_value(kwargs['status'],
Expand All @@ -368,7 +369,7 @@ def construct_parameters(**kwargs):
username=kwargs['username'],
passwd=kwargs['password']
)
elif kwargs['transport_type'] == 'ez_texting':
elif kwargs['transport_type'] == 'ez_texting' and LooseVersion(kwargs['zbx_api_version']) <= LooseVersion('4.2'):
return dict(
description=kwargs['name'],
status=to_numeric_value(kwargs['status'],
Expand All @@ -390,8 +391,10 @@ def construct_parameters(**kwargs):
'Canada': '1'}),
)

return {'unsupported_parameter': kwargs['transport_type'], 'zbx_api_version': kwargs['zbx_api_version']}

def check_if_mediatype_exists(module, zbx, name):

def check_if_mediatype_exists(module, zbx, name, zbx_api_version):
"""Checks if mediatype exists.

Args:
Expand All @@ -402,10 +405,15 @@ def check_if_mediatype_exists(module, zbx, name):
Returns:
Tuple of (True, `id of the mediatype`) if mediatype exists, (False, None) otherwise
"""
filter_key_name = 'description'
if LooseVersion(zbx_api_version) >= LooseVersion('4.4'):
# description key changed to name key from zabbix 4.4
filter_key_name = 'name'

try:
mediatype_list = zbx.mediatype.get({
'output': 'extend',
'filter': {'description': [name]}
'filter': {filter_key_name: [name]}
})
if len(mediatype_list) < 1:
return False, None
Expand Down Expand Up @@ -452,10 +460,10 @@ def get_update_params(module, zbx, mediatype_id, **kwargs):
returned by diff() function with
existing mediatype data and new params passed to it.
"""
existing_mediatype = container_to_bytes(zbx.mediatype.get({
existing_mediatype = zbx.mediatype.get({
'output': 'extend',
'mediatypeids': [mediatype_id]
})[0])
})[0]

if existing_mediatype['type'] != kwargs['type']:
return kwargs, diff(existing_mediatype, kwargs)
Expand Down Expand Up @@ -589,7 +597,8 @@ def main():
except Exception as e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

mediatype_exists, mediatype_id = check_if_mediatype_exists(module, zbx, name)
zbx_api_version = zbx.api_version()[:3]
mediatype_exists, mediatype_id = check_if_mediatype_exists(module, zbx, name, zbx_api_version)

parameters = construct_parameters(
name=name,
Expand All @@ -611,9 +620,17 @@ def main():
smtp_authentication=smtp_authentication,
smtp_verify_host=smtp_verify_host,
smtp_verify_peer=smtp_verify_peer,
message_text_limit=message_text_limit
message_text_limit=message_text_limit,
zbx_api_version=zbx_api_version
)

if 'unsupported_parameter' in parameters:
module.fail_json(msg="%s is unsupported for Zabbix version %s" % (parameters['unsupported_parameter'], parameters['zbx_api_version']))

if LooseVersion(zbx_api_version) >= LooseVersion('4.4'):
# description key changed to name key from zabbix 4.4
parameters['name'] = parameters.pop('description')

if mediatype_exists:
if state == 'absent':
if module.check_mode:
Expand Down