From c7b7e8f6f85646fc1a5ef5cc098af3ba3def76c1 Mon Sep 17 00:00:00 2001 From: Linus Vogel Date: Sun, 3 May 2026 21:47:56 +0200 Subject: [PATCH] improved host cli --- pillar_tool/ptcli/cli/host.py | 10 ++++++---- pillar_tool/routers/host.py | 28 ++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/pillar_tool/ptcli/cli/host.py b/pillar_tool/ptcli/cli/host.py index 0974315..c7fa434 100644 --- a/pillar_tool/ptcli/cli/host.py +++ b/pillar_tool/ptcli/cli/host.py @@ -21,7 +21,7 @@ def host_list(): for h in response.json(): click.echo(f" - {h}") except requests.exceptions.HTTPError as e: - raise click.ClickException(f"Failed to list hosts:\n{e}") + raise click.ClickException(f"Failed to list hosts:\n{e.response.text}") @host.command("create") @click.argument("fqdn") @@ -35,7 +35,7 @@ def host_create(fqdn: str, parent: str | None): response = requests.post(f"{base_url()}/host/{fqdn}", json=params.model_dump(), headers=auth_header()) response.raise_for_status() except requests.exceptions.HTTPError as e: - raise click.ClickException(f"Failed to create host:\n{e}") + raise click.ClickException(f"Failed to create host:\n{e.response.text}") click.echo(f"Host '{fqdn}' created!") @@ -51,7 +51,9 @@ def host_delete(fqdn: str): click.echo("Deleting host...") try: - response = requests.delete(f'{base_url}/host/{fqdn}', headers=auth_header()) + response = requests.delete(f'{base_url()}/host/{fqdn}', headers=auth_header()) response.raise_for_status() + + click.echo(f"Host '{fqdn}' deleted!") except requests.exceptions.HTTPError as e: - raise click.ClickException(f"Failed to delete host:\n{e}") + raise click.ClickException(f"Failed to delete host:\n{e.response.text}") diff --git a/pillar_tool/routers/host.py b/pillar_tool/routers/host.py index 5d0bcaa..2d60ace 100644 --- a/pillar_tool/routers/host.py +++ b/pillar_tool/routers/host.py @@ -1,6 +1,6 @@ import uuid -from sqlalchemy import select, insert, bindparam, and_ +from sqlalchemy import select, insert, bindparam, and_, delete from sqlalchemy.orm import Session from starlette.exceptions import HTTPException from starlette.requests import Request @@ -153,12 +153,17 @@ async def host_add(request: Request, fqdn: str, params: HostCreateParams): is_hostgroup=False ) stmt_create_host_with_parent = insert(Host).values(id=new_host.id, name=new_host.name, parent_id=new_host.parent_id, is_hostgroup=new_host.is_hostgroup) - db.execute(stmt_create_host_with_parent).fetchall() + db.execute(stmt_create_host_with_parent) # Prepare response with creation details output = { "message": "Host created", - "host": new_host, # return the final host in the hierarchy + "host": { + 'id': str(new_host.id), + 'name': new_host.name, + 'parent': str(new_host.parent_id), + 'is_hostgroup': new_host.is_hostgroup + }, # return the final host in the hierarchy } # include the full path to the new host if it exists @@ -189,6 +194,21 @@ async def host_delete(request: Request, fqdn: str): Raises: HTTPException: If FQDN format is invalid or host is not found """ - pass + + db: Session = request.state.db + + if not validate_fqdn(fqdn): + raise HTTPException(status_code=400, detail="Provided host is not an FQDN") + + host_stmt = select(Host).where(and_(Host.name == fqdn, Host.is_hostgroup == False)) + host_res = db.execute(host_stmt).fetchall() + if len(host_res) != 1: + raise HTTPException(status_code=400, detail="Host not found") + host: Host = host_res[0][0] + + db.execute(delete(Host).where(Host.id == host.id)) + + return JSONResponse(status_code=204, content={"message": "Host deleted"}) +