[checkmk-commits] Check_MK Git: check_mk: Web-API improvements: now able to unset host attributes via edit_host function

Andreas Boesl ab at mathias-kettner.de
Tue Aug 12 17:56:53 CEST 2014


Module: check_mk
Branch: master
Commit: 8cda5a5e6135fdefca7bad5d8574a2ac6a6d2a2d
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8cda5a5e6135fdefca7bad5d8574a2ac6a6d2a2d

Author: Andreas Boesl <ab at mathias-kettner.de>
Date:   Tue Aug 12 17:56:46 2014 +0200

Web-API improvements: now able to unset host attributes via edit_host function

---

 web/htdocs/wato.py           |   23 +++++++++++------------
 web/plugins/webapi/webapi.py |    8 +++++---
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 7c05413..af6fc2b 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -16834,17 +16834,16 @@ def add_host_to_folder(folder, hostname, attributes):
     call_hook_hosts_changed(folder)
 
 # Updates attributes of one host
-# This function is quite special (tag deletion) and only used by the Web-API so far
-def update_host_attributes(host, attributes):
+def update_host_attributes(host, attr = {}, unset_attr = []):
     cleaned_attr = dict([(k, v) for (k, v) in host.iteritems() if not k.startswith('.') ])
 
-    for key, value in attributes.items():
-        # If a host tag (e.g. tag_agent) is set to False, delete this key
-        if key.startswith("tag_") and value == False:
-            if key in cleaned_attr:
-                del cleaned_attr[key]
-        else:
-            cleaned_attr[key] = value
+    # unset keys
+    for key in unset_attr:
+        if key in cleaned_attr:
+            del cleaned_attr[key]
+
+    # set new keys
+    cleaned_attr.update(attr)
 
     set_host_attributes(host, cleaned_attr)
 
@@ -17011,18 +17010,18 @@ class API:
         # Update the all_hosts reference. Saves quite some time on followup calls
         all_hosts[hostname] = folder[".hosts"][hostname]
 
-    def edit_host(self, hostname, host_attr, dry_run = False):
+    def edit_host(self, hostname, attr = {}, unset_attr = []):
         self.__prepare_folder_info()
         all_hosts = self.__get_all_hosts()
 
-        attributes = self.__get_valid_api_host_attributes(host_attr)
+        attributes = self.__get_valid_api_host_attributes(attr)
         self.__validate_host_data(hostname, attributes = attributes,
                                             all_hosts = all_hosts,
                                             validate = ["host_missing", "tags", "site", "permissions_edit"])
 
         ### Update Host
         host = all_hosts[hostname]
-        update_host_attributes(host, attributes)
+        update_host_attributes(host, attr = attributes, unset_attr = unset_attr)
 
         # Update all_hosts reference. Saves quite some time on followup calls
         folder_path = all_hosts[hostname][".folder"][".path"]
diff --git a/web/plugins/webapi/webapi.py b/web/plugins/webapi/webapi.py
index 744c52e..131a475 100644
--- a/web/plugins/webapi/webapi.py
+++ b/web/plugins/webapi/webapi.py
@@ -63,13 +63,14 @@ api_actions["add_host"] = {
 ###############
 
 def action_edit_host(request):
-    hostname   = request.get("hostname")
-    attributes = request.get("attributes", {})
+    hostname         = request.get("hostname")
+    attributes       = request.get("attributes", {})
+    unset_attributes = request.get("unset_attributes", [])
 
     if not hostname:
         raise MKUserError(None, "Hostname is missing")
 
-    return g_api.edit_host(hostname, attributes)
+    return g_api.edit_host(hostname, attr = attributes, unset_attr = unset_attributes)
 
 api_actions["edit_host"] = {
     "handler"     : action_edit_host,
@@ -81,6 +82,7 @@ api_actions["edit_host"] = {
                                     "tag_agent": "snmp-only",
                                     "site": "slave"
                                 },
+                           "unset_attributes": ["tag_criticality"],
                            "hostname": "testhost"
                          }),
     "locking"     : True,



More information about the checkmk-commits mailing list