improved environment cli

This commit is contained in:
Linus Vogel 2026-05-03 21:47:42 +02:00
parent 4b5b9cfe6a
commit 8f8baeabe4

View File

@ -22,7 +22,7 @@ def environment_list():
for env in response.json():
click.echo(f" - {env}")
except requests.exceptions.HTTPError as e:
raise click.ClickException(f"Failed to list environments:\n{e}")
click.echo(f"Failed to list environments:\n{e.response.text}")
@environment.command("show")
@ -33,10 +33,9 @@ def environment_show(name: str):
try:
# Validate name format before making request
if not split_and_validate_path.__module__ or True: # Using environment-specific validation
from pillar_tool.util.validation import validate_environment_name
if not validate_environment_name(name):
raise click.ClickException(
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
return
response = requests.get(f'{base_url()}/environment/{name}', headers=auth_header())
response.raise_for_status()
@ -45,7 +44,7 @@ def environment_show(name: str):
click.echo(f"Environment: {env_data['environment']}")
click.echo(f"Hosts assigned: {env_data['host_count']}")
except requests.exceptions.HTTPError as e:
raise click.ClickException(f"Failed to show environment:\n{e}")
click.echo(f"Failed to show environment:\n{e.response.text}")
@environment.command("create")
@ -55,8 +54,8 @@ def environment_create(name: str):
click.echo(f"Creating environment '{name}'...")
try:
if not validate_environment_name(name):
raise click.ClickException(
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
return
# No body data needed for environment creation
response = requests.post(f'{base_url()}/environment/{name}', headers=auth_header())
@ -64,7 +63,7 @@ def environment_create(name: str):
click.echo(f"Environment '{name}' created successfully.")
except requests.exceptions.HTTPError as e:
raise click.ClickException(f"Failed to create environment:\n{e}")
click.echo(f"Failed to create environment:\n{e.response.text}")
@environment.command("delete")
@ -74,8 +73,8 @@ def environment_delete(name: str):
click.echo(f"Deleting environment '{name}'...")
try:
if not validate_environment_name(name):
raise click.ClickException(
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
return
response = requests.delete(f'{base_url()}/environment/{name}', headers=auth_header())
response.raise_for_status()
@ -85,13 +84,13 @@ def environment_delete(name: str):
if e.response is not None and e.response.status_code == 409:
conflict_data = e.response.json() if e.response.content else {}
hosts_list = conflict_data.get('assigned_hosts', [])
raise click.ClickException(
click.echo(
f"Failed to delete environment:\n"
f"{conflict_data.get('message', 'Environment has assigned hosts')}\n"
f"Assigned hosts: {', '.join(hosts_list) if hosts_list else 'none'}"
)
else:
raise click.ClickException(f"Failed to delete environment:\n{e}")
click.echo(f"Failed to delete environment:\n{e.response.text}")
@environment.command("import")
@click.argument("name")
@ -101,8 +100,8 @@ def environment_import(name: str):
try:
if not validate_environment_name(name):
raise click.ClickException(
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
return
response = requests.patch(f'{base_url()}/environment/{name}', headers=auth_header())
response.raise_for_status()
@ -110,7 +109,7 @@ def environment_import(name: str):
click.echo(f"Environment '{name}' imported successfully.")
except requests.exceptions.HTTPError as e:
if e.response is not None:
raise click.ClickException(f"Failed to import environment:\n{e}")
raise click.ClickException(f"Failed to import environment:\n{e.response.text}")
else:
raise click.ClickException(f"Failed to import environment:\n{e}")