Module: check_mk
Branch: master
Commit: fb61174f85477d500caf9a3cf5bc4649129526c5
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fb61174f85477d…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Dec 26 21:14:35 2018 +0100
New API call edit_hosts
Added a new API call "edit_hosts" which does the same work as the
"edit_host" calls with the difference that it may create multiple
hosts.
The request must contain a single key "hosts" which must be a list
containing the single host specifications equal to the edit_host call.
Added tests for the "edit_host" call on the way.
Change-Id: If80aeb83865be64ff94748b3ddf94a1be67f9ed1
---
cmk/gui/plugins/webapi/webapi.py | 9 ++++++
tests/integration/cmk/gui/test_webapi.py | 35 +++++++++++++++++++++++
tests/testlib/__init__.py | 49 ++++++++++++++++++++++++++++++++
3 files changed, 93 insertions(+)
diff --git a/cmk/gui/plugins/webapi/webapi.py b/cmk/gui/plugins/webapi/webapi.py
index 9e9daff..a935934 100644
--- a/cmk/gui/plugins/webapi/webapi.py
+++ b/cmk/gui/plugins/webapi/webapi.py
@@ -224,6 +224,10 @@ class APICallHosts(APICallCollection):
"handler": self._edit,
"locking": True,
},
+ "edit_hosts": {
+ "handler": self._edit_hosts,
+ "locking": True,
+ },
"get_host": {
"handler": self._get,
"locking": False,
@@ -328,6 +332,11 @@ class APICallHosts(APICallCollection):
host.edit(current_attributes, cluster_nodes)
+ def _edit_hosts(self, request):
+ validate_request_keys(request, required_keys=["hosts"])
+ for host_request in request["hosts"]:
+ self._edit(host_request)
+
def _get(self, request):
validate_request_keys(
request, required_keys=["hostname"],
optional_keys=["effective_attributes"])
diff --git a/tests/integration/cmk/gui/test_webapi.py
b/tests/integration/cmk/gui/test_webapi.py
index 298a3fd..3940a7f 100644
--- a/tests/integration/cmk/gui/test_webapi.py
+++ b/tests/integration/cmk/gui/test_webapi.py
@@ -69,6 +69,41 @@ def test_add_hosts(web):
web.delete_hosts(hosts)
+def test_edit_host(web):
+ try:
+ web.add_host(
+ "test-edit-host", attributes={
+ "ipaddress": "127.0.0.1",
+ })
+
+ web.edit_host("test-edit-host", attributes={"ipaddress":
"127.10.0.1"})
+ finally:
+ web.delete_host("test-edit-host")
+
+
+def test_edit_hosts(web):
+ try:
+ web.add_host(
+ "test-edit-hosts1", attributes={
+ "ipaddress": "127.0.0.1",
+ })
+ web.add_host(
+ "test-edit-hosts2", attributes={
+ "ipaddress": "127.0.0.1",
+ })
+
+ web.edit_hosts([
+ ("test-edit-hosts1", {
+ "ipaddress": "127.10.0.1"
+ }, []),
+ ("test-edit-hosts2", {
+ "ipaddress": "127.20.0.1"
+ }, []),
+ ])
+ finally:
+ web.delete_hosts(["test-edit-hosts1", "test-edit-hosts2"])
+
+
def test_get_all_hosts_basic(web):
try:
web.add_host(
diff --git a/tests/testlib/__init__.py b/tests/testlib/__init__.py
index 14f00e0..4afd1e4 100644
--- a/tests/testlib/__init__.py
+++ b/tests/testlib/__init__.py
@@ -1283,6 +1283,55 @@ class CMKWebSession(WebSession):
for hostname, _folder, _attributes in create_hosts:
assert hostname in hosts
+ def edit_host(self, hostname, attributes=None, unset_attributes=None,
cluster_nodes=None):
+ if attributes is None:
+ attributes = {}
+
+ if unset_attributes is None:
+ unset_attributes = []
+
+ result = self._api_request(
+ "webapi.py?action=edit_host", {
+ "request": json.dumps({
+ "hostname": hostname,
+ "unset_attributes": unset_attributes,
+ "attributes": attributes,
+ "nodes": cluster_nodes,
+ }),
+ })
+
+ assert result is None
+
+ host = self.get_host(hostname)
+
+ assert host["hostname"] == hostname
+ assert host["attributes"] == attributes
+
+ def edit_hosts(self, edit_hosts):
+ hosts = [{
+ "hostname": hostname,
+ "attributes": attributes,
+ "unset_attributes": unset_attributes,
+ } for hostname, attributes, unset_attributes in edit_hosts]
+
+ result = self._api_request("webapi.py?action=edit_hosts", {
+ "request": json.dumps({
+ "hosts": hosts,
+ }),
+ })
+
+ assert result is None
+
+ hosts = self.get_all_hosts()
+ for hostname, attributes, unset_attributes in edit_hosts:
+ host = hosts[hostname]
+
+ for k, v in attributes.items():
+ assert host["attributes"][k] == v
+
+ for unset in unset_attributes:
+ assert unset not in host["attributes"]
+
def get_host(self, hostname, effective_attributes=False):
result = self._api_request(
"webapi.py?action=get_host", {