Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
d964112
Added the base commands of sam list and their corresponding help mess…
May 19, 2022
5cc31a5
Added no-args handling to base commands and added files for integrati…
May 24, 2022
70b3ba8
Made additions to resources integration tests
May 25, 2022
fc81ceb
Made additions to the sam list integration test suite
May 25, 2022
97ec32d
Added to sam list integration and unit tests
May 25, 2022
3e200f2
Added integration tests to test help messages of sam list commands
May 26, 2022
bcdf40b
Reformatted files
May 26, 2022
93f55cd
Cleaned up unfinished tests
May 26, 2022
60915f1
adding check to see what the appveyor test will produce. Trying to re…
May 27, 2022
a5418f6
Fixed test to check help messages
May 27, 2022
a3d150a
modified pattern matching for eliminating newlines when matching help…
May 27, 2022
5dff9e2
Changed the way whitespaces are handled in matching help messages
May 27, 2022
bafaea2
Addressed PR comments, moved items into common classes
May 27, 2022
8e084ff
Made modifications based on comments, removed relative import paths, …
May 31, 2022
719cfd5
Reformatted files
May 31, 2022
c7499e4
removed folder deletion
Jun 6, 2022
ee2015a
removed uneccessary folder creation and deletion
Jun 6, 2022
2c62cd9
fixed errors with cwd of integration tests
Jun 6, 2022
de4439b
Added implementation and tests for the stack-outputs command
Jun 8, 2022
ef1f273
Merge branch 'list-command' into base-command
Jun 8, 2022
08bf419
Added test skips for integration tests, added unit tests, removed red…
Jun 8, 2022
1358e61
commit to retrigger appveyor tests
Jun 8, 2022
cf5c191
Commmit to trigger appveyor
Jun 8, 2022
3aa02da
Modified client source, made fixes based on comments
Jun 9, 2022
5bc4247
Made fixes based on comments
Jun 14, 2022
ae4e2f0
Combined get_stack_info and stack_exists, and modified unit tests
Jun 16, 2022
38fc5f0
Empty-Commit
Jun 16, 2022
86166a7
Empty-Commit
Jun 16, 2022
3c8f6a1
Empty-Commit
Jun 16, 2022
3bf1d89
fixed tests based on comments
Jun 16, 2022
0b5e407
reformatted file
Jun 16, 2022
bf8e4f3
Refactored stack outputs command to the producer mapper consumer desi…
Jun 17, 2022
7740f30
Fixed merge conflicts
Jun 17, 2022
435abd7
Fixed formatting
Jun 17, 2022
7d0c721
Moved interfaces, made changes based on comments
Jun 21, 2022
68d5a02
Made fixes based on comments
Jun 23, 2022
57c199e
Made fixes based on comments
Jun 27, 2022
657f6cd
Empty commit
Jun 27, 2022
e374d52
Merge branch 'list-command' into base-command
Jun 29, 2022
5bebdbc
Made changes based on comments, added new exceptions
Jun 29, 2022
eb6639d
Fixed format
Jun 29, 2022
b088203
Fixed return type declaration
Jun 29, 2022
5367428
Fixed return type declaration
Jun 29, 2022
523915d
Changed return type to list
Jun 29, 2022
84382cf
Fixed error
Jun 29, 2022
c549ce6
Implementation of the local transform and resource collection
Jun 30, 2022
a820f04
Empty-Commit
Jun 30, 2022
6d35129
Merge branch 'list-command' into base-command
andrew-zhan139 Jun 30, 2022
defa233
Added section to avoid unused variable
Jun 30, 2022
282c914
Merge branch 'base-command' of github.com:andrew-zhan139/aws-sam-cli …
Jun 30, 2022
e98ac15
Refactored common code
Jun 30, 2022
562d2ad
Added tests, modified PR
Jun 30, 2022
c5d91eb
Fixed formatting
Jul 1, 2022
7fd3179
Made fixes based on PR comments
Jul 7, 2022
8d9a798
Fixed formatting
Jul 7, 2022
91a06a3
Fixed typing errors
Jul 7, 2022
3479342
Reverted typing
Jul 7, 2022
da1eda7
Fixed error with typing
Jul 7, 2022
4cd1506
Made changes to handling optional params
Jul 7, 2022
0851eb3
Fixes to typing errors
Jul 7, 2022
9de6afa
Made edits based on comments
Jul 11, 2022
26fb5f0
Fixed error
Jul 11, 2022
13c7edb
Changed return type
Jul 11, 2022
e7c17d2
Reverted return type due to make pr error
Jul 11, 2022
9ab5674
Added change to fix make pr error
Jul 11, 2022
c105195
Removed translate_utils.py file
Jul 12, 2022
502e463
Added cloud resources to sam list resources output
Jul 14, 2022
72b5a63
Merge branch 'list-command' into base-command
andrew-zhan139 Jul 14, 2022
a6e4f1a
Empty commit
Jul 14, 2022
0ce203c
modified test format
Jul 14, 2022
f9c0be0
Merge branch 'base-command' of github.com:andrew-zhan139/aws-sam-cli …
Jul 14, 2022
955c500
Modified tests
Jul 20, 2022
e9c6358
Modified test
Jul 21, 2022
ae421a8
Adding the sam list testable resources command, tests, and table outp…
Jul 26, 2022
23fa28a
Merge branch 'list-command' into base-command
andrew-zhan139 Jul 26, 2022
040e7df
Changed table and made changes based on pr comments
Aug 4, 2022
81002a6
Merge branch 'base-command' of github.com:andrew-zhan139/aws-sam-cli …
Aug 4, 2022
12273f2
Fixed integration test expected outputs
Aug 4, 2022
99ad642
Fixed table heading
Aug 9, 2022
db4a279
Added docstring and re-arranged the testable resources producer to re…
Aug 10, 2022
95d1758
Fixed format
Aug 10, 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
1 change: 1 addition & 0 deletions samcli/commands/list/cli_common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def stack_name_option(f):
def output_click_option():
return click.option(
"--output",
default="table",
help="Output the results from the command in a given " "output format (json or table). ",
type=click.Choice(["json", "table"], case_sensitive=False),
)
Expand Down
42 changes: 42 additions & 0 deletions samcli/commands/list/table_consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
The table consumer for 'sam list'
"""
from typing import Dict, Any
from samcli.lib.list.list_interfaces import ListInfoPullerConsumer
from samcli.commands._utils.table_print import pprint_column_names, pprint_columns


class StringConsumerTableOutput(ListInfoPullerConsumer):
"""
Outputs data in table format
"""

def consume(self, data: Dict[Any, Any]) -> None:
"""
Outputs the data in a table format
Parameters
----------
data: Dict[Any, Any]
The data to be outputted
"""

@pprint_column_names(
format_string=data["format_string"],
format_kwargs=data["format_args"],
table_header=data["table_name"],
)
def print_table_rows(**kwargs):
"""
Prints the rows of the table based on the data provided
"""
for entry in data["data"]:
pprint_columns(
columns=entry,
width=kwargs["width"],
margin=kwargs["margin"],
format_string=data["format_string"],
format_args=kwargs["format_args"],
columns_dict=data["format_args"].copy(),
)

print_table_rows()
16 changes: 13 additions & 3 deletions samcli/commands/list/testable_resources/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from samcli.cli.main import pass_context, common_options, aws_creds_options, print_cmdline_args
from samcli.lib.utils.version_checker import check_newer_version
from samcli.lib.telemetry.metric import track_command
from samcli.commands._utils.options import template_option_without_build
from samcli.cli.cli_config_file import configuration_option, TomlProvider


HELP_TEXT = """
Expand All @@ -19,22 +21,30 @@


@click.command(name="testable-resources", help=HELP_TEXT)
@configuration_option(provider=TomlProvider(section="parameters"))
@stack_name_option
@output_option
@template_option_without_build
@aws_creds_options
@common_options
@pass_context
@track_command
@check_newer_version
@print_cmdline_args
def cli(self, stack_name, output):
def cli(self, stack_name, output, template_file, config_file, config_env):
"""
`sam list testable-resources` command entry point
"""
do_cli(stack_name=stack_name, output=output, region=self.region, profile=self.profile)
do_cli(stack_name=stack_name, output=output, region=self.region, profile=self.profile, template_file=template_file)


def do_cli(stack_name, output, region, profile):
def do_cli(stack_name, output, region, profile, template_file):
"""
Implementation of the ``cli`` method
"""
from samcli.commands.list.testable_resources.testable_resources_context import TestableResourcesContext

with TestableResourcesContext(
stack_name=stack_name, output=output, region=region, profile=profile, template_file=template_file
) as testable_resources_context:
testable_resources_context.run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
Display of the Testable Resources of a SAM stack
"""
import logging
from typing import Optional

from samcli.commands.list.cli_common.list_common_context import ListContext
from samcli.lib.list.testable_resources.testable_resources_producer import TestableResourcesProducer
from samcli.lib.list.mapper_consumer_factory import MapperConsumerFactory
from samcli.lib.list.list_interfaces import ProducersEnum

LOG = logging.getLogger(__name__)


class TestableResourcesContext(ListContext):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets ensure that every class and function has docstrings as well as call parameters explained.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added docstrings

"""
Context class for testable resources
"""

def __init__(
self, stack_name: str, output: str, region: Optional[str], profile: Optional[str], template_file: Optional[str]
):
"""
Parameters
----------
stack_name: str
The name of the stack
output: str
The format of the output, either json or table
region: Optional[str]
The region of the stack
profile: Optional[str]
Optional profile to be used
template_file: Optional[str]
The location of the template file. If one is not specified, the default will be "template.yaml" in the CWD
"""
super().__init__()
self.stack_name = stack_name
self.output = output
self.region = region
self.profile = profile
self.template_file = template_file
self.iam_client = None
self.cloudcontrol_client = None
self.apigateway_client = None
self.apigatewayv2_client = None

def __enter__(self):
self.init_clients()
return self

def __exit__(self, *args):
pass

def init_clients(self) -> None:
"""
Initialize the clients being used by sam list.
"""
super().init_clients()
self.iam_client = self.client_provider("iam")
self.cloudcontrol_client = self.client_provider("cloudcontrol")
self.apigateway_client = self.client_provider("apigateway")
self.apigatewayv2_client = self.client_provider("apigatewayv2")

def run(self) -> None:
"""
Get the resources for a stack
"""
factory = MapperConsumerFactory()
container = factory.create(producer=ProducersEnum.TESTABLE_RESOURCES_PRODUCER, output=self.output)
testable_resource_producer = TestableResourcesProducer(
stack_name=self.stack_name,
region=self.region,
profile=self.profile,
template_file=self.template_file,
cloudformation_client=self.cloudformation_client,
iam_client=self.iam_client,
cloudcontrol_client=self.cloudcontrol_client,
apigateway_client=self.apigateway_client,
apigatewayv2_client=self.apigatewayv2_client,
mapper=container.mapper,
consumer=container.consumer,
)
testable_resource_producer.produce()
44 changes: 39 additions & 5 deletions samcli/lib/list/mapper_consumer_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,48 @@
from samcli.lib.list.list_interfaces import MapperConsumerFactoryInterface
from samcli.lib.list.data_to_json_mapper import DataToJsonMapper
from samcli.commands.list.json_consumer import StringConsumerJsonOutput
from samcli.commands.list.table_consumer import StringConsumerTableOutput
from samcli.lib.list.mapper_consumer_container import MapperConsumerContainer
from samcli.lib.list.list_interfaces import ProducersEnum
from samcli.lib.list.stack_outputs.stack_output_to_table_mapper import StackOutputToTableMapper
from samcli.lib.list.resources.resources_to_table_mapper import ResourcesToTableMapper
from samcli.lib.list.testable_resources.testable_resources_to_table_mapper import TestableResourcesToTableMapper
from samcli.lib.list.list_interfaces import ProducersEnum, Mapper


class MapperConsumerFactory(MapperConsumerFactoryInterface):
"""
Factory class to create factory objects that map a given producer and output format to a mapper and a consumer
"""

def create(self, producer: ProducersEnum, output: str) -> MapperConsumerContainer:
# Will add conditions here to return different sorts of containers later on
data_to_json_mapper = DataToJsonMapper()
json_consumer = StringConsumerJsonOutput()
container = MapperConsumerContainer(data_to_json_mapper, json_consumer)
"""
Creates a MapperConsumerContainer that contains the resulting mapper and consumer given
the producer and output format

Parameters
----------
producer: ProducersEnum
An enum representing the producers (stack-outputs, resources, or testable-resources producer)
output: str
The output format, either json or table

Returns
-------
container: MapperConsumerContainer
A MapperConsumerContainer containing the resulting mapper and consumer to be used by the producer
"""
if output == "json":

@hoffa hoffa Aug 10, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to think about: could we have this more generally across SAM CLI commands, instead of re-implementing for each? Similar to how AWS CLI has --output to specify the format for any time. I don't mean that we should implement it for all commands, but maybe building the foundation that would make adding output formats to commands trivial.

This would help tremendously in providing a consistent experience, and many commands would improve with parsable outputs (e.g. sam local, sam deploy --no-deploy-changeset, etc.).

data_to_json_mapper = DataToJsonMapper()
json_consumer = StringConsumerJsonOutput()
container = MapperConsumerContainer(data_to_json_mapper, json_consumer)
return container
table_mapper: Mapper
table_consumer = StringConsumerTableOutput()
if producer == ProducersEnum.STACK_OUTPUTS_PRODUCER:
table_mapper = StackOutputToTableMapper()
elif producer == ProducersEnum.RESOURCES_PRODUCER:
table_mapper = ResourcesToTableMapper()
elif producer == ProducersEnum.TESTABLE_RESOURCES_PRODUCER:
table_mapper = TestableResourcesToTableMapper()
container = MapperConsumerContainer(table_mapper, table_consumer)
return container
48 changes: 48 additions & 0 deletions samcli/lib/list/resources/resources_to_table_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Implementation of the resources to table mapper
"""
from typing import Dict, Any
from collections import OrderedDict
from samcli.lib.list.list_interfaces import Mapper


class ResourcesToTableMapper(Mapper):
"""
Mapper class for mapping resources data for table output
"""

def map(self, data: list) -> Dict[Any, Any]:
"""
Maps data to the format needed for consumption by the table consumer

Parameters
----------
data: list
List of dictionaries containing the entries of the resources data

Returns
-------
table_data: Dict[Any, Any]
Dictionary containing the information and data needed for the table
consumer to output the data in table format
"""
entry_list = []
for resource in data:
entry_list.append(
[
resource.get("LogicalResourceId", "-"),
resource.get("PhysicalResourceId", "-"),
]
)
table_data = {
"format_string": "{Logical ID:<{0}} {Physical ID:<{1}}",
"format_args": OrderedDict(
{
"Logical ID": "Logical ID",
"Physical ID": "Physical ID",
}
),
"table_name": "Resources",
"data": entry_list,
}
return table_data
46 changes: 46 additions & 0 deletions samcli/lib/list/stack_outputs/stack_output_to_table_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Implementation of the stack output to table mapper
"""
from typing import Dict, Any
from collections import OrderedDict
from samcli.lib.list.list_interfaces import Mapper


class StackOutputToTableMapper(Mapper):
"""
Mapper class for mapping stack-outputs data for table output
"""

def map(self, data: list) -> Dict[Any, Any]:
"""
Maps data to the format needed for consumption by the table consumer

Parameters
----------
data: list
List of dictionaries containing the entries of the stack outputs data

Returns
-------
table_data: Dict[Any, Any]
Dictionary containing the information and data needed for the table consumer
to output the data in table format
"""
entry_list = []
for stack_output in data:
entry_list.append(
[
stack_output.get("OutputKey", "-"),
stack_output.get("OutputValue", "-"),
stack_output.get("Description", "-"),
]
)
table_data = {
"format_string": "{OutputKey:<{0}} {OutputValue:<{1}} {Description:<{2}}",
"format_args": OrderedDict(
{"OutputKey": "OutputKey", "OutputValue": "OutputValue", "Description": "Description"}
),
"table_name": "Stack Outputs",
"data": entry_list,
}
return table_data
6 changes: 4 additions & 2 deletions samcli/lib/list/stack_outputs/stack_outputs_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def get_stack_info(self) -> Optional[Any]:

def produce(self):
response = self.get_stack_info()
output_list = []
for stack_output in response:
stack_output_data = StackOutputs(
OutputKey=stack_output["OutputKey"],
OutputValue=stack_output["OutputValue"],
Description=stack_output["Description"],
)
mapped_output = self.mapper.map(dataclasses.asdict(stack_output_data))
self.consumer.consume(mapped_output)
output_list.append(dataclasses.asdict(stack_output_data))
mapped_output = self.mapper.map(output_list)
self.consumer.consume(data=mapped_output)
Empty file.
17 changes: 17 additions & 0 deletions samcli/lib/list/testable_resources/testable_res_def.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
The container for Testable Resources
"""
from typing import Any
from dataclasses import dataclass


@dataclass
class TestableResDef:
"""
Dataclass for containing entries of testable resources data
"""

LogicalResourceId: str
PhysicalResourceId: str
CloudEndpointOrFunctionURL: Any
Methods: Any
Loading