Module: check_mk
Branch: master
Commit: eca0e13a18ed01200647cd4074f8fa4628d74866
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=eca0e13a18ed01…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Apr 3 09:50:41 2019 +0200
ConfigCache: Manage HostConfig instances during lifetime
The base code may use config_cache.get_host_config(hostname) to
get a host configuration instance of the given host name. The
ConfigCache object ensures that only a single object is created
per host.
Change-Id: I9b6f285ff5db064318c20eee005206f1ec8ea14f
---
cmk_base/config.py | 15 +++++++++++++++
tests/unit/cmk_base/test_config.py | 9 +++++++++
2 files changed, 24 insertions(+)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index a306b9f..f43a361 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2927,6 +2927,21 @@ class ConfigCache(object):
# It is used to determine the best rule evualation method
self._all_processed_hosts_similarity = 1
+ # Keep HostConfig instances created with the current configuration cache
+ self._host_configs = {}
+
+ def get_host_config(self, hostname):
+ """Returns a HostConfig instance for the given host
+
+ It lazy initializes the host config object and caches the objects during the
livetime
+ of the ConfigCache."""
+ host_config = self._host_configs.get(hostname)
+ if host_config:
+ return host_config
+
+ host_config = self._host_configs[hostname] = HostConfig(hostname)
+ return host_config
+
def _collect_hosttags(self):
for tagged_host in all_hosts + clusters.keys():
parts = tagged_host.split("|")
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index f18895b..4994423 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -386,3 +386,12 @@ def test_tags_of_service(monkeypatch):
assert config.get_config_cache().tags_of_service("test-host", "CPU
load") == {
"tag_group1": "val1"
}
+
+
+def test_config_cache_get_host_config():
+ cache = config.ConfigCache()
+ assert cache._host_configs == {}
+
+ host_config = cache.get_host_config("xyz")
+ assert isinstance(host_config, config.HostConfig)
+ assert host_config is cache.get_host_config("xyz")