import click import requests from .cli_main import main, auth_header, base_url @main.group("top") def top(): pass @top.command("get") @click.argument("host") def top_get(host: str): click.echo("Querying top for host...") try: response = requests.get(f'{base_url()}/top/query/{host}', headers=auth_header()) response.raise_for_status() click.echo("Top:") click.echo(response.json()) except requests.exceptions.HTTPError as e: raise click.ClickException(f"Failed to query top:\n{e}") @top.command("setenv") @click.argument("host") @click.argument("environment") def top_setenv(host: str, environment: str): click.echo("Assigning environment to host...") try: response = requests.post(f'{base_url()}/top/setenv/{host}/{environment}', headers=auth_header()) response.raise_for_status() click.echo("Assigned environment") except requests.exceptions.HTTPError as e: raise click.ClickException(f"Failed to assign environment:\n{e}") @top.command("assign") @click.argument("host") @click.argument("state") def top_assign(host: str, state: str): click.echo("Assigning state to host...") try: response = requests.post(f'{base_url()}/top/assign/{host}/{state}', headers=auth_header()) response.raise_for_status() click.echo("Assigned state") except requests.exceptions.HTTPError as e: raise click.ClickException(f"Failed to assign state:\n{e}") @top.command("unassign") @click.argument("host") @click.argument("state") def top_assign(host: str, state: str): click.echo("Assigning state to host...") try: response = requests.delete(f'{base_url()}/top/assign/{host}/{state}', headers=auth_header()) response.raise_for_status() click.echo("Assigned state") except requests.exceptions.HTTPError as e: click.echo(f"Failed to assign state:\n{response.text}")