From e16d2ceae83dd76ae3ca740c445d4830ea0bafba Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Wed, 14 Sep 2016 05:13:35 -0700 Subject: [PATCH 1/2] add template.get function --- salt/modules/zabbix.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/salt/modules/zabbix.py b/salt/modules/zabbix.py index 52985f479c37..657e839da89b 100644 --- a/salt/modules/zabbix.py +++ b/salt/modules/zabbix.py @@ -1452,3 +1452,50 @@ def hostinterface_update(interfaceid, **connection_args): raise KeyError except KeyError: return ret + + +def template_get(name=None, host=None, templateids=None, **connection_args): + ''' + Retrieve templates according to the given parameters. + + Args: + host: technical name of the template + name: visible name of the template + hostids: ids of the templates + + optional connection_args: + _connection_user: zabbix user (can also be set in opts or pillar, see module's docstring) + _connection_password: zabbix password (can also be set in opts or pillar, see module's docstring) + _connection_url: url of zabbix frontend (can also be set in opts or pillar, see module's docstring) + + all optional template.get parameters: keyword argument names differ depending on your zabbix version, see: + + https://www.zabbix.com/documentation/2.4/manual/api/reference/template/get + + Returns: + Array with convenient template details, False if no template found or on failure. + + CLI Example: + .. code-block:: bash + + salt '*' zabbix.template_get name='Template OS Linux' + salt '*' zabbix.template_get templateids="['10050', '10001']" + ''' + conn_args = _login(**connection_args) + try: + if conn_args: + method = 'template.get' + params = {"output": "extend", "filter": {}} + if 'name': + params['filter'].setdefault('name', name) + if 'host': + params['filter'].setdefault('host', host) + if 'templateids': + params.setdefault('templateids', templateids) + params = _params_extend(params, **connection_args) + ret = _query(method, params, conn_args['url'], conn_args['auth']) + return ret['result'] if len(ret['result']) > 0 else False + else: + raise KeyError + except KeyError: + return False From 0a21ce9aa27aef0f15008bd29bf8be9c36179d46 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Sat, 17 Sep 2016 15:58:28 +0100 Subject: [PATCH 2/2] correct key check --- salt/modules/zabbix.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/salt/modules/zabbix.py b/salt/modules/zabbix.py index 657e839da89b..1c8327fcde17 100644 --- a/salt/modules/zabbix.py +++ b/salt/modules/zabbix.py @@ -1486,11 +1486,11 @@ def template_get(name=None, host=None, templateids=None, **connection_args): if conn_args: method = 'template.get' params = {"output": "extend", "filter": {}} - if 'name': + if name: params['filter'].setdefault('name', name) - if 'host': + if host: params['filter'].setdefault('host', host) - if 'templateids': + if templateids: params.setdefault('templateids', templateids) params = _params_extend(params, **connection_args) ret = _query(method, params, conn_args['url'], conn_args['auth'])