Module: check_mk
Branch: master
Commit: c03e54a0d7a0ecd943973e1c60a29674439dabb2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c03e54a0d7a0ec…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Jan 14 16:17:35 2019 +0100
Introduce HW/SW inventory web API call.
CMK-1404
Change-Id: I0a707bdd2360048fe13c4719418c994af15fa880
---
cmk/gui/inventory.py | 6 ++--
cmk/gui/plugins/webapi/inventory.py | 55 ++++++++++++++++++++++++++++++++
tests/integration/cmk/gui/test_webapi.py | 14 ++++++++
tests/testlib/__init__.py | 17 ++++++++++
tests/unit/cmk/gui/test_webapi_unit.py | 3 +-
5 files changed, 91 insertions(+), 4 deletions(-)
diff --git a/cmk/gui/inventory.py b/cmk/gui/inventory.py
index 0df4855..daa600c 100644
--- a/cmk/gui/inventory.py
+++ b/cmk/gui/inventory.py
@@ -380,14 +380,14 @@ def page_host_inv_api():
if hosts:
result = {}
for host_name in hosts:
- result[host_name] = _inventory_of_host(host_name, request)
+ result[host_name] = inventory_of_host(host_name, request)
else:
host_name = request.get("host")
if host_name is None:
raise MKUserError("host", _("You need to provide a
\"host\"."))
- result = _inventory_of_host(host_name, request)
+ result = inventory_of_host(host_name, request)
if not result and not has_inventory(host_name):
raise MKGeneralException(_("Found no inventory data for this
host."))
@@ -417,7 +417,7 @@ def has_inventory(hostname):
return os.path.exists(inventory_path)
-def _inventory_of_host(host_name, request):
+def inventory_of_host(host_name, request):
site = request.get("site")
if not _may_see(host_name, site):
raise MKAuthException(_("Sorry, you are not allowed to access the host
%s.") % host_name)
diff --git a/cmk/gui/plugins/webapi/inventory.py b/cmk/gui/plugins/webapi/inventory.py
new file mode 100644
index 0000000..992a1f6
--- /dev/null
+++ b/cmk/gui/plugins/webapi/inventory.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# .------------------------------------------------------------------------.
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |_____| |
+# | _____ _ _ |
+# | | ____|_ __ | |_ ___ _ __ _ __ _ __(_)___ ___ |
+# | | _| | '_ \| __/ _ \ '__| '_ \| '__| / __|/ _ \
|
+# | | |___| | | | || __/ | | |_) | | | \__ \ __/ |
+# | |_____|_| |_|\__\___|_| | .__/|_| |_|___/\___| |
+# | |_| |
+# | _____ _ _ _ _ |
+# | | ____|__| (_) |_(_) ___ _ __ |
+# | | _| / _` | | __| |/ _ \| '_ \ |
+# | | |__| (_| | | |_| | (_) | | | | |
+# | |_____\__,_|_|\__|_|\___/|_| |_| |
+# | |
+# |
mathias-kettner.com mathias-kettner.de |
+# '------------------------------------------------------------------------'
+# This file is part of the Check_MK Enterprise Edition (CEE).
+# Copyright by Mathias Kettner and Mathias Kettner GmbH. All rights reserved.
+#
+# Distributed under the Check_MK Enterprise License.
+#
+# You should have received a copy of the Check_MK Enterprise License
+# along with Check_MK. If not, email to mk(a)mathias-kettner.de
+# or write to the postal address provided at
www.mathias-kettner.de
+
+from cmk.gui.inventory import inventory_of_host
+
+from cmk.gui.plugins.webapi import (
+ api_call_collection_registry,
+ APICallCollection,
+)
+
+
+(a)api_call_collection_registry.register
+class APICallInventory(APICallCollection):
+ def get_api_calls(self):
+ return {
+ "get_inventory": {
+ "handler": self._get_inventory,
+ "required_keys": ["hosts"],
+ "optional_keys": ["site", "paths"],
+ "locking": False,
+ }
+ }
+
+ def _get_inventory(self, request):
+ return {host_name: inventory_of_host(host_name, request) \
+ for host_name in request.get("hosts")}
diff --git a/tests/integration/cmk/gui/test_webapi.py
b/tests/integration/cmk/gui/test_webapi.py
index 3940a7f..f844d23 100644
--- a/tests/integration/cmk/gui/test_webapi.py
+++ b/tests/integration/cmk/gui/test_webapi.py
@@ -745,3 +745,17 @@ def test_get_graph_hover(web, graph_test_config):
assert curve_value["rendered_value"][1] == "n/a"
#assert isinstance(curve_value["rendered_value"][0], (int, float))
#assert curve_value["rendered_value"][1] != ""
+
+
+def test_get_inventory(web):
+ try:
+ web.add_host(
+ "test-host", attributes={
+ "ipaddress": "127.0.0.1",
+ })
+
+ inv = web.get_inventory(["test-host"])
+
+ assert inv["test-host"] == {}
+ finally:
+ web.delete_host("test-host")
diff --git a/tests/testlib/__init__.py b/tests/testlib/__init__.py
index 4afd1e4..44cb4f2 100644
--- a/tests/testlib/__init__.py
+++ b/tests/testlib/__init__.py
@@ -1684,6 +1684,23 @@ class CMKWebSession(WebSession):
return result
+ def get_inventory(self, hosts, site=None, paths=None):
+ request = {
+ "hosts": hosts,
+ }
+ if site is not None:
+ request["site"] = site
+ if paths is not None:
+ request["paths"] = paths
+ result = self._api_request("webapi.py?action=get_inventory", {
+ "request": json.dumps(request),
+ })
+
+ assert isinstance(result, dict)
+ for host in hosts:
+ assert isinstance(result[host], dict)
+ return result
+
class CMKEventConsole(CMKWebSession):
def __init__(self, site):
diff --git a/tests/unit/cmk/gui/test_webapi_unit.py
b/tests/unit/cmk/gui/test_webapi_unit.py
index 6b5ad6d..15305f6 100644
--- a/tests/unit/cmk/gui/test_webapi_unit.py
+++ b/tests/unit/cmk/gui/test_webapi_unit.py
@@ -1,4 +1,4 @@
-# Needed to make the test load all webapi plugins (incl. CEE)
+# force loading of web API plugins
import cmk.gui.webapi # pylint: disable=unused-import
from cmk.gui.plugins.webapi.utils import api_call_collection_registry
@@ -46,6 +46,7 @@ def test_registered_api_call_collections():
'get_graph',
'get_host',
'get_hosttags',
+ 'get_inventory',
'get_ruleset',
'get_rulesets_info',
'get_site',