Module: check_mk
Branch: master
Commit: 55d2bdc79e39d4a90e190595b567b93f71e86f3c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=55d2bdc79e39d4…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Apr 24 08:58:02 2019 +0200
Move config.host_icons_and_actions processing to HostConfig
CMK-2030
Change-Id: Ib4d56373a8904336b672679af845602abaeab7cd
---
cmk_base/config.py | 5 +++++
cmk_base/core_config.py | 44 ++++++++++++++++++--------------------
tests/unit/cmk_base/test_config.py | 15 +++++++++++++
3 files changed, 41 insertions(+), 23 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 96d9173..2451188 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2577,6 +2577,11 @@ class HostConfig(object):
return levels
@property
+ def icons_and_actions(self):
+ # type: () -> List[str]
+ return list(set(self._config_cache.host_extra_conf(self.hostname,
host_icons_and_actions)))
+
+ @property
def hostgroups(self):
# type: () -> List[str]
"""Returns the list of hostgroups of this host
diff --git a/cmk_base/core_config.py b/cmk_base/core_config.py
index fb498ff..37da308 100644
--- a/cmk_base/core_config.py
+++ b/cmk_base/core_config.py
@@ -29,7 +29,7 @@ import numbers
import os
import sys
import itertools
-from typing import Optional, Any, List, Dict # pylint: disable=unused-import
+from typing import Text, Optional, Any, List, Dict # pylint: disable=unused-import
import cmk.utils.paths
import cmk.utils.tty as tty
@@ -170,25 +170,6 @@ def autodetect_plugin(command_line):
return command_line
-# TODO: Better move to cmk_base.config module
-def _icons_and_actions_of(config_cache, what, hostname, svcdesc=None, checkname=None,
params=None):
- if what == 'host':
- return list(set(config_cache.host_extra_conf(hostname,
config.host_icons_and_actions)))
- else:
- actions = set(
- config_cache.service_extra_conf(hostname, svcdesc,
config.service_icons_and_actions))
-
- # Some WATO rules might register icons on their own
- if checkname:
- checkgroup = config.check_info[checkname]["group"]
- if checkgroup in ['ps', 'services'] and isinstance(params,
dict):
- icon = params.get('icon')
- if icon:
- actions.add(icon)
-
- return list(actions)
-
-
def check_icmp_arguments_of(config_cache, hostname, add_defaults=True, family=None):
host_config = config_cache.get_host_config(hostname)
levels = host_config.ping_levels
@@ -358,8 +339,9 @@ def _extra_service_attributes(hostname, description, config_cache,
checkname, pa
attrs["_%s" % varname.upper()] = value
# Add custom user icons and actions
- actions = _icons_and_actions_of(config_cache, "service", hostname,
description, checkname,
- params)
+ host_config = config_cache.get_host_config(hostname)
+ actions = _icons_and_actions_of_service(config_cache, host_config, description,
checkname,
+ params)
if actions:
attrs["_ACTIONS"] = ','.join(actions)
return attrs
@@ -371,6 +353,22 @@ def _custom_service_attributes_of(config_cache, hostname,
service_description):
config.custom_service_attributes)))
+def _icons_and_actions_of_service(config_cache, host_config, svcdesc, checkname,
params):
+ actions = set(
+ config_cache.service_extra_conf(host_config.hostname, svcdesc,
+ config.service_icons_and_actions))
+
+ # Some WATO rules might register icons on their own
+ if checkname:
+ checkgroup = config.check_info[checkname]["group"]
+ if checkgroup in ['ps', 'services'] and isinstance(params,
dict):
+ icon = params.get('icon')
+ if icon:
+ actions.add(icon)
+
+ return list(actions)
+
+
#.
# .--HostAttributes------------------------------------------------------.
# | _ _ _ _ _ _ _ _ _ |
@@ -442,7 +440,7 @@ def get_host_attributes(hostname, config_cache):
attrs["_FILENAME"] = path
# Add custom user icons and actions
- actions = _icons_and_actions_of(config_cache, "host", hostname)
+ actions = host_config.icons_and_actions
if actions:
attrs["_ACTIONS"] = ",".join(actions)
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 9b55acb..bce2a1e 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -448,6 +448,21 @@ def test_host_config_ping_levels(monkeypatch, hostname, result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", []),
+ ("testhost2", ["icon1", "icon2"]),
+])
+def test_host_config_icons_and_actions(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset("host_icons_and_actions", [
+ ("icon1", [], ["testhost2"], {}),
+ ("icon1", [], ["testhost2"], {}),
+ ("icon2", [], ["testhost2"], {}),
+ ])
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).icons_and_actions == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])