refactor of hostgroup router
This commit is contained in:
parent
03546a9d60
commit
b0099d9d71
@ -43,7 +43,7 @@ def hostgroups_get(req: Request):
|
||||
ancestors = [host]
|
||||
while ancestors[-1].parent_id is not None:
|
||||
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))
|
||||
|
||||
@ -73,9 +73,9 @@ def hostgroup_get(req: Request, name: str, params: HostgroupParams):
|
||||
# decode the path
|
||||
last = None
|
||||
ancestors = []
|
||||
path = []
|
||||
path: list[str] = []
|
||||
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:
|
||||
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
|
||||
|
||||
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)
|
||||
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: list[Host] = list(map(lambda x: x[0], db.execute(children_stmt).fetchall()))
|
||||
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={
|
||||
'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))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user