Module: check_mk
Branch: master
Commit: bf60746a27e2fb042afe92629f8b3c7449f4faf6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=bf60746a27e2fb…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Apr 24 09:03:10 2019 +0200
Move config.extra_host_attributes processing to HostConfig
CMK-2030
Change-Id: I441a66571857a5a6377ee9bcb9012e6ae30c6ac0
---
cmk_base/config.py | 14 ++++++++++++++
cmk_base/core_config.py | 15 +--------------
tests/unit/cmk_base/test_config.py | 32 ++++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 14 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 2451188..6600138 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2582,6 +2582,20 @@ class HostConfig(object):
return list(set(self._config_cache.host_extra_conf(self.hostname,
host_icons_and_actions)))
@property
+ def extra_host_attributes(self):
+ # type: () -> Dict[str, str]
+ attrs = {}
+ for key, ruleset in extra_host_conf.items():
+ values = self._config_cache.host_extra_conf(self.hostname, ruleset)
+ if values:
+ if key[0] == "_":
+ key = key.upper()
+
+ if values[0] is not None:
+ attrs[key] = values[0]
+ return attrs
+
+ @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 37da308..9c11136 100644
--- a/cmk_base/core_config.py
+++ b/cmk_base/core_config.py
@@ -383,8 +383,8 @@ def _icons_and_actions_of_service(config_cache, host_config, svcdesc,
checkname,
def get_host_attributes(hostname, config_cache):
- attrs = _extra_host_attributes(config_cache, hostname)
host_config = config_cache.get_host_config(hostname)
+ attrs = host_config.extra_host_attributes
# Pre 1.6 legacy attribute. We have changed our whole code to use the
# livestatus column "tags" which is populated by all attributes starting
with
@@ -454,19 +454,6 @@ def _get_tag_attributes(collection, prefix):
return {u"__%s_%s" % (prefix, k): unicode(v) for k, v in
collection.iteritems()}
-def _extra_host_attributes(config_cache, hostname):
- attrs = {}
- for key, conflist in config.extra_host_conf.items():
- values = config_cache.host_extra_conf(hostname, conflist)
- if values:
- if key[0] == "_":
- key = key.upper()
-
- if values[0] is not None:
- attrs[key] = values[0]
- return attrs
-
-
def get_cluster_attributes(config_cache, host_config, nodes):
# type: (config.ConfigCache, config.HostConfig, List[str]) -> Dict
sorted_nodes = sorted(nodes)
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index bce2a1e..1182ad1 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -463,6 +463,38 @@ def test_host_config_icons_and_actions(monkeypatch, hostname,
result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", {}),
+ ("testhost2", {
+ '_CUSTOM': ['value1'],
+ 'dingdong': ['value1']
+ }),
+])
+def test_host_config_extra_host_attributes(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_option(
+ "extra_host_conf", {
+ "dingdong": [
+ ([
+ "value1",
+ ], [], ["testhost2"], {}),
+ ([
+ "value2",
+ ], [], ["testhost2"], {}),
+ ],
+ "_custom": [
+ ([
+ "value1",
+ ], [], ["testhost2"], {}),
+ ([
+ "value2",
+ ], [], ["testhost2"], {}),
+ ],
+ })
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).extra_host_attributes == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])