improved host cli

This commit is contained in:
Linus Vogel 2026-05-03 21:47:56 +02:00
parent 8f8baeabe4
commit c7b7e8f6f8
2 changed files with 30 additions and 8 deletions

View File

@ -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}")

View File

@ -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"})