refactor of hostgroup router

This commit is contained in:
Linus Vogel 2026-05-14 19:50:05 +02:00
parent 03546a9d60
commit b0099d9d71

View File

@ -43,7 +43,7 @@ def hostgroups_get(req: Request):
ancestors = [host] ancestors = [host]
while ancestors[-1].parent_id is not None: while ancestors[-1].parent_id is not None:
ancestors.append(all_hostgroups[ancestors[-1].parent_id]) ancestors.append(all_hostgroups[ancestors[-1].parent_id])
all_hostgroup_names.append('/'.join(map(lambda x: x.name, reversed(ancestors)))) all_hostgroup_names.append('/'.join(map(lambda x: x.name, reversed(ancestors)))) # type: ignore
print("[DEBUG] hostgroups_get: resolved hierarchical names for {} host group(s): {}".format(len(all_hostgroup_names), all_hostgroup_names)) print("[DEBUG] hostgroups_get: resolved hierarchical names for {} host group(s): {}".format(len(all_hostgroup_names), all_hostgroup_names))
@ -73,9 +73,9 @@ def hostgroup_get(req: Request, name: str, params: HostgroupParams):
# decode the path # decode the path
last = None last = None
ancestors = [] ancestors = []
path = [] path: list[str] = []
if params: if params:
path = split_and_validate_path(params.path) if params.path else [] path = (split_and_validate_path(params.path) or []) if params.path else []
if len(path) > 0: if len(path) > 0:
print("[DEBUG] hostgroup_get: resolving parent path with {} segment(s): {}".format(len(path), path)) print("[DEBUG] hostgroup_get: resolving parent path with {} segment(s): {}".format(len(path), path))
@ -184,7 +184,7 @@ def hostgroup_delete(req: Request, name: str, params: HostgroupParams = Depends(
""" """
db = req.state.db db = req.state.db
labels = split_and_validate_path(params.path) or [] labels = (split_and_validate_path(params.path) or []) if params.path is not None else []
labels.append(name) labels.append(name)
last = None last = None
@ -208,10 +208,10 @@ def hostgroup_delete(req: Request, name: str, params: HostgroupParams = Depends(
children_stmt = select(Host).where(Host.parent_id == last) children_stmt = select(Host).where(Host.parent_id == last)
children: list[Host] = list(map(lambda x: x[0], db.execute(children_stmt).fetchall())) children: list[Host] = list(map(lambda x: x[0], db.execute(children_stmt).fetchall()))
if len(children) != 0: if len(children) != 0:
print("[DEBUG] hostgroup_delete: ERROR - Cannot delete hostgroup '{}' because it has {} child(ren): {}. All children must be deleted first.".format(labels[-1], len(children), [ '/'.join(labels + [x.name]) for x in children ])) print("[DEBUG] hostgroup_delete: ERROR - Cannot delete hostgroup '{}' because it has {} child(ren): {}. All children must be deleted first.".format(labels[-1], len(children), [ '/'.join(labels + [x.name]) for x in children ])) # type: ignore
return JSONResponse(status_code=400, content={ return JSONResponse(status_code=400, content={
'message': "Cannot delete a hostgroup that still has children", 'message': "Cannot delete a hostgroup that still has children",
'children': [ '/'.join(labels + [x.name]) for x in children ] 'children': [ '/'.join(labels + [x.name]) for x in children ] # type: ignore
}) })
print("[DEBUG] hostgroup_delete: deleting hostgroup '{}' (id={}) with no remaining children".format(labels[-1], last)) print("[DEBUG] hostgroup_delete: deleting hostgroup '{}' (id={}) with no remaining children".format(labels[-1], last))