Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9c53a6f
Added --s3-bucket and --s3-prefix flags to sam delete
lucashuy May 25, 2022
9f5f0ca
Added and fixed tests for --s3 flags for sam delete
lucashuy May 25, 2022
36a522e
Added integration tests and cleaned up format and log messages
lucashuy May 27, 2022
d6f79a1
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into s3_…
lucashuy May 27, 2022
a52bee0
Merge branch 'develop' into s3_delete
mildaniel May 27, 2022
86dec31
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into s3_…
lucashuy May 27, 2022
cbe55c8
Merge branch 'develop' into s3_delete
mndeveci May 30, 2022
4ebb1ea
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into s3_…
lucashuy May 30, 2022
c168ef5
Merge branch 's3_delete' of github.com:lucashuy/aws-sam-cli into s3_d…
lucashuy May 30, 2022
ab594d9
Added type hinting and fetch region from boto3 session
lucashuy May 30, 2022
916c38d
Expanded arguments
lucashuy May 30, 2022
afac2d0
Merge branch 'develop' into s3_delete
lucashuy May 31, 2022
11666b6
Merge branch 'develop' into s3_delete
lucashuy May 31, 2022
33b9830
Merge branch 'develop' into s3_delete
mndeveci May 31, 2022
331d2d8
Merge branch 'develop' into s3_delete
lucashuy Jun 1, 2022
75fd419
Merge branch 'develop' into s3_delete
lucashuy Jun 3, 2022
fd328c7
Merge branch 'develop' into s3_delete
lucashuy Jun 6, 2022
c7ba02b
Merge branch 'develop' into s3_delete
lucashuy Jun 7, 2022
bbac979
Merge branch 'develop' of https://github.com/aws/aws-sam-cli into s3_…
lucashuy Jun 8, 2022
66b1a46
Reverted warning message back to only warn on missing bucket info
lucashuy Jun 8, 2022
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
32 changes: 30 additions & 2 deletions samcli/commands/delete/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging

from typing import Optional
import click
from samcli.cli.main import aws_creds_options, common_options, pass_context, print_cmdline_args

Expand Down Expand Up @@ -63,12 +64,26 @@
is_flag=True,
required=False,
)
@click.option(
"--s3-bucket",
help=("The S3 bucket path you want to delete."),
type=click.STRING,
default=None,
required=False,
)
@click.option(
"--s3-prefix",
help=("The S3 prefix you want to delete"),
type=click.STRING,
default=None,
required=False,
)
@aws_creds_options
@common_options
@pass_context
@check_newer_version
@print_cmdline_args
def cli(ctx, stack_name: str, config_file: str, config_env: str, no_prompts: bool):
def cli(ctx, stack_name: str, config_file: str, config_env: str, no_prompts: bool, s3_bucket: str, s3_prefix: str):
"""
`sam delete` command entry point
"""
Expand All @@ -81,10 +96,21 @@ def cli(ctx, stack_name: str, config_file: str, config_env: str, no_prompts: boo
config_env=config_env,
profile=ctx.profile,
no_prompts=no_prompts,
s3_bucket=s3_bucket,
s3_prefix=s3_prefix,
) # pragma: no cover


def do_cli(stack_name: str, region: str, config_file: str, config_env: str, profile: str, no_prompts: bool):
def do_cli(
stack_name: str,
region: str,
config_file: str,
config_env: str,
profile: str,
no_prompts: bool,
s3_bucket: Optional[str],
s3_prefix: Optional[str],
):
"""
Implementation of the ``cli`` method
"""
Expand All @@ -97,5 +123,7 @@ def do_cli(stack_name: str, region: str, config_file: str, config_env: str, prof
config_file=config_file,
config_env=config_env,
no_prompts=no_prompts,
s3_bucket=s3_bucket,
s3_prefix=s3_prefix,
) as delete_context:
delete_context.run()
41 changes: 29 additions & 12 deletions samcli/commands/delete/delete_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import logging

import json
import boto3

from typing import Optional

import boto3

import click
from click import confirm
Expand Down Expand Up @@ -36,15 +38,25 @@

class DeleteContext:
# TODO: Separate this context into 2 separate contexts guided and non-guided, just like deploy.
def __init__(self, stack_name: str, region: str, profile: str, config_file: str, config_env: str, no_prompts: bool):
def __init__(
self,
stack_name: str,
region: str,
profile: str,
config_file: str,
config_env: str,
no_prompts: bool,
s3_bucket: Optional[str],
s3_prefix: Optional[str],
):
self.stack_name = stack_name
self.region = region
self.profile = profile
self.config_file = config_file
self.config_env = config_env
self.no_prompts = no_prompts
self.s3_bucket = None
self.s3_prefix = None
self.s3_bucket = s3_bucket
self.s3_prefix = s3_prefix
self.cf_utils = None
self.s3_uploader = None
self.ecr_uploader = None
Expand Down Expand Up @@ -95,8 +107,10 @@ def parse_config_file(self):
self.region = config_options.get("region", None)
if not self.profile:
self.profile = config_options.get("profile", None)
self.s3_bucket = config_options.get("s3_bucket", None)
self.s3_prefix = config_options.get("s3_prefix", None)
if not self.s3_bucket:
self.s3_bucket = config_options.get("s3_bucket", None)
if not self.s3_prefix:
self.s3_prefix = config_options.get("s3_prefix", None)

def init_clients(self):
"""
Expand Down Expand Up @@ -142,8 +156,9 @@ def s3_prompts(self):
Guided prompts asking user to delete s3 artifacts
"""
# Note: s3_bucket and s3_prefix information is only
# available if a local toml file is present or if
# this information is obtained from the template resources and so if this
# available if it is provided as an option flag, a
# local toml file or if this information is obtained
# from the template resources and so if this
# information is not found, warn the user that S3 artifacts
# will need to be manually deleted.

Expand Down Expand Up @@ -319,12 +334,14 @@ def delete(self):
self.cf_utils.delete_stack(stack_name=self.stack_name, retain_resources=retain_resources)
self.cf_utils.wait_for_delete(self.stack_name)

# If s3_bucket information is not available, warn the user
# Warn the user that s3 information is missing and to use --s3 options
if not self.s3_bucket:
LOG.debug("Cannot delete s3 files as no s3_bucket found")
LOG.debug("Cannot delete s3 objects as bucket is missing")
click.secho(
"\nWarning: s3_bucket and s3_prefix information could not be obtained from local config file"
" or cloudformation template, delete the s3 files manually if required",
"\nWarning: Cannot resolve s3 bucket information from command options"
" , local config file or cloudformation template. Please use"
" --s3-bucket next time and"
" delete s3 files manually if required.",
fg="yellow",
)

Expand Down
25 changes: 19 additions & 6 deletions tests/integration/delete/delete_integ_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from pathlib import Path
from typing import Optional
from unittest import TestCase


Expand All @@ -22,21 +23,33 @@ def base_command(self):
return command

def get_delete_command_list(
self, stack_name=None, region=None, config_file=None, config_env=None, profile=None, no_prompts=None
self,
stack_name: Optional[str] = None,
region: Optional[str] = None,
config_file: Optional[str] = None,
config_env: Optional[str] = None,
profile: Optional[str] = None,
no_prompts: Optional[bool] = None,
s3_bucket: Optional[str] = None,
s3_prefix: Optional[str] = None,
):
command_list = [self.base_command(), "delete"]

if stack_name:
command_list += ["--stack-name", str(stack_name)]
command_list += ["--stack-name", stack_name]
if region:
command_list += ["--region", str(region)]
command_list += ["--region", region]
if config_file:
command_list += ["--config-file", str(config_file)]
command_list += ["--config-file", config_file]
if config_env:
command_list += ["--config-env", str(config_env)]
command_list += ["--config-env", config_env]
if profile:
command_list += ["--profile", str(profile)]
command_list += ["--profile", profile]
if no_prompts:
command_list += ["--no-prompts"]
if s3_bucket:
command_list += ["--s3-bucket", s3_bucket]
if s3_prefix:
command_list += ["--s3-prefix", s3_prefix]

return command_list
Loading