Module: check_mk
Branch: master
Commit: 58267b64ce2062d222ab71ee48e7fae566d1c05d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=58267b64ce2062…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Apr 24 09:48:50 2019 +0200
Move config.inv_parameters processing to HostConfig
CMK-2030
Change-Id: Id7b7bf3635792ac30ce75f7b215e181e6250ab2d
---
cmk_base/config.py | 5 +++++
cmk_base/inventory.py | 15 +++++----------
tests/unit/cmk_base/test_config.py | 23 +++++++++++++++++++++++
3 files changed, 33 insertions(+), 10 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 6600138..e844725 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2595,6 +2595,11 @@ class HostConfig(object):
attrs[key] = values[0]
return attrs
+ def inventory_parameters(self, section_name):
+ # type: (str) -> Dict
+ return self._config_cache.host_extra_conf_merged(self.hostname,
+ inv_parameters.get(section_name,
[]))
+
@property
def hostgroups(self):
# type: () -> List[str]
diff --git a/cmk_base/inventory.py b/cmk_base/inventory.py
index b0fae6c..c89316f 100644
--- a/cmk_base/inventory.py
+++ b/cmk_base/inventory.py
@@ -223,8 +223,8 @@ def _do_inv_for(sources, multi_host_sections, host_config, ipaddress,
do_status_
_do_inv_for_cluster(host_config, inventory_tree)
else:
node["is_cluster"] = False
- _do_inv_for_realhost(sources, multi_host_sections, hostname, ipaddress,
inventory_tree,
- status_data_tree, discovered_host_labels)
+ _do_inv_for_realhost(host_config, sources, multi_host_sections, hostname,
ipaddress,
+ inventory_tree, status_data_tree, discovered_host_labels)
inventory_tree.normalize_nodes()
old_timestamp = _save_inventory_tree(hostname, inventory_tree)
@@ -265,8 +265,8 @@ def _do_inv_for_cluster(host_config, inventory_tree):
})
-def _do_inv_for_realhost(sources, multi_host_sections, hostname, ipaddress,
inventory_tree,
- status_data_tree, discovered_host_labels):
+def _do_inv_for_realhost(host_config, sources, multi_host_sections, hostname, ipaddress,
+ inventory_tree, status_data_tree, discovered_host_labels):
for source in sources.get_data_sources():
if isinstance(source, data_sources.SNMPDataSource):
source.set_on_error("raise")
@@ -324,7 +324,7 @@ def _do_inv_for_realhost(sources, multi_host_sections, hostname,
ipaddress, inve
kwargs[dynamic_arg_name] = dynamic_arg_value
if len(inv_function_args) == 2:
- params = _get_inv_params(hostname, section_name)
+ params = host_config.inventory_parameters(section_name)
args = [section_content, params]
else:
args = [section_content]
@@ -340,11 +340,6 @@ def _gather_snmp_check_plugin_names_inventory(host_config,
host_config, on_error, do_snmp_scan, for_inventory=True,
for_mgmt_board=for_mgmt_board)
-def _get_inv_params(hostname, section_name):
- return config.get_config_cache().host_extra_conf_merged(
- hostname, config.inv_parameters.get(section_name, []))
-
-
#.
# .--Inventory Tree------------------------------------------------------.
# | ___ _ _____ |
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 1182ad1..e7a0775 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -495,6 +495,29 @@ def test_host_config_extra_host_attributes(monkeypatch, hostname,
result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", {}),
+ ("testhost2", {
+ 'value1': 1,
+ 'value2': 2,
+ }),
+])
+def test_host_config_inventory_parameters(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_option("inv_parameters", {
+ "if": [
+ ({
+ "value1": 1,
+ }, [], ["testhost2"], {}),
+ ({
+ "value2": 2,
+ }, [], ["testhost2"], {}),
+ ],
+ })
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).inventory_parameters("if") ==
result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])