Module: check_mk
Branch: master
Commit: 66eab3c7d01f803b433376640e9348a2b03971a1
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=66eab3c7d01f80…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Nov 1 19:38:18 2018 +0100
Host attributes can now be locked individually
This is will be needed for the dynamic configuration but is not limited
to this feature. It may be used in other situations where some
attributes of a host / folder are controlled by another tool instead of
manual configuration and manual editing is not permitted.
The locking is done for each object individually. Locked attributes can
not be edited using the GUI anymore.
The lock can currently only be set or removed using the Web API.
Adding a lock is done by setting the "locked" attribute with a data
structure like this:
<pre>
(...)
"locked": {
"locked_by": ("site_id", "dcd",
"connection-id"),
"attributes": [
"alias",
"ipaddress",
"ipv6address",
],
},
(...)
</pre>
The structure above locks the host attributes alias, ipaddress and
ipv6address.
The locked_by attribute is the Check_MK installation global identity of
the component that set the lock. This is either used as information for
that component that this object belongs to it and tells other components
that they are not allowed to edit these attributes.
CMK-1144
Change-Id: I70c8a15d42a0b508a84775104520d206ae84476b
---
cmk/gui/plugins/wato/builtin_attributes.py | 34 ++++++++++++++++++++++++++++++
cmk/gui/plugins/wato/utils/__init__.py | 19 +++++++++++++----
cmk/gui/watolib.py | 3 +++
3 files changed, 52 insertions(+), 4 deletions(-)
diff --git a/cmk/gui/plugins/wato/builtin_attributes.py
b/cmk/gui/plugins/wato/builtin_attributes.py
index 6259000..82067e2 100644
--- a/cmk/gui/plugins/wato/builtin_attributes.py
+++ b/cmk/gui/plugins/wato/builtin_attributes.py
@@ -50,6 +50,7 @@ from cmk.gui.valuespec import (
AbsoluteDate,
TextUnicode,
SiteChoice,
+ ID,
)
from cmk.gui.exceptions import MKUserError
@@ -577,3 +578,36 @@ declare_host_attribute(
show_in_table=True,
show_in_folder=True,
)
+
+declare_host_attribute(
+ ValueSpecAttribute(
+ "locked",
+ Dictionary(
+ title=_("Locked"),
+ help=_("The host is (partially) managed by an automatic data source like
the "
+ "Dynamic Configuration."),
+ elements=[
+ ("locked_by",
+ Tuple(
+ orientation="horizontal",
+ title_br=False,
+ elements=[
+ SiteChoice(),
+ ID(title=_("Program"),),
+ ID(title=_("Connection ID"),),
+ ],
+ title=_("Locked by"),
+ )),
+ ("attributes", ListOf(
+ ID(),
+ title=_("Locked attributes"),
+ )),
+ ],
+ )),
+ show_in_table=False,
+ show_in_form=True,
+ show_in_folder=True,
+ show_in_host_search=False,
+ show_inherited_value=False,
+ editable=False,
+)
diff --git a/cmk/gui/plugins/wato/utils/__init__.py
b/cmk/gui/plugins/wato/utils/__init__.py
index afa6f79..add5d03 100644
--- a/cmk/gui/plugins/wato/utils/__init__.py
+++ b/cmk/gui/plugins/wato/utils/__init__.py
@@ -1340,6 +1340,7 @@ mode_registry = ModeRegistry()
# parent: The parent folder of the objects to configure
# myself: For mode "folder" the folder itself or None, if we edit a new folder
# This is needed for handling mandatory attributes.
+# TODO: Wow, this function REALLY has to be cleaned up
def configure_attributes(new,
hosts,
for_what,
@@ -1436,9 +1437,17 @@ def configure_attributes(new,
# "bulk": determine, if this attribute has the same setting for all
hosts.
values = []
+ num_have_locked_it = 0
num_haveit = 0
for host in hosts.itervalues():
- if host and host.has_explicit_attribute(attrname):
+ if not host:
+ continue
+
+ locked = host.attribute("locked")
+ if locked and attrname in locked["attributes"]:
+ num_have_locked_it += 1
+
+ if host.has_explicit_attribute(attrname):
num_haveit += 1
if host.attribute(attrname) not in values:
values.append(host.attribute(attrname))
@@ -1532,7 +1541,9 @@ def configure_attributes(new,
else:
active = False
- if not new and (not attr.editable() or not attr.may_edit()):
+ is_editable = attr.editable() and attr.may_edit() and num_have_locked_it ==
0
+
+ if not new and not is_editable:
# Bug in pylint 1.9.2
https://github.com/PyCQA/pylint/issues/1984,
already fixed in master.
if active: # pylint: disable=simplifiable-if-statement
force_entry = True
@@ -1562,7 +1573,7 @@ def configure_attributes(new,
else:
defvalue = attr.default_value()
- if not new and (not attr.editable() or not attr.may_edit()):
+ if not new and not is_editable:
# In edit mode only display non editable values, don't show the
# input fields
html.open_div(id_="attr_hidden_%s" % attrname,
style="display:none;")
@@ -1602,7 +1613,7 @@ def configure_attributes(new,
value = values[0]
elif for_what in ["host", "cluster",
"folder"]:
- if not new and (not attr.editable() or not attr.may_edit()) and active:
+ if not new and not is_editable and active:
value = values[0]
else:
explanation = " (" + inherited_from + ")"
diff --git a/cmk/gui/watolib.py b/cmk/gui/watolib.py
index 25f4f9b..345c021 100644
--- a/cmk/gui/watolib.py
+++ b/cmk/gui/watolib.py
@@ -1485,6 +1485,7 @@ class CREFolder(BaseFolder):
return
variables = self._load_hosts_file()
+ # Can either be set to True or a string (which will be used as host lock
message)
self._locked_hosts = variables["_lock"]
# Add entries in clusters{} to all_hosts, prepare cluster to node mapping
@@ -1817,7 +1818,9 @@ class CREFolder(BaseFolder):
wato_info = self._load_wato_info()
self._title = wato_info.get("title", self._fallback_title())
self._attributes =
self._transform_old_attributes(wato_info.get("attributes", {}))
+ # Can either be set to True or a string (which will be used as host lock
message)
self._locked = wato_info.get("lock", False)
+ # Can either be set to True or a string (which will be used as host lock
message)
self._locked_subfolders = wato_info.get("lock_subfolders", False)
if "num_hosts" in wato_info: