Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions samcli/commands/delete/delete_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from click import confirm
from click import prompt

from samcli.lib.utils.hash import str_checksum
from samcli.cli.cli_config_file import TomlProvider
from samcli.lib.utils.botoconfig import get_boto_config_with_user_agent
from samcli.lib.delete.cfn_utils import CfnUtils
Expand All @@ -26,6 +25,7 @@
from samcli.lib.package.artifact_exporter import Template
from samcli.lib.package.ecr_uploader import ECRUploader
from samcli.lib.package.uploaders import Uploaders
from samcli.lib.bootstrap.companion_stack.companion_stack_builder import CompanionStack

CONFIG_COMMAND = "deploy"
CONFIG_SECTION = "parameters"
Expand Down Expand Up @@ -288,12 +288,12 @@ def delete(self):
retain_resources = self.ecr_repos_prompts(template)

# ECR companion stack delete prompts, if it exists
parent_stack_hash = str_checksum(self.stack_name)
possible_companion_stack_name = f"{self.stack_name[:104]}-{parent_stack_hash[:8]}-CompanionStack"
ecr_companion_stack_exists = self.cf_utils.has_stack(stack_name=possible_companion_stack_name)
companion_stack = CompanionStack(self.stack_name)

ecr_companion_stack_exists = self.cf_utils.has_stack(stack_name=companion_stack.stack_name)
if ecr_companion_stack_exists:
LOG.debug("ECR Companion stack found for the input stack")
self.companion_stack_name = possible_companion_stack_name
self.companion_stack_name = companion_stack.stack_name
self.delete_ecr_companion_stack()

# Delete the artifacts and retain resources user selected not to delete
Expand Down
13 changes: 8 additions & 5 deletions tests/unit/commands/delete/test_delete_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from samcli.lib.bootstrap.companion_stack.data_types import CompanionStack
from unittest import TestCase
from unittest.mock import patch, call, MagicMock

Expand Down Expand Up @@ -294,6 +295,7 @@ def test_guided_prompts_s3_bucket_present_no_prefix_execute_run(
@patch.object(S3Uploader, "delete_artifact", MagicMock())
@patch.object(ECRUploader, "delete_ecr_repository", MagicMock())
@patch.object(Template, "get_ecr_repos", MagicMock(side_effect=({}, {"logical_id": {"Repository": "test_id"}})))
@patch.object(CompanionStack, "stack_name", "Companion-Stack-Name")
def test_guided_prompts_ecr_companion_stack_present_execute_run(
self, patched_click_get_current_context, patched_confirm, patched_get_cf_template_name
):
Expand Down Expand Up @@ -339,7 +341,7 @@ def test_guided_prompts_ecr_companion_stack_present_execute_run(
call(
click.style(
"\tDo you you want to delete the ECR companion stack"
+ " test-098f6bcd-CompanionStack in the region us-east-1 ?",
+ " Companion-Stack-Name in the region us-east-1 ?",
bold=True,
),
default=False,
Expand Down Expand Up @@ -368,15 +370,16 @@ def test_guided_prompts_ecr_companion_stack_present_execute_run(
@patch.object(S3Uploader, "delete_prefix_artifacts", MagicMock())
@patch.object(ECRUploader, "delete_ecr_repository", MagicMock())
@patch.object(Template, "get_ecr_repos", MagicMock(return_value=({"logical_id": {"Repository": "test_id"}})))
@patch.object(CompanionStack, "stack_name", "Companion-Stack-Name")
def test_no_prompts_input_is_ecr_companion_stack_present_execute_run(
self, patched_click_get_current_context, patched_click_echo, patched_get_cf_template_name
):
CfnUtils.get_stack_template.return_value = {
"TemplateBody": {"Metadata": {"CompanionStackname": "test-098f6bcd-CompanionStack"}}
"TemplateBody": {"Metadata": {"CompanionStackname": "Companion-Stack-Name"}}
}
patched_get_cf_template_name.return_value = "hello.template"
with DeleteContext(
stack_name="test-098f6bcd-CompanionStack",
stack_name="Companion-Stack-Name",
region="us-east-1",
config_file="samconfig.toml",
config_env="default",
Expand All @@ -388,7 +391,7 @@ def test_no_prompts_input_is_ecr_companion_stack_present_execute_run(

delete_context.run()
expected_click_echo_calls = [
call("\t- Deleting Cloudformation stack test-098f6bcd-CompanionStack"),
call("\t- Deleting Cloudformation stack Companion-Stack-Name"),
call("\nDeleted successfully"),
]
self.assertEqual(expected_click_echo_calls, patched_click_echo.call_args_list)
Expand All @@ -403,7 +406,7 @@ def test_no_prompts_input_is_ecr_companion_stack_present_execute_run(
"wait_for_delete",
MagicMock(
side_effect=(
CfDeleteFailedStatusError("test-098f6bcd-CompanionStack", "Mock WaitError"),
CfDeleteFailedStatusError("Companion-Stack-Name", "Mock WaitError"),
{},
CfDeleteFailedStatusError("test", "Mock WaitError"),
{},
Expand Down