Module: check_mk
Branch: master
Commit: 13dba66f5e514e024dad21c71e01fd6ed09e0493
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=13dba66f5e514e…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Wed May 9 12:42:45 2018 +0200
5809 New Web API Call: delete_hosts, allows to delete multiple hosts at once
The required payload for delete_hosts is a variable <tt>hostnames</tt> with a
list of hostnames.
For example, <tt>"hostnames": ["host1", "host2",
"host3"]</tt>.
The API returns an error if a specified host does not exist.
Change-Id: I788711d72507b446a2aa5cba223e77e9b9c2da0d
---
.werks/5809 | 16 ++++++++++++++++
web/plugins/webapi/webapi.py | 23 +++++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/.werks/5809 b/.werks/5809
new file mode 100644
index 0000000..e9eb964
--- /dev/null
+++ b/.werks/5809
@@ -0,0 +1,16 @@
+Title: New Web API Call: delete_hosts, allows to delete multiple hosts at once
+Level: 1
+Component: wato
+Compatible: compat
+Edition: cre
+Version: 1.6.0i1
+Date: 1525862330
+Class: feature
+
+The required payload for delete_hosts is a variable <tt>hostnames</tt> with a
list of hostnames.
+
+For example, <tt>"hostnames": ["host1", "host2",
"host3"]</tt>.
+
+The API returns an error if a specified host does not exist.
+
+
diff --git a/web/plugins/webapi/webapi.py b/web/plugins/webapi/webapi.py
index b2a41f1..cce2963 100644
--- a/web/plugins/webapi/webapi.py
+++ b/web/plugins/webapi/webapi.py
@@ -310,6 +310,10 @@ class APICallHosts(APICallCollection):
"handler" : self._delete,
"locking" : True,
},
+ "delete_hosts": {
+ "handler" : self._delete_hosts,
+ "locking" : True,
+ },
"get_all_hosts": {
"handler" : self._get_all,
"locking" : False,
@@ -442,6 +446,25 @@ class APICallHosts(APICallCollection):
host.folder().delete_hosts([host.name()])
+ def _delete_hosts(self, request):
+ validate_request_keys(request, required_keys=["hostnames"])
+
+ all_hosts = watolib.Host.all()
+ delete_hostnames = set(request["hostnames"])
+ all_hostnames = set(all_hosts.keys())
+
+ unknown_hosts = delete_hostnames - all_hostnames
+ if unknown_hosts:
+ raise MKUserError(None, _("No such host(s): %s") % ",
".join(unknown_hosts))
+
+ grouped_by_folders = {}
+ for hostname in delete_hostnames:
+ grouped_by_folders.setdefault(all_hosts[hostname].folder(),
[]).append(hostname)
+
+ for folder, hostnames in grouped_by_folders.iteritems():
+ folder.delete_hosts(hostnames)
+
+
#.
# .--Groups--------------------------------------------------------------.
# | ____ |