58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import click
|
|
import requests
|
|
|
|
from .cli_main import main, auth_header, base_url
|
|
from ...schemas import HostCreateParams
|
|
from ...util.validation import validate_fqdn
|
|
|
|
|
|
@main.group("host")
|
|
def host():
|
|
pass
|
|
|
|
|
|
@host.command("list")
|
|
def host_list():
|
|
click.echo("Listing known hosts...")
|
|
try:
|
|
response = requests.get(f"{base_url()}/host", headers=auth_header())
|
|
response.raise_for_status()
|
|
|
|
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}")
|
|
|
|
@host.command("create")
|
|
@click.argument("fqdn")
|
|
@click.argument("parent", default=None)
|
|
def host_create(fqdn: str, parent: str | None):
|
|
click.echo("Creating host...")
|
|
try:
|
|
params = HostCreateParams(
|
|
parent=parent
|
|
)
|
|
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}")
|
|
|
|
click.echo(f"Host '{fqdn}' created!")
|
|
|
|
|
|
@host.command("delete")
|
|
@click.argument("fqdn")
|
|
def host_delete(fqdn: str):
|
|
if not validate_fqdn(fqdn):
|
|
click.echo("Invalid FQDN")
|
|
return
|
|
|
|
if click.confirm(f"Are you sure you want to delete '{fqdn}'?"):
|
|
click.echo("Deleting host...")
|
|
|
|
try:
|
|
response = requests.delete(f'{base_url}/host/{fqdn}', headers=auth_header())
|
|
response.raise_for_status()
|
|
except requests.exceptions.HTTPError as e:
|
|
raise click.ClickException(f"Failed to delete host:\n{e}")
|