Skip to content

Commit

Permalink
zabbix_template: Fix the issue for the unicode strings doesn’t decode…
Browse files Browse the repository at this point in the history
… in python2 (#322)

* fix the issue for the unicode strings doesn’t decode in python2

* add the integration test for #314

* add changelog file

* add the test for comparing the unicode #322 (comment)
  • Loading branch information
sky-joker committed Feb 2, 2021
1 parent 2f8c3a4 commit 14cf8b5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/322-zabbix_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- zabbix_template - fixed the bug that it doesn't decode the Unicode in Python2 (https://github.com/ansible-collections/community.zabbix/pull/322).
10 changes: 8 additions & 2 deletions plugins/modules/zabbix_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@

import json
import traceback
import codecs
import re
import xml.etree.ElementTree as ET

from distutils.version import LooseVersion
Expand Down Expand Up @@ -622,7 +622,13 @@ def import_template(self, template_content, template_type='json'):
if LooseVersion(self._zbx_api_version) >= LooseVersion('5.2'):
update_rules["templateDashboards"] = update_rules.pop("templateScreens")

template_content = codecs.decode(template_content, 'unicode-escape')
# The loaded unicode slash of multibyte as a string is escaped when parsing JSON by json.loads in Python2.
# So, it is imported in the unicode string into Zabbix.
# The following processing is removing the unnecessary slash in escaped for decoding correctly to the multibyte string.
# https://github.com/ansible-collections/community.zabbix/issues/314
if PY2:
template_content = re.sub(r'\\\\u([0-9a-z]{,4})', r'\\u\1', template_content)

import_data = {'format': template_type, 'source': template_content, 'rules': update_rules}
self._zapi.configuration.import_(import_data)
except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"zabbix_export": {
"version": "5.0",
"groups": [
{
"name": "Templates"
}
],
"templates": [
{
"template": "ExampleTemplate314",
"name": "ExampleTemplate314",
"description": "\u30c6\u30b9\u30c8\u30b3\u30e1\u30f3\u30c8",
"groups": [
{
"name": "Templates"
}
],
"items": [
{
"name": "test",
"type": "ZABBIX_ACTIVE",
"key": "logrt[\"/test.*test.log\",\"test.*total=([0-9.]+)\",,,skip,\\1]"
}
],
}
],
}
}
40 changes: 40 additions & 0 deletions tests/integration/targets/test_zabbix_template/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,43 @@
- assert:
that:
- delete_zabbix_template_result.changed is sameas false

- when: zabbix_version is version('5.0', '>=')
block:
# The test if decode Unicode correctly and to be imported the template.
# https://github.com/ansible-collections/community.zabbix/issues/314
- name: Import Zabbix template from JSON file with unicode.
zabbix_template:
server_url: "{{ zabbix_server_url }}"
login_user: "{{ zabbix_login_user }}"
login_password: "{{ zabbix_login_password }}"
template_json: "{{ lookup('file', 'template1_50_higher_decode_unicode.json') }}"
state: present
register: import_template_json

- name: Gather Zabbix template infomation.
zabbix_template_info:
server_url: "{{ zabbix_server_url }}"
login_user: "{{ zabbix_login_user }}"
login_password: "{{ zabbix_login_password }}"
template_name: ExampleTemplate314
format: json
register: gather_template_result

- assert:
that:
- import_template_json.changed is sameas true
- gather_template_result.template_json.zabbix_export.templates.0.description == "\u30c6\u30b9\u30c8\u30b3\u30e1\u30f3\u30c8"

- name: Delete Zabbix template.
zabbix_template:
server_url: "{{ zabbix_server_url }}"
login_user: "{{ zabbix_login_user }}"
login_password: "{{ zabbix_login_password }}"
template_name: ExampleTemplate314
state: absent
register: delete_zabbix_template_result

- assert:
that:
- delete_zabbix_template_result.changed is sameas true

0 comments on commit 14cf8b5

Please sign in to comment.