Skip to content

Commit

Permalink
Merge from aws/aws-sam-cli/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
aws-sam-cli-bot authored May 3, 2022
2 parents 3a57b7a + c97b9cf commit 1248aa1
Show file tree
Hide file tree
Showing 19 changed files with 1,063 additions and 82 deletions.
2 changes: 1 addition & 1 deletion samcli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
SAM CLI version
"""

__version__ = "1.47.0"
__version__ = "1.48.0"
10 changes: 10 additions & 0 deletions samcli/commands/init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ def wrapped(*args, **kwargs):
""" """,
required=False,
)
@click.option(
"--tracing/--no-tracing",
default=None,
help="Enable AWS X-Ray tracing for your lambda functions",
)
@common_options
@non_interactive_validation
@pass_context
Expand All @@ -251,6 +256,7 @@ def cli(
app_template,
no_input,
extra_context,
tracing,
config_file,
config_env,
):
Expand All @@ -272,6 +278,7 @@ def cli(
app_template,
no_input,
extra_context,
tracing,
) # pragma: no cover


Expand All @@ -291,6 +298,7 @@ def do_cli(
app_template,
no_input,
extra_context,
tracing,
):
"""
Implementation of the ``cli`` method
Expand Down Expand Up @@ -339,6 +347,7 @@ def do_cli(
name,
no_input,
extra_context,
tracing,
)
else:
if not (pt_explicit or runtime or dependency_manager or base_image or architecture):
Expand All @@ -357,6 +366,7 @@ def do_cli(
name,
app_template,
no_input,
tracing,
)


Expand Down
13 changes: 12 additions & 1 deletion samcli/commands/init/init_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
from samcli.lib.init.exceptions import InitErrorException


def do_generate(location, package_type, runtime, dependency_manager, output_dir, name, no_input, extra_context):
def do_generate(
location,
package_type,
runtime,
dependency_manager,
output_dir,
name,
no_input,
extra_context,
tracing,
):
try:
generate_project(
location,
Expand All @@ -18,6 +28,7 @@ def do_generate(location, package_type, runtime, dependency_manager, output_dir,
name,
no_input,
extra_context,
tracing,
)
except InitErrorException as e:
raise UserException(str(e), wrapped_from=e.__class__.__name__) from e
72 changes: 67 additions & 5 deletions samcli/commands/init/interactive_init_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def do_interactive(
name,
app_template,
no_input,
tracing,
):
"""
Implementation of the ``cli`` method when --interactive is provided.
Expand All @@ -70,6 +71,7 @@ def do_interactive(
app_template,
no_input,
location_opt_choice,
tracing,
)


Expand All @@ -86,7 +88,40 @@ def generate_application(
app_template,
no_input,
location_opt_choice,
tracing,
): # pylint: disable=too-many-arguments
"""
The method holds the decision logic for generating an application
Parameters
----------
location : str
Location to SAM template
pt_explicit : bool
boolean representing if the customer explicitly stated packageType
package_type : str
Zip or Image
runtime : str
AWS Lambda runtime or Custom runtime
architecture : str
The architecture type 'x86_64' and 'arm64' in AWS
base_image : str
AWS Lambda base image
dependency_manager : str
Runtime's Dependency manager
output_dir : str
Project output directory
name : str
name of the project
app_template : str
AWS Serverless Application template
no_input : bool
Whether to prompt for input or to accept default values
(the default is False, which prompts the user for values it doesn't know for baking)
location_opt_choice : int
User input for selecting how to get customer a vended serverless application
tracing : bool
boolen value to determine if X-Ray tracing show be activated or not
"""
if location_opt_choice == "1":
_generate_from_use_case(
location,
Expand All @@ -99,14 +134,17 @@ def generate_application(
name,
app_template,
architecture,
tracing,
)

else:
_generate_from_location(location, package_type, runtime, dependency_manager, output_dir, name, no_input)
_generate_from_location(
location, package_type, runtime, dependency_manager, output_dir, name, no_input, tracing
)


# pylint: disable=too-many-statements
def _generate_from_location(location, package_type, runtime, dependency_manager, output_dir, name, no_input):
def _generate_from_location(location, package_type, runtime, dependency_manager, output_dir, name, no_input, tracing):
location = click.prompt("\nTemplate location (git, mercurial, http(s), zip, path)", type=str)
summary_msg = """
-----------------------
Expand All @@ -118,7 +156,7 @@ def _generate_from_location(location, package_type, runtime, dependency_manager,
location=location, output_dir=output_dir
)
click.echo(summary_msg)
do_generate(location, package_type, runtime, dependency_manager, output_dir, name, no_input, None)
do_generate(location, package_type, runtime, dependency_manager, output_dir, name, no_input, None, tracing)


# pylint: disable=too-many-statements
Expand All @@ -133,6 +171,7 @@ def _generate_from_use_case(
name: Optional[str],
app_template: Optional[str],
architecture: Optional[str],
tracing: Optional[bool],
) -> None:
templates = InitTemplates()
runtime_or_base_image = runtime if runtime else base_image
Expand All @@ -157,6 +196,9 @@ def _generate_from_use_case(
)
runtime, base_image, package_type, dependency_manager, template_chosen = chosen_app_template_properties

if tracing is None:
tracing = prompt_user_to_enable_tracing()

app_template = template_chosen["appTemplate"]
base_image = (
LAMBDA_IMAGES_RUNTIMES_MAP.get(str(runtime)) if not base_image and package_type == IMAGE else base_image
Expand Down Expand Up @@ -202,7 +244,15 @@ def _generate_from_use_case(
"""
click.secho(next_commands_msg, fg="yellow")
do_generate(
location, package_type, lambda_supported_runtime, dependency_manager, output_dir, name, no_input, extra_context
location,
package_type,
lambda_supported_runtime,
dependency_manager,
output_dir,
name,
no_input,
extra_context,
tracing,
)
# executing event_bridge logic if call is for Schema dynamic template
if is_dynamic_schemas_template:
Expand Down Expand Up @@ -244,7 +294,7 @@ def _generate_default_hello_world_application(
"""
is_package_type_image = bool(package_type == IMAGE)
if use_case == "Hello World Example" and not (runtime or base_image or is_package_type_image or dependency_manager):
if click.confirm("\n Use the most popular runtime and package type? (Python and zip)"):
if click.confirm("\nUse the most popular runtime and package type? (Python and zip)"):
runtime, package_type, dependency_manager, pt_explicit = "python3.9", ZIP, "pip", True
return (runtime, package_type, dependency_manager, pt_explicit)

Expand Down Expand Up @@ -306,6 +356,17 @@ def _get_app_template_properties(
return (runtime, base_image, package_type, dependency_manager, template_chosen)


def prompt_user_to_enable_tracing():
"""
Prompt user to if X-Ray Tracing should activated for functions in the SAM template and vice versa
"""
if click.confirm("\nWould you like to enable X-Ray tracing on the function(s) in your application? "):
doc_link = "https://aws.amazon.com/xray/pricing/"
click.echo(f"X-Ray will incur an additional cost. View {doc_link} for more details")
return True
return False


def _get_choice_from_options(chosen, options, question, msg):

if chosen:
Expand Down Expand Up @@ -606,6 +667,7 @@ def generate_summary_message(
Architectures: {architecture[0]}
Dependency Manager: {dependency_manager}
Output Directory: {output_dir}
Next steps can be found in the README file at {output_dir}/{name}/README.md
"""

Expand Down
9 changes: 9 additions & 0 deletions samcli/lib/init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cookiecutter.main import cookiecutter

from samcli.local.common.runtime_template import RUNTIME_DEP_TEMPLATE_MAPPING, is_custom_runtime
from samcli.lib.init.template_modifiers.xray_tracing_template_modifier import XRayTracingTemplateModifier
from samcli.lib.utils.packagetype import ZIP
from samcli.lib.utils import osutils
from .exceptions import GenerateProjectFailedError, InvalidLocationError
Expand All @@ -28,6 +29,7 @@ def generate_project(
name=None,
no_input=False,
extra_context=None,
tracing=False,
):
"""Generates project using cookiecutter and options given
Expand Down Expand Up @@ -56,6 +58,8 @@ def generate_project(
(the default is False, which prompts the user for values it doesn't know for baking)
extra_context : Optional[Dict]
An optional dictionary, the extra cookiecutter context
tracing: Optional[str]
Enable or disable X-Ray Tracing
Raises
------
Expand Down Expand Up @@ -111,3 +115,8 @@ def generate_project(
raise InvalidLocationError(template=params["template"]) from e
except CookiecutterException as e:
raise GenerateProjectFailedError(project=name, provider_error=e) from e

if tracing:
template_file_path = f"{output_dir}/{name}/template.yaml"
template_modifier = XRayTracingTemplateModifier(template_file_path)
template_modifier.modify_template()
Empty file.
Loading

0 comments on commit 1248aa1

Please sign in to comment.