improved environment cli
This commit is contained in:
parent
4b5b9cfe6a
commit
8f8baeabe4
@ -22,7 +22,7 @@ def environment_list():
|
|||||||
for env in response.json():
|
for env in response.json():
|
||||||
click.echo(f" - {env}")
|
click.echo(f" - {env}")
|
||||||
except requests.exceptions.HTTPError as e:
|
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")
|
@environment.command("show")
|
||||||
@ -33,10 +33,9 @@ def environment_show(name: str):
|
|||||||
try:
|
try:
|
||||||
# Validate name format before making request
|
# Validate name format before making request
|
||||||
if not split_and_validate_path.__module__ or True: # Using environment-specific validation
|
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):
|
if not validate_environment_name(name):
|
||||||
raise click.ClickException(
|
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
||||||
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
return
|
||||||
|
|
||||||
response = requests.get(f'{base_url()}/environment/{name}', headers=auth_header())
|
response = requests.get(f'{base_url()}/environment/{name}', headers=auth_header())
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
@ -45,7 +44,7 @@ def environment_show(name: str):
|
|||||||
click.echo(f"Environment: {env_data['environment']}")
|
click.echo(f"Environment: {env_data['environment']}")
|
||||||
click.echo(f"Hosts assigned: {env_data['host_count']}")
|
click.echo(f"Hosts assigned: {env_data['host_count']}")
|
||||||
except requests.exceptions.HTTPError as e:
|
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")
|
@environment.command("create")
|
||||||
@ -55,8 +54,8 @@ def environment_create(name: str):
|
|||||||
click.echo(f"Creating environment '{name}'...")
|
click.echo(f"Creating environment '{name}'...")
|
||||||
try:
|
try:
|
||||||
if not validate_environment_name(name):
|
if not validate_environment_name(name):
|
||||||
raise click.ClickException(
|
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
||||||
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
return
|
||||||
|
|
||||||
# No body data needed for environment creation
|
# No body data needed for environment creation
|
||||||
response = requests.post(f'{base_url()}/environment/{name}', headers=auth_header())
|
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.")
|
click.echo(f"Environment '{name}' created successfully.")
|
||||||
except requests.exceptions.HTTPError as e:
|
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")
|
@environment.command("delete")
|
||||||
@ -74,8 +73,8 @@ def environment_delete(name: str):
|
|||||||
click.echo(f"Deleting environment '{name}'...")
|
click.echo(f"Deleting environment '{name}'...")
|
||||||
try:
|
try:
|
||||||
if not validate_environment_name(name):
|
if not validate_environment_name(name):
|
||||||
raise click.ClickException(
|
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
||||||
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
return
|
||||||
|
|
||||||
response = requests.delete(f'{base_url()}/environment/{name}', headers=auth_header())
|
response = requests.delete(f'{base_url()}/environment/{name}', headers=auth_header())
|
||||||
response.raise_for_status()
|
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:
|
if e.response is not None and e.response.status_code == 409:
|
||||||
conflict_data = e.response.json() if e.response.content else {}
|
conflict_data = e.response.json() if e.response.content else {}
|
||||||
hosts_list = conflict_data.get('assigned_hosts', [])
|
hosts_list = conflict_data.get('assigned_hosts', [])
|
||||||
raise click.ClickException(
|
click.echo(
|
||||||
f"Failed to delete environment:\n"
|
f"Failed to delete environment:\n"
|
||||||
f"{conflict_data.get('message', 'Environment has assigned hosts')}\n"
|
f"{conflict_data.get('message', 'Environment has assigned hosts')}\n"
|
||||||
f"Assigned hosts: {', '.join(hosts_list) if hosts_list else 'none'}"
|
f"Assigned hosts: {', '.join(hosts_list) if hosts_list else 'none'}"
|
||||||
)
|
)
|
||||||
else:
|
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")
|
@environment.command("import")
|
||||||
@click.argument("name")
|
@click.argument("name")
|
||||||
@ -101,8 +100,8 @@ def environment_import(name: str):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if not validate_environment_name(name):
|
if not validate_environment_name(name):
|
||||||
raise click.ClickException(
|
click.echo("Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
||||||
"Invalid environment name. Use only alphanumeric, underscore or dash characters.")
|
return
|
||||||
|
|
||||||
response = requests.patch(f'{base_url()}/environment/{name}', headers=auth_header())
|
response = requests.patch(f'{base_url()}/environment/{name}', headers=auth_header())
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
@ -110,7 +109,7 @@ def environment_import(name: str):
|
|||||||
click.echo(f"Environment '{name}' imported successfully.")
|
click.echo(f"Environment '{name}' imported successfully.")
|
||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
if e.response is not None:
|
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:
|
else:
|
||||||
raise click.ClickException(f"Failed to import environment:\n{e}")
|
raise click.ClickException(f"Failed to import environment:\n{e}")
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user