Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stack delete confirmation prompt #548

Merged
merged 5 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions src/zenml/cli/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,33 +564,45 @@ def describe_stack(stack_name: Optional[str]) -> None:

@stack.command("delete")
@click.argument("stack_name", type=str)
def delete_stack(stack_name: str) -> None:
@click.option("--yes", "-y", is_flag=True, required=False)
def delete_stack(stack_name: str, yes: bool = False) -> None:
"""Delete a stack."""
cli_utils.print_active_profile()
if yes:
confirmation = True
else:
confirmation = cli_utils.confirmation(
f"This will delete stack '{stack_name}' from your repository. \n"
"Are you sure you want to proceed?"
)

with console.status(f"Deleting stack '{stack_name}'...\n"):
cfg = GlobalConfiguration()
repo = Repository()

if cfg.active_stack_name == stack_name:
cli_utils.error(
f"Stack {stack_name} cannot be deleted while it is globally "
f"active. Please choose a different active global stack first "
f"by running 'zenml stack set --global STACK'."
)
return

if repo.active_stack_name == stack_name:
cli_utils.error(
f"Stack {stack_name} cannot be deleted while it is "
f"active. Please choose a different active stack first by "
f"running 'zenml stack set STACK'."
)
return
if confirmation:
with console.status(f"Deleting stack '{stack_name}'...\n"):
cfg = GlobalConfiguration()
repo = Repository()

if cfg.active_stack_name == stack_name:
cli_utils.error(
f"Stack {stack_name} cannot be deleted while it is globally "
f"active. Please choose a different active global stack first "
f"by running 'zenml stack set --global STACK'."
)
return

if repo.active_stack_name == stack_name:
cli_utils.error(
f"Stack {stack_name} cannot be deleted while it is "
f"active. Please choose a different active stack first by "
f"running 'zenml stack set STACK'."
)
return

Repository().deregister_stack(stack_name)
cli_utils.declare(f"Deleted stack '{stack_name}'.")

else:
cli_utils.declare("Stack deletion cancelled.")


@stack.command("set")
@click.argument("stack_name", type=str)
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/cli/test_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from zenml.artifact_stores import LocalArtifactStore
from zenml.cli.stack import (
delete_stack,
describe_stack,
remove_stack_component,
rename_stack,
Expand Down Expand Up @@ -203,3 +204,19 @@ def test_remove_non_core_component_from_stack_succeeds(clean_repo) -> None:
)
assert result.exit_code == 0
assert clean_repo.active_stack.secrets_manager is None


def test_deleting_stack_with_flag_succeeds(clean_repo) -> None:
"""Test stack delete with flag succeeds."""
new_stack = Stack(
name="arias_new_stack",
orchestrator=clean_repo.active_stack.orchestrator,
metadata_store=clean_repo.active_stack.metadata_store,
artifact_store=clean_repo.active_stack.artifact_store,
)
clean_repo.register_stack(new_stack)
runner = CliRunner()
result = runner.invoke(delete_stack, ["arias_new_stack", "-y"])
assert result.exit_code == 0
with pytest.raises(KeyError):
clean_repo.get_stack("arias_new_stack")