Skip to content

Commit

Permalink
Use existing folder_tree references
Browse files Browse the repository at this point in the history
Change-Id: I836d4da7749936d49b85a62f1656530f1cd330e8
  • Loading branch information
LarsMichelsen committed Oct 9, 2024
1 parent e96a35b commit 54b1674
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion cmk/gui/wato/pages/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from cmk.gui.watolib.hosts_and_folders import (
folder_from_request,
folder_preserving_link,
folder_tree,
Host,
validate_all_hosts,
)
Expand Down Expand Up @@ -214,7 +215,7 @@ def page(self) -> None:
errors = None
if self._mode == "edit":
errors = (
validate_all_hosts([self._host.name()]).get(self._host.name(), [])
validate_all_hosts(folder_tree(), [self._host.name()]).get(self._host.name(), [])
+ self._host.validation_errors()
)

Expand Down
2 changes: 1 addition & 1 deletion cmk/gui/watolib/activate_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ def start(
return self._activation_id

def _verify_valid_host_config(self):
defective_hosts = validate_all_hosts([], force_all=True)
defective_hosts = validate_all_hosts(folder_tree(), [], force_all=True)
if defective_hosts:
raise MKUserError(
None,
Expand Down
36 changes: 19 additions & 17 deletions cmk/gui/watolib/hosts_and_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ def _folder_attributes_for_base_config(self) -> dict[str, FolderAttributesForBas

def save(self) -> None:
self.save_folder_attributes()
folder_tree().invalidate_caches()
self.tree.invalidate_caches()
self.save_hosts()

def serialize(self) -> WATOFolderInfo:
Expand Down Expand Up @@ -1630,7 +1630,7 @@ def has_hosts(self) -> bool:
return len(self.hosts()) != 0

def host_validation_errors(self) -> dict[HostName, list[str]]:
return validate_all_hosts(self.host_names())
return validate_all_hosts(self.tree, self.host_names())

def has_parent(self) -> bool:
return self.parent() is not None
Expand Down Expand Up @@ -1809,7 +1809,7 @@ def _choices_for_moving(self, what: str) -> Choices:
get_wato_redis_client(self.tree).choices_for_moving(self.path(), _MoveType(what))
)

for folder in folder_tree().all_folders().values():
for folder in self.tree.all_folders().values():
if not folder.permissions.may("write"):
continue
if folder.is_same_as(self):
Expand Down Expand Up @@ -2205,7 +2205,7 @@ def delete_subfolder(self, name: str) -> None:
)
del self._subfolders[name]
shutil.rmtree(subfolder.filesystem_path())
folder_tree().invalidate_caches()
self.tree.invalidate_caches()
need_sidebar_reload()
folder_lookup_cache().delete()

Expand Down Expand Up @@ -2250,7 +2250,7 @@ def move_subfolder_to(self, subfolder: Folder, target_folder: Folder) -> None:
old_filesystem_path = subfolder.filesystem_path()
shutil.move(old_filesystem_path, target_folder.filesystem_path())

folder_tree().invalidate_caches()
self.tree.invalidate_caches()

# Since redis only updates on the next request, we can no longer use it here
# We COULD enforce a redis update here, but this would take too much time
Expand All @@ -2259,9 +2259,9 @@ def move_subfolder_to(self, subfolder: Folder, target_folder: Folder) -> None:
# Reload folder at new location and rewrite host files
# Again, some special handling because of the missing slash in the main folder
if not target_folder.is_root():
moved_subfolder = folder_tree().folder(f"{target_folder.path()}/{subfolder.name()}")
moved_subfolder = self.tree.folder(f"{target_folder.path()}/{subfolder.name()}")
else:
moved_subfolder = folder_tree().folder(subfolder.name())
moved_subfolder = self.tree.folder(subfolder.name())

# Do not update redis while rewriting a plethora of host files
# Redis automatically updates on the next request
Expand Down Expand Up @@ -2318,7 +2318,7 @@ def edit(self, new_title: str, new_attributes: HostAttributes) -> None:
# might need to be rewritten in order to reflect Changes
# in Nagios-relevant attributes.
self.save_folder_attributes()
folder_tree().invalidate_caches()
self.tree.invalidate_caches()
self.recursively_save_hosts()

affected_sites = list(set(affected_sites + self.all_site_ids()))
Expand Down Expand Up @@ -2457,7 +2457,7 @@ def _validate_delete_hosts(
errors.extend(_("%s is locked by Quick setup.") % host_name for host_name in hosts)

# 2. check if hosts have parents
if hosts_with_children := self._get_parents_of_hosts(host_names):
if hosts_with_children := self._get_parents_of_hosts(self.tree, host_names):
errors.extend(
_("%s is parent of %s.") % (parent, ", ".join(children))
for parent, children in sorted(hosts_with_children.items())
Expand All @@ -2478,11 +2478,13 @@ def _get_hosts_locked_by_quick_setup(host_names: Collection[HostName]) -> list[H
]

@staticmethod
def _get_parents_of_hosts(host_names: Collection[HostName]) -> dict[HostName, list[HostName]]:
def _get_parents_of_hosts(
tree: FolderTree, host_names: Collection[HostName]
) -> dict[HostName, list[HostName]]:
# Note: Deletion of chosen hosts which are parents
# is possible if and only if all children are chosen, too.
hosts_with_children: dict[HostName, list[HostName]] = {}
for child_key, child in folder_tree().root_folder().all_hosts_recursively().items():
for child_key, child in tree.root_folder().all_hosts_recursively().items():
for host_name in host_names:
if host_name in child.parents():
hosts_with_children.setdefault(host_name, [])
Expand Down Expand Up @@ -2537,8 +2539,8 @@ def move_hosts(self, host_names: Collection[HostName], target_folder: Folder) ->
target_folder._add_host(host)

affected_sites = list(set(affected_sites + [host.site_id()]))
old_folder_text = self.path() or folder_tree().root_folder().title()
new_folder_text = target_folder.path() or folder_tree().root_folder().title()
old_folder_text = self.path() or self.tree.root_folder().title()
new_folder_text = target_folder.path() or self.tree.root_folder().title()
add_change(
"move-host",
_l('Moved host from "%s" (ID: %s) to "%s" (ID: %s)')
Expand Down Expand Up @@ -2887,7 +2889,7 @@ def hosts(self) -> Mapping[HostName, Host]:
return self._found_hosts

def host_validation_errors(self) -> dict[HostName, list[str]]:
return validate_all_hosts(list(self.hosts().keys()))
return validate_all_hosts(self.tree, list(self.hosts().keys()))

def load_host(self, host_name: HostName) -> Host:
try:
Expand Down Expand Up @@ -3547,7 +3549,7 @@ def call_hook_hosts_changed(folder: Folder) -> None:

# The same with all hosts!
if hooks.registered("all-hosts-changed"):
hosts = _collect_hosts(folder_tree().root_folder())
hosts = _collect_hosts(folder.tree.root_folder())
hooks.call("all-hosts-changed", hosts)


Expand All @@ -3556,11 +3558,11 @@ def call_hook_hosts_changed(folder: Folder) -> None:
# symbols in the host list and the host detail view
# Returns dictionary { hostname: [errors] }
def validate_all_hosts(
hostnames: Sequence[HostName], force_all: bool = False
tree: FolderTree, hostnames: Sequence[HostName], force_all: bool = False
) -> dict[HostName, list[str]]:
if hooks.registered("validate-all-hosts") and (len(hostnames) > 0 or force_all):
hosts_errors: dict[HostName, list[str]] = {}
all_hosts = _collect_hosts(folder_tree().root_folder())
all_hosts = _collect_hosts(tree.root_folder())

if force_all:
hostnames = list(all_hosts.keys())
Expand Down

0 comments on commit 54b1674

Please sign in to comment.