Skip to content

Commit

Permalink
feat: Enable hook-name and skip-prepare-infra flagf for sam local sta…
Browse files Browse the repository at this point in the history
…rt-api (#5217)

* Enable hook-name flag for sam local start-api

* Format files
  • Loading branch information
mildaniel authored May 31, 2023
1 parent 4185d83 commit b7150f4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
23 changes: 22 additions & 1 deletion samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
from samcli.cli.cli_config_file import TomlProvider, configuration_option
from samcli.cli.main import aws_creds_options, pass_context, print_cmdline_args
from samcli.cli.main import common_options as cli_framework_options
from samcli.commands._utils.experimental import ExperimentalFlag, is_experimental_enabled
from samcli.commands._utils.option_value_processor import process_image_options
from samcli.commands._utils.options import generate_next_command_recommendation
from samcli.commands._utils.options import (
generate_next_command_recommendation,
hook_name_click_option,
skip_prepare_infra_option,
)
from samcli.commands.local.cli_common.options import (
invoke_common_options,
local_common_options,
Expand Down Expand Up @@ -54,6 +59,10 @@
context_settings={"max_content_width": 120},
)
@configuration_option(provider=TomlProvider(section="parameters"))
@hook_name_click_option(
force_prepare=False, invalid_coexist_options=["t", "template-file", "template", "parameter-overrides"]
)
@skip_prepare_infra_option
@service_common_options(3000)
@click.option(
"--static-dir",
Expand Down Expand Up @@ -98,6 +107,8 @@ def cli(
container_host,
container_host_interface,
invoke_image,
hook_name,
skip_prepare_infra,
):
"""
`sam local start-api` command entry point
Expand Down Expand Up @@ -128,6 +139,7 @@ def cli(
container_host,
container_host_interface,
invoke_image,
hook_name,
) # pragma: no cover


Expand Down Expand Up @@ -155,6 +167,7 @@ def do_cli( # pylint: disable=R0914
container_host,
container_host_interface,
invoke_image,
hook_name,
):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
Expand All @@ -170,6 +183,14 @@ def do_cli( # pylint: disable=R0914

LOG.debug("local start-api command is called")

if (
hook_name
and ExperimentalFlag.IaCsSupport.get(hook_name) is not None
and not is_experimental_enabled(ExperimentalFlag.IaCsSupport.get(hook_name))
):
LOG.info("Terraform Support beta feature is not enabled.")
return

processed_invoke_images = process_image_options(invoke_image)

# Pass all inputs to setup necessary context to invoke function locally.
Expand Down
4 changes: 4 additions & 0 deletions samcli/commands/local/start_api/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"parameter_overrides",
]

EXTENSION_OPTIONS: List[str] = ["hook_name", "skip_prepare_infra"]

CONTAINER_OPTION_NAMES: List[str] = [
"host",
"port",
Expand Down Expand Up @@ -53,6 +55,7 @@
+ ARTIFACT_LOCATION_OPTIONS
+ CONFIGURATION_OPTION_NAMES
+ ALL_COMMON_OPTIONS
+ EXTENSION_OPTIONS
)

OPTIONS_INFO: Dict[str, Dict] = {
Expand All @@ -65,6 +68,7 @@
"Artifact Location Options": {
"option_names": {opt: {"rank": idx} for idx, opt in enumerate(ARTIFACT_LOCATION_OPTIONS)}
},
"Extension Options": {"option_names": {opt: {"rank": idx} for idx, opt in enumerate(EXTENSION_OPTIONS)}},
"Configuration Options": {
"option_names": {opt: {"rank": idx} for idx, opt in enumerate(CONFIGURATION_OPTION_NAMES)},
"extras": [
Expand Down
1 change: 0 additions & 1 deletion samcli/commands/remote_invoke/remote_invoke_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


class RemoteInvokeContext:

_boto_client_provider: BotoProviderType
_boto_resource_provider: BotoProviderType
_stack_name: Optional[str]
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/commands/local/start_api/core/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_get_options_local_start_api_command(self, mock_get_params):
MockParams(rv=("--parameter-overrides", ""), name="parameter_overrides"),
MockParams(rv=("--host", ""), name="host"),
MockParams(rv=("--config-file", ""), name="config_file"),
MockParams(rv=("--hook_name", ""), name="hook_name"),
MockParams(rv=("--beta-features", ""), name="beta_features"),
MockParams(rv=("--log-file", ""), name="log_file"),
MockParams(rv=("--debug", ""), name="debug"),
Expand All @@ -41,6 +42,7 @@ def test_get_options_local_start_api_command(self, mock_get_params):
"Container Options": [("", ""), ("--host", ""), ("", "")],
"Description": [(cmd.description + cmd.description_addendum, "")],
"Examples": [("", ""), ("$sam local start-api\x1b[0m", "")],
"Extension Options": [("", ""), ("--hook_name", ""), ("", "")],
"Other Options": [("", ""), ("--debug", ""), ("", "")],
"Beta Options": [("", ""), ("--beta-features", ""), ("", "")],
"Required Options": [("", ""), ("--template-file", ""), ("", "")],
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/commands/local/start_api/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def setUp(self):
self.warm_containers = None
self.debug_function = None

self.hook_name = None

self.ctx_mock = Mock()
self.ctx_mock.region = self.region_name
self.ctx_mock.profile = self.profile
Expand Down Expand Up @@ -196,4 +198,5 @@ def call_cli(self):
container_host=self.container_host,
container_host_interface=self.container_host_interface,
invoke_image=self.invoke_image,
hook_name=self.hook_name,
)
1 change: 1 addition & 0 deletions tests/unit/commands/samconfig/test_samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ def test_local_start_api(self, do_cli_mock):
"localhost",
"127.0.0.1",
("image",),
None,
)

@patch("samcli.commands.local.start_lambda.cli.do_cli")
Expand Down

0 comments on commit b7150f4

Please sign in to comment.