From c2ebb3850e74e4b214c6d838f57a71bb59117dab Mon Sep 17 00:00:00 2001 From: Jacob Fuss <32497805+jfuss@users.noreply.github.com> Date: Thu, 7 Mar 2019 11:03:35 -0800 Subject: [PATCH 1/5] Chore: Bump aws-sam-translator to 1.10.0 (#1043) --- requirements/base.txt | 2 +- .../validate/lib/sam_template_validator.py | 25 +++---------------- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/requirements/base.txt b/requirements/base.txt index 65bbf5ee59..bb4c20f9db 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -6,7 +6,7 @@ Flask~=1.0.2 boto3~=1.9, >=1.9.56 PyYAML~=3.12 cookiecutter~=1.6.0 -aws-sam-translator==1.9.1 +aws-sam-translator==1.10.0 docker>=3.3.0 dateparser~=0.7 python-dateutil~=2.6 diff --git a/samcli/commands/validate/lib/sam_template_validator.py b/samcli/commands/validate/lib/sam_template_validator.py index 57e46b56b9..773fbf1c0a 100644 --- a/samcli/commands/validate/lib/sam_template_validator.py +++ b/samcli/commands/validate/lib/sam_template_validator.py @@ -59,29 +59,10 @@ def is_valid(self): self._replace_local_codeuri() - # In the Paser class, within the SAM Translator, they log a warning for when the template - # does not match the schema. The logger they use is the root logger instead of one scoped to - # their module. Currently this does not cause templates to fail, so we will suppress this - # by patching the logging.warning method that is used in that class. - class WarningSuppressLogger(object): - - def __init__(self, obj_to_patch): - self.obj_to_patch = obj_to_patch - - def __enter__(self): - self.obj_to_patch.warning = self.warning - - def __exit__(self, exc_type, exc_val, exc_tb): - self.obj_to_patch.warning = self.obj_to_patch.warning - - def warning(self, message): - pass - try: - with WarningSuppressLogger(parser.logging): - template = sam_translator.translate(sam_template=self.sam_template, - parameter_values={}) - LOG.debug("Translated template is:\n%s", yaml_dump(template)) + template = sam_translator.translate(sam_template=self.sam_template, + parameter_values={}) + LOG.debug("Translated template is:\n%s", yaml_dump(template)) except InvalidDocumentException as e: raise InvalidSamDocumentException( functools.reduce(lambda message, error: message + ' ' + str(error), e.causes, str(e))) From 5deff3c96bed9d5de8cfcada49cd594659bef595 Mon Sep 17 00:00:00 2001 From: Sriram Madapusi Vasudevan <3770774+TheSriram@users.noreply.github.com> Date: Fri, 8 Mar 2019 07:33:06 -0800 Subject: [PATCH 2/5] feat(init): gradle init support (#1040) --- samcli/commands/init/__init__.py | 22 ++- samcli/local/common/__init__.py | 0 samcli/local/common/runtime_template.py | 83 +++++++++ samcli/local/init/__init__.py | 49 +++-- .../LICENSE | 0 .../README.md | 0 .../cookiecutter.json | 0 .../tests/test_cookiecutter.py | 0 .../HelloWorldFunction/build.gradle | 23 +++ .../gradle/wrapper/gradle-wrapper.properties | 5 + .../HelloWorldFunction/gradlew | 172 ++++++++++++++++++ .../HelloWorldFunction/gradlew.bat | 84 +++++++++ .../src/main/java/helloworld/App.java | 0 .../main/java/helloworld/GatewayResponse.java | 0 .../src/test/java/helloworld/AppTest.java | 0 .../{{cookiecutter.project_name}}/README.md | 146 +++++++++++++++ .../template.yaml | 42 +++++ .../LICENSE | 14 ++ .../README.md | 42 +++++ .../cookiecutter.json | 4 + .../tests/test_cookiecutter.py | 41 +++++ .../HelloWorldFunction}/pom.xml | 0 .../src/main/java/helloworld/App.java | 38 ++++ .../main/java/helloworld/GatewayResponse.java | 33 ++++ .../src/test/java/helloworld/AppTest.java | 21 +++ .../{{cookiecutter.project_name}}/README.md | 25 +-- .../template.yaml | 0 tests/unit/commands/init/test_cli.py | 9 +- tests/unit/local/init/test_init.py | 31 +++- 29 files changed, 830 insertions(+), 54 deletions(-) create mode 100644 samcli/local/common/__init__.py create mode 100644 samcli/local/common/runtime_template.py rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java => cookiecutter-aws-sam-hello-java-gradle}/LICENSE (100%) rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java => cookiecutter-aws-sam-hello-java-gradle}/README.md (100%) rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java => cookiecutter-aws-sam-hello-java-gradle}/cookiecutter.json (100%) rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java => cookiecutter-aws-sam-hello-java-gradle}/tests/test_cookiecutter.py (100%) create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/build.gradle create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradle/wrapper/gradle-wrapper.properties create mode 100755 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew.bat rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}} => cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction}/src/main/java/helloworld/App.java (100%) rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}} => cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction}/src/main/java/helloworld/GatewayResponse.java (100%) rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}} => cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction}/src/test/java/helloworld/AppTest.java (100%) create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/template.yaml create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/LICENSE create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/README.md create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/cookiecutter.json create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/tests/test_cookiecutter.py rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}} => cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction}/pom.xml (100%) create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java create mode 100644 samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java => cookiecutter-aws-sam-hello-java-maven}/{{cookiecutter.project_name}}/README.md (85%) rename samcli/local/init/templates/{cookiecutter-aws-sam-hello-java => cookiecutter-aws-sam-hello-java-maven}/{{cookiecutter.project_name}}/template.yaml (100%) diff --git a/samcli/commands/init/__init__.py b/samcli/commands/init/__init__.py index 9e77e1fa8a..e015a08df5 100644 --- a/samcli/commands/init/__init__.py +++ b/samcli/commands/init/__init__.py @@ -3,29 +3,31 @@ Init command to scaffold a project app from a template """ import logging + import click from samcli.cli.main import pass_context, common_options +from samcli.commands.exceptions import UserException +from samcli.local.common.runtime_template import INIT_RUNTIMES, SUPPORTED_DEP_MANAGERS from samcli.local.init import generate_project -from samcli.local.init import RUNTIME_TEMPLATE_MAPPING from samcli.local.init.exceptions import GenerateProjectFailedError -from samcli.commands.exceptions import UserException LOG = logging.getLogger(__name__) -SUPPORTED_RUNTIME = [r for r in RUNTIME_TEMPLATE_MAPPING] @click.command(context_settings=dict(help_option_names=[u'-h', u'--help'])) @click.option('-l', '--location', help="Template location (git, mercurial, http(s), zip, path)") -@click.option('-r', '--runtime', type=click.Choice(SUPPORTED_RUNTIME), default="nodejs8.10", +@click.option('-r', '--runtime', type=click.Choice(INIT_RUNTIMES), default="nodejs8.10", help="Lambda Runtime of your app") +@click.option('-d', '--dependency-manager', type=click.Choice(SUPPORTED_DEP_MANAGERS), default=None, + help="Dependency manager of your Lambda runtime", required=False) @click.option('-o', '--output-dir', default='.', type=click.Path(), help="Where to output the initialized app into") @click.option('-n', '--name', default="sam-app", help="Name of your project to be generated as a folder") @click.option('--no-input', is_flag=True, default=False, help="Disable prompting and accept default values defined template config") @common_options @pass_context -def cli(ctx, location, runtime, output_dir, name, no_input): +def cli(ctx, location, runtime, dependency_manager, output_dir, name, no_input): """ \b Initialize a serverless application with a SAM template, folder structure for your Lambda functions, connected to an event source such as APIs, @@ -43,6 +45,10 @@ def cli(ctx, location, runtime, output_dir, name, no_input): \b $ sam init --runtime python3.6 \b + Initializes a new SAM project using Java 8 and Gradle dependency manager + \b + $ sam init --runtime java8 --dependency-manager gradle + \b Initializes a new SAM project using custom template in a Git/Mercurial repository \b # gh being expanded to github url @@ -66,11 +72,11 @@ def cli(ctx, location, runtime, output_dir, name, no_input): """ # All logic must be implemented in the `do_cli` method. This helps ease unit tests - do_cli(ctx, location, runtime, output_dir, + do_cli(ctx, location, runtime, dependency_manager, output_dir, name, no_input) # pragma: no cover -def do_cli(ctx, location, runtime, output_dir, name, no_input): +def do_cli(ctx, location, runtime, dependency_manager, output_dir, name, no_input): """ Implementation of the ``cli`` method, just separated out for unit testing purposes """ @@ -101,7 +107,7 @@ def do_cli(ctx, location, runtime, output_dir, name, no_input): next_step_msg = no_build_msg if runtime in no_build_step_required else build_msg try: - generate_project(location, runtime, output_dir, name, no_input) + generate_project(location, runtime, dependency_manager, output_dir, name, no_input) if not location: click.secho(next_step_msg, bold=True) click.secho("Read {name}/README.md for further instructions\n".format(name=name), bold=True) diff --git a/samcli/local/common/__init__.py b/samcli/local/common/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samcli/local/common/runtime_template.py b/samcli/local/common/runtime_template.py new file mode 100644 index 0000000000..04672af324 --- /dev/null +++ b/samcli/local/common/runtime_template.py @@ -0,0 +1,83 @@ +""" +All-in-one metadata about runtimes +""" + +import itertools +import os + +try: + import pathlib +except ImportError: + import pathlib2 as pathlib + +_init_path = str(pathlib.Path(os.path.dirname(__file__)).parent) +_templates = os.path.join(_init_path, 'init', 'templates') + +RUNTIME_DEP_TEMPLATE_MAPPING = { + "python": [ + { + "runtimes": ["python2.7", "python3.6", "python3.7"], + "dependency_manager": "pip", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), + "build": True + } + ], + "ruby": [ + { + "runtimes": ["ruby2.5"], + "dependency_manager": "bundler", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-ruby"), + "build": True + } + ], + "nodejs": [ + { + "runtimes": ["nodejs8.10"], + "dependency_manager": "npm", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs"), + "build": True + }, + { + "runtimes": ["nodejs6.10"], + "dependency_manager": "npm", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs6"), + "build": True + }, + ], + "dotnet": [ + { + "runtimes": ["dotnetcore", "dotnetcore1.0", "dotnetcore2.0", "dotnetcore2.1"], + "dependency_manager": "cli-package", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), + "build": False + }, + ], + "go": [ + { + "runtimes": ["go1.x"], + "dependency_manager": None, + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-go"), + "build": False + } + ], + "java": [ + { + "runtimes": ["java8"], + "dependency_manager": "maven", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-java-maven"), + "build": False + }, + { + "runtimes": ["java8"], + "dependency_manager": "gradle", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-java-gradle"), + "build": True + } + ] +} + +SUPPORTED_DEP_MANAGERS = set([c['dependency_manager'] for c in list( + itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values()))) if c['dependency_manager']]) +RUNTIMES = set(itertools.chain(*[c['runtimes'] for c in list( + itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values())))])) +INIT_RUNTIMES = RUNTIMES.union(RUNTIME_DEP_TEMPLATE_MAPPING.keys()) diff --git a/samcli/local/init/__init__.py b/samcli/local/init/__init__.py index 02c8a10358..89e4bc52c9 100644 --- a/samcli/local/init/__init__.py +++ b/samcli/local/init/__init__.py @@ -1,41 +1,20 @@ """ Init module to scaffold a project app from a template """ +import itertools import logging -import os -from cookiecutter.main import cookiecutter from cookiecutter.exceptions import CookiecutterException +from cookiecutter.main import cookiecutter + +from samcli.local.common.runtime_template import RUNTIME_DEP_TEMPLATE_MAPPING from samcli.local.init.exceptions import GenerateProjectFailedError LOG = logging.getLogger(__name__) -_init_path = os.path.dirname(__file__) -_templates = os.path.join(_init_path, 'templates') - -RUNTIME_TEMPLATE_MAPPING = { - "python3.7": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "python3.6": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "python2.7": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "python": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "ruby2.5": os.path.join(_templates, "cookiecutter-aws-sam-hello-ruby"), - "nodejs6.10": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs6"), - "nodejs8.10": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs"), - "nodejs": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs"), - "dotnetcore2.0": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnetcore2.1": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnetcore1.0": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnetcore": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnet": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "go1.x": os.path.join(_templates, "cookiecutter-aws-sam-hello-golang"), - "go": os.path.join(_templates, "cookiecutter-aws-sam-hello-golang"), - "java8": os.path.join(_templates, "cookiecutter-aws-sam-hello-java"), - "java": os.path.join(_templates, "cookiecutter-aws-sam-hello-java") -} - def generate_project( - location=None, runtime="nodejs", + location=None, runtime="nodejs", dependency_manager=None, output_dir=".", name='sam-sample-app', no_input=False): """Generates project using cookiecutter and options given @@ -50,6 +29,8 @@ def generate_project( (the default is None, which means no custom template) runtime: str, optional Lambda Runtime (the default is "nodejs", which creates a nodejs project) + dependency_manager: str, optional + Dependency Manager for the Lambda Runtime Project(the default is "npm" for a "nodejs" Lambda runtime) output_dir: str, optional Output directory where project should be generated (the default is ".", which implies current folder) @@ -66,8 +47,22 @@ def generate_project( If the process of baking a project fails """ + template = None + + for mapping in list(itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values()))): + if runtime in mapping['runtimes'] or any([r.startswith(runtime) for r in mapping['runtimes']]): + if not dependency_manager: + template = mapping['init_location'] + break + elif dependency_manager == mapping['dependency_manager']: + template = mapping['init_location'] + + if not template: + msg = "Lambda Runtime {} does not support dependency manager: {}".format(runtime, dependency_manager) + raise GenerateProjectFailedError(project=name, provider_error=msg) + params = { - "template": location if location else RUNTIME_TEMPLATE_MAPPING[runtime], + "template": location if location else template, "output_dir": output_dir, "no_input": no_input } diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/LICENSE b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/LICENSE similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/LICENSE rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/LICENSE diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/README.md similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/README.md rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/README.md diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/cookiecutter.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/cookiecutter.json similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/cookiecutter.json rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/cookiecutter.json diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/tests/test_cookiecutter.py b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/tests/test_cookiecutter.py similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/tests/test_cookiecutter.py rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/tests/test_cookiecutter.py diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/build.gradle b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/build.gradle new file mode 100644 index 0000000000..5df0f0ab7a --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/build.gradle @@ -0,0 +1,23 @@ +plugins { + id 'java' +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'software.amazon.awssdk:annotations:2.1.0' + compile ( + 'com.amazonaws:aws-lambda-java-core:1.1.0' + ) + testCompile 'junit:junit:4.12' +} + +sourceSets { + main { + java { + srcDir 'src/main' + } + } +} \ No newline at end of file diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradle/wrapper/gradle-wrapper.properties b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..558870dad5 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew new file mode 100755 index 0000000000..af6708ff22 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew.bat b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew.bat new file mode 100644 index 0000000000..0f8d5937c4 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/App.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/App.java rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/GatewayResponse.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/GatewayResponse.java rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/test/java/helloworld/AppTest.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/test/java/helloworld/AppTest.java rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md new file mode 100644 index 0000000000..daa130ff80 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md @@ -0,0 +1,146 @@ +# {{ cookiecutter.project_name }} + +This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: + +```bash +. +├── HelloWorldFunction +│   ├── build.gradle <-- Java Dependencies +│   ├── gradle <-- Gradle related Boilerplate +│   │   └── wrapper +│   │   ├── gradle-wrapper.jar +│   │   └── gradle-wrapper.properties +│   ├── gradlew <-- Linux/Mac Gradle Wrapper +│   ├── gradlew.bat <-- Windows Gradle Wrapper +│   └── src +│   ├── main +│   │   └── java +│   │   └── helloworld <-- Source code for a lambda function +│   │   ├── App.java <-- Lambda function code +│   │   └── GatewayResponse.java <-- POJO for API Gateway Responses object +│   └── test <-- Unit tests +│   └── java +│   └── helloworld +│   └── AppTest.java +├── README.md <-- This instructions file +└── template.yaml +``` + +## Requirements + +* AWS CLI already configured with Administrator permission +* [Java SE Development Kit 8 installed](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) +* [Docker installed](https://www.docker.com/community-edition) + +## Setup process + +### Installing dependencies + +```bash +sam build +``` + +You can also build on a Lambda like environment by using: + +```bash +sam build --use-container +``` + +### Local development + +**Invoking function locally through local API Gateway** + +```bash +sam local start-api +``` + +If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` + +**SAM CLI** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: + +```yaml +... +Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get +``` + +## Packaging and deployment + +Firstly, we need a `S3 bucket` where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one: + +```bash +aws s3 mb s3://BUCKET_NAME +``` + +Next, run the following command to package our Lambda function to S3: + +```bash +sam package \ + --output-template-file packaged.yaml \ + --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME +``` + +Next, the following command will create a Cloudformation Stack and deploy your SAM resources. + +```bash +sam deploy \ + --template-file packaged.yaml \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --capabilities CAPABILITY_IAM +``` + +> **See [Serverless Application Model (SAM) HOWTO Guide](https://github.com/awslabs/serverless-application-model/blob/master/HOWTO.md) for more details in how to get started.** + +After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: + +```bash +aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --query 'Stacks[].Outputs' +``` + +## Testing + +We use `JUnit` for testing our code and you can simply run the following command to run our tests: + +```bash +gradle test +``` + +# Appendix + +## AWS CLI commands + +AWS CLI commands to package, deploy and describe outputs defined within the cloudformation stack: + +```bash +sam package \ + --template-file template.yaml \ + --output-template-file packaged.yaml \ + --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME + +sam deploy \ + --template-file packaged.yaml \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --capabilities CAPABILITY_IAM \ + --parameter-overrides MyParameterSample=MySampleValue + +aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --query 'Stacks[].Outputs' +``` + +## Bringing to the next level + +Here are a few ideas that you can use to get more acquainted as to how this overall process works: + +* Create an additional API resource (e.g. /hello/{proxy+}) and return the name requested through this new path +* Update unit test to capture that +* Package & Deploy + +Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications: + +* [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/template.yaml b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/template.yaml new file mode 100644 index 0000000000..cbd079179a --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/template.yaml @@ -0,0 +1,42 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + {{ cookiecutter.project_name }} + + Sample SAM Template for {{ cookiecutter.project_name }} + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 20 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: HelloWorldFunction + Handler: helloworld.App::handleRequest + Runtime: java8 + Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object + Variables: + PARAM1: VALUE + Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get + +Outputs: + # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function + # Find out more about other implicit resources you can reference within SAM + # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/LICENSE b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/LICENSE new file mode 100644 index 0000000000..f19aaa6d09 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/LICENSE @@ -0,0 +1,14 @@ +MIT No Attribution + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/README.md new file mode 100644 index 0000000000..ec9954e48b --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/README.md @@ -0,0 +1,42 @@ +# Cookiecutter SAM for Java Lambda functions + +This is a [Cookiecutter](https://github.com/audreyr/cookiecutter) template to create a Serverless Hello World App based on Serverless Application Model (SAM) and Java. + +It is important to note that you should not try to `git clone` this project but use `cookiecutter` CLI instead as ``{{cookiecutter.project_name}}`` will be rendered based on your input and therefore all variables and files will be rendered properly. + +## Requirements + +Install `cookiecutter` command line: + +**Pip users**: + +* `pip install cookiecutter` + +**Homebrew users**: + +* `brew install cookiecutter` + +**Windows or Pipenv users**: + +* `pipenv install cookiecutter` + +**NOTE**: [`Pipenv`](https://github.com/pypa/pipenv) is the new and recommended Python packaging tool that works across multiple platforms and makes Windows a first-class citizen. + +## Usage + +Generate a new SAM based Serverless App: `cookiecutter gh:aws-samples/cookiecutter-aws-sam-hello-java`. + +You'll be prompted a few questions to help this cookiecutter template to scaffold this project and after its completed you should see a new folder at your current path with the name of the project you gave as input. + +**NOTE**: After you understand how cookiecutter works (cookiecutter.json, mainly), you can fork this repo and apply your own mechanisms to accelerate your development process and this can be followed for any programming language and OS. + + +# Credits + +* This project has been generated with [Cookiecutter](https://github.com/audreyr/cookiecutter) + + +License +------- + +This project is licensed under the terms of the [MIT License with no attribution](/LICENSE) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/cookiecutter.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/cookiecutter.json new file mode 100644 index 0000000000..9d928e8536 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/cookiecutter.json @@ -0,0 +1,4 @@ +{ + "project_name": "Name of the project", + "runtime": "java8" +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/tests/test_cookiecutter.py b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/tests/test_cookiecutter.py new file mode 100644 index 0000000000..8f2a6cf519 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/tests/test_cookiecutter.py @@ -0,0 +1,41 @@ +""" + Tests cookiecutter baking process and rendered content +""" + +def test_project_tree(cookies): + result = cookies.bake(extra_context={ + 'project_name': 'hello sam' + }) + assert result.exit_code == 0 + assert result.exception is None + assert result.project.basename == 'hello sam' + assert result.project.isdir() + assert result.project.join('template.yaml').isfile() + assert result.project.join('README.md').isfile() + assert result.project.join('src').isdir() + assert result.project.join('src', 'main').isdir() + assert result.project.join('src', 'main', 'java').isdir() + assert result.project.join('src', 'main', 'java', 'helloworld').isdir() + assert result.project.join('src', 'main', 'java', 'helloworld', 'App.java').isfile() + assert result.project.join('src', 'main', 'java', 'helloworld', 'GatewayResponse.java').isfile() + assert result.project.join('src', 'test', 'java').isdir() + assert result.project.join('src', 'test', 'java', 'helloworld').isdir() + assert result.project.join('src', 'test', 'java', 'helloworld', 'AppTest.java').isfile() + + +def test_app_content(cookies): + result = cookies.bake(extra_context={'project_name': 'my_lambda'}) + app_file = result.project.join('src', 'main', 'java', 'helloworld', 'App.java') + app_content = app_file.readlines() + app_content = ''.join(app_content) + + contents = ( + "package helloword", + "class App implements RequestHandler", + "https://checkip.amazonaws.com", + "return new GatewayResponse", + "getPageContents", + ) + + for content in contents: + assert content in app_content diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/pom.xml b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/pom.xml similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/pom.xml rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/pom.xml diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java new file mode 100644 index 0000000000..18890033e1 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java @@ -0,0 +1,38 @@ +package helloworld; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +/** + * Handler for requests to Lambda function. + */ +public class App implements RequestHandler { + + public Object handleRequest(final Object input, final Context context) { + Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + headers.put("X-Custom-Header", "application/json"); + try { + final String pageContents = this.getPageContents("https://checkip.amazonaws.com"); + String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); + return new GatewayResponse(output, headers, 200); + } catch (IOException e) { + return new GatewayResponse("{}", headers, 500); + } + } + + private String getPageContents(String address) throws IOException{ + URL url = new URL(address); + try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { + return br.lines().collect(Collectors.joining(System.lineSeparator())); + } + } +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java new file mode 100644 index 0000000000..ea58a79c99 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java @@ -0,0 +1,33 @@ +package helloworld; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * POJO containing response object for API Gateway. + */ +public class GatewayResponse { + + private final String body; + private final Map headers; + private final int statusCode; + + public GatewayResponse(final String body, final Map headers, final int statusCode) { + this.statusCode = statusCode; + this.body = body; + this.headers = Collections.unmodifiableMap(new HashMap<>(headers)); + } + + public String getBody() { + return body; + } + + public Map getHeaders() { + return headers; + } + + public int getStatusCode() { + return statusCode; + } +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java new file mode 100644 index 0000000000..e64bdfc55a --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java @@ -0,0 +1,21 @@ +package helloworld; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class AppTest { + @Test + public void successfulResponse() { + App app = new App(); + GatewayResponse result = (GatewayResponse) app.handleRequest(null, null); + assertEquals(result.getStatusCode(), 200); + assertEquals(result.getHeaders().get("Content-Type"), "application/json"); + String content = result.getBody(); + assertNotNull(content); + assertTrue(content.contains("\"message\"")); + assertTrue(content.contains("\"hello world\"")); + assertTrue(content.contains("\"location\"")); + } +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md similarity index 85% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/README.md rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md index 980f230f17..91899e4c3d 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md @@ -4,18 +4,19 @@ This is a sample template for {{ cookiecutter.project_name }} - Below is a brief ```bash . -├── README.md <-- This instructions file -├── pom.xml <-- Java dependencies -├── src -│ ├── main -│ │ └── java -│ │ └── helloworld <-- Source code for a lambda function -│ │ ├── App.java <-- Lambda function code -│ │ └── GatewayResponse.java <-- POJO for API Gateway Responses object -│ └── test <-- Unit tests -│ └── java -│ └── helloworld -│ └── AppTest.java +├── HelloWorldFunction +│   ├── pom.xml <-- Java dependencies +│   └── src +│   ├── main +│   │   └── java +│   │   └── helloworld <-- Source code for a lambda function +│   │   ├── App.java <-- Lambda function code +│   │   └── GatewayResponse.java <-- POJO for API Gateway Responses object +│   └── test <-- Unit tests +│   └── java +│   └── helloworld +│   └── AppTest.java +├── README.md <-- This instructions file └── template.yaml ``` diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/template.yaml b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/template.yaml rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml diff --git a/tests/unit/commands/init/test_cli.py b/tests/unit/commands/init/test_cli.py index 2fe29d6d40..25fe7142c3 100644 --- a/tests/unit/commands/init/test_cli.py +++ b/tests/unit/commands/init/test_cli.py @@ -12,6 +12,7 @@ def setUp(self): self.ctx = None self.location = None self.runtime = "python3.6" + self.dependency_manager = "pip" self.output_dir = "." self.name = "testing project" self.no_input = False @@ -21,12 +22,13 @@ def test_init_cli(self, generate_project_patch): # GIVEN generate_project successfully created a project # WHEN a project name has been passed init_cli( - ctx=self.ctx, location=self.location, runtime=self.runtime, output_dir=self.output_dir, + ctx=self.ctx, location=self.location, runtime=self.runtime, + dependency_manager=self.dependency_manager, output_dir=self.output_dir, name=self.name, no_input=self.no_input) # THEN we should receive no errors generate_project_patch.assert_called_once_with( - self.location, self.runtime, + self.location, self.runtime, self.dependency_manager, self.output_dir, self.name, self.no_input) @patch("samcli.commands.init.generate_project") @@ -42,8 +44,9 @@ def test_init_cli_generate_project_fails(self, generate_project_patch): with self.assertRaises(UserException): init_cli( self.ctx, location="self.location", runtime=self.runtime, + dependency_manager=self.dependency_manager, output_dir=self.output_dir, name=self.name, no_input=self.no_input) generate_project_patch.assert_called_with( - self.location, self.runtime, + self.location, self.runtime, self.dependency_manager, self.output_dir, self.name, self.no_input) diff --git a/tests/unit/local/init/test_init.py b/tests/unit/local/init/test_init.py index 4e79db081e..8ecb941a85 100644 --- a/tests/unit/local/init/test_init.py +++ b/tests/unit/local/init/test_init.py @@ -4,7 +4,7 @@ from cookiecutter.exceptions import CookiecutterException from samcli.local.init import generate_project from samcli.local.init import GenerateProjectFailedError -from samcli.local.init import RUNTIME_TEMPLATE_MAPPING +from samcli.local.init import RUNTIME_DEP_TEMPLATE_MAPPING class TestInit(TestCase): @@ -12,18 +12,20 @@ class TestInit(TestCase): def setUp(self): self.location = None self.runtime = "python3.6" + self.dependency_manager = "pip" self.output_dir = "." self.name = "testing project" self.no_input = True self.extra_context = {'project_name': 'testing project', "runtime": self.runtime} - self.template = RUNTIME_TEMPLATE_MAPPING[self.runtime] + self.template = RUNTIME_DEP_TEMPLATE_MAPPING["python"][0]["init_location"] @patch("samcli.local.init.cookiecutter") def test_init_successful(self, cookiecutter_patch): # GIVEN generate_project successfully created a project # WHEN a project name has been passed generate_project( - location=self.location, runtime=self.runtime, output_dir=self.output_dir, + location=self.location, runtime=self.runtime, dependency_manager=self.dependency_manager, + output_dir=self.output_dir, name=self.name, no_input=self.no_input) # THEN we should receive no errors @@ -31,6 +33,27 @@ def test_init_successful(self, cookiecutter_patch): extra_context=self.extra_context, no_input=self.no_input, output_dir=self.output_dir, template=self.template) + @patch("samcli.local.init.cookiecutter") + def test_init_successful_with_no_dep_manager(self, cookiecutter_patch): + generate_project( + location=self.location, runtime=self.runtime, dependency_manager=None, + output_dir=self.output_dir, + name=self.name, no_input=self.no_input) + + # THEN we should receive no errors + cookiecutter_patch.assert_called_once_with( + extra_context=self.extra_context, no_input=self.no_input, + output_dir=self.output_dir, template=self.template) + + def test_init_error_with_non_compatible_dependency_manager(self): + with self.assertRaises(GenerateProjectFailedError) as ctx: + generate_project( + location=self.location, runtime=self.runtime, dependency_manager="gradle", + output_dir=self.output_dir, name=self.name, no_input=self.no_input) + self.assertEquals("An error occurred while generating this " + "testing project: Lambda Runtime python3.6 " + "does not support dependency manager: gradle", str(ctx.exception)) + @patch("samcli.local.init.cookiecutter") def test_when_generate_project_returns_error(self, cookiecutter_patch): @@ -44,7 +67,7 @@ def test_when_generate_project_returns_error(self, cookiecutter_patch): # THEN we should receive a GenerateProjectFailedError Exception with self.assertRaises(GenerateProjectFailedError) as ctx: generate_project( - location=self.location, runtime=self.runtime, + location=self.location, runtime=self.runtime, dependency_manager=self.dependency_manager, output_dir=self.output_dir, name=self.name, no_input=self.no_input) self.assertEquals(expected_msg, str(ctx.exception)) From d7ff936855278089f49d1310677ad6d32ded2176 Mon Sep 17 00:00:00 2001 From: Jacob Fuss <32497805+jfuss@users.noreply.github.com> Date: Fri, 8 Mar 2019 08:38:48 -0800 Subject: [PATCH 3/5] feat(build): Support building Java8 functions through Maven (#1044) --- requirements/base.txt | 2 +- samcli/lib/build/workflow_config.py | 10 ++++- samcli/local/common/runtime_template.py | 2 +- .../{{cookiecutter.project_name}}/README.md | 37 +++++++++++-------- .../template.yaml | 2 +- tests/integration/buildcmd/test_build_cmd.py | 20 ++++++---- .../testdata/buildcmd/Java/maven/pom.xml | 26 +++++++++++++ .../src/main/java/aws/example/Hello.java | 13 +++++++ .../lib/build_module/test_workflow_config.py | 16 +++++--- 9 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 tests/integration/testdata/buildcmd/Java/maven/pom.xml create mode 100644 tests/integration/testdata/buildcmd/Java/maven/src/main/java/aws/example/Hello.java diff --git a/requirements/base.txt b/requirements/base.txt index bb4c20f9db..928877a0dd 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -12,5 +12,5 @@ dateparser~=0.7 python-dateutil~=2.6 pathlib2~=2.3.2; python_version<"3.4" requests==2.20.1 -aws_lambda_builders==0.1.0 +aws_lambda_builders==0.2.0 serverlessrepo==0.1.5 diff --git a/samcli/lib/build/workflow_config.py b/samcli/lib/build/workflow_config.py index 055b0c2240..fd901290a3 100644 --- a/samcli/lib/build/workflow_config.py +++ b/samcli/lib/build/workflow_config.py @@ -41,6 +41,13 @@ manifest_name="build.gradle", executable_search_paths=None) +JAVA_MAVEN_CONFIG = CONFIG( + language="java", + dependency_manager="maven", + application_framework=None, + manifest_name="pom.xml", + executable_search_paths=None) + class UnsupportedRuntimeException(Exception): pass @@ -83,7 +90,8 @@ def get_workflow_config(runtime, code_dir, project_dir): # manifest "java8": ManifestWorkflowSelector([ # Gradle builder needs custom executable paths to find `gradlew` binary - JAVA_GRADLE_CONFIG._replace(executable_search_paths=[code_dir, project_dir]) + JAVA_GRADLE_CONFIG._replace(executable_search_paths=[code_dir, project_dir]), + JAVA_MAVEN_CONFIG ]), } diff --git a/samcli/local/common/runtime_template.py b/samcli/local/common/runtime_template.py index 04672af324..bcfaa20a55 100644 --- a/samcli/local/common/runtime_template.py +++ b/samcli/local/common/runtime_template.py @@ -65,7 +65,7 @@ "runtimes": ["java8"], "dependency_manager": "maven", "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-java-maven"), - "build": False + "build": True }, { "runtimes": ["java8"], diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md index 91899e4c3d..0d52d9f69c 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md @@ -3,20 +3,19 @@ This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: ```bash -. -├── HelloWorldFunction -│   ├── pom.xml <-- Java dependencies -│   └── src -│   ├── main -│   │   └── java -│   │   └── helloworld <-- Source code for a lambda function -│   │   ├── App.java <-- Lambda function code -│   │   └── GatewayResponse.java <-- POJO for API Gateway Responses object -│   └── test <-- Unit tests -│   └── java -│   └── helloworld -│   └── AppTest.java -├── README.md <-- This instructions file +├── README.md <-- This instructions file +├── HelloWorldFunction <-- Source for HelloWorldFunction Lambda Function +│ ├── pom.xml <-- Java dependencies +│ └── src +│ ├── main +│ │ └── java +│ │ └── helloworld +│ │ ├── App.java <-- Lambda function code +│ │ └── GatewayResponse.java <-- POJO for API Gateway Responses object +│ └── test <-- Unit tests +│ └── java +│ └── helloworld +│ └── AppTest.java └── template.yaml ``` @@ -31,11 +30,16 @@ This is a sample template for {{ cookiecutter.project_name }} - Below is a brief ### Installing dependencies -We use `maven` to install our dependencies and package our application into a JAR file: +```bash +sam build +``` + +You can also build on a Lambda like environment by using ```bash -mvn package +sam build --use-container ``` + ### Local development **Invoking function locally through local API Gateway** @@ -109,6 +113,7 @@ aws cloudformation describe-stacks \ We use `JUnit` for testing our code and you can simply run the following command to run our tests: ```bash +cd HelloWorldFunction mvn test ``` diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml index 080a1e6e84..cbd079179a 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml @@ -14,7 +14,7 @@ Resources: HelloWorldFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: - CodeUri: target/HelloWorld-1.0.jar + CodeUri: HelloWorldFunction Handler: helloworld.App::handleRequest Runtime: java8 Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object diff --git a/tests/integration/buildcmd/test_build_cmd.py b/tests/integration/buildcmd/test_build_cmd.py index b8b5183968..3816e2624e 100644 --- a/tests/integration/buildcmd/test_build_cmd.py +++ b/tests/integration/buildcmd/test_build_cmd.py @@ -240,22 +240,26 @@ def _verify_built_artifact(self, build_dir, function_logical_id, expected_files, self.assertTrue(any([True if self.EXPECTED_RUBY_GEM in gem else False for gem in os.listdir(str(gem_path))])) -class TestBuildCommand_JavaGradle(BuildIntegBase): +class TestBuildCommand_Java(BuildIntegBase): - EXPECTED_FILES_PROJECT_MANIFEST = {'aws', 'lib', "META-INF"} + EXPECTED_FILES_PROJECT_MANIFEST_GRADLE = {'aws', 'lib', "META-INF"} + EXPECTED_FILES_PROJECT_MANIFEST_MAVEN = {'aws', 'lib'} EXPECTED_DEPENDENCIES = {'annotations-2.1.0.jar', "aws-lambda-java-core-1.1.0.jar"} FUNCTION_LOGICAL_ID = "Function" USING_GRADLE_PATH = os.path.join("Java", "gradle") USING_GRADLEW_PATH = os.path.join("Java", "gradlew") + USING_MAVEN_PATH = str(Path('Java', 'maven')) @parameterized.expand([ - ("java8", USING_GRADLE_PATH, False), - ("java8", USING_GRADLEW_PATH, False), - ("java8", USING_GRADLE_PATH, "use_container"), - ("java8", USING_GRADLEW_PATH, "use_container"), + ("java8", USING_GRADLE_PATH, EXPECTED_FILES_PROJECT_MANIFEST_GRADLE, False), + ("java8", USING_GRADLEW_PATH, EXPECTED_FILES_PROJECT_MANIFEST_GRADLE, False), + ("java8", USING_MAVEN_PATH, EXPECTED_FILES_PROJECT_MANIFEST_MAVEN, False), + ("java8", USING_GRADLE_PATH, EXPECTED_FILES_PROJECT_MANIFEST_GRADLE, "use_container"), + ("java8", USING_GRADLEW_PATH, EXPECTED_FILES_PROJECT_MANIFEST_GRADLE, "use_container"), + ("java8", USING_MAVEN_PATH, EXPECTED_FILES_PROJECT_MANIFEST_MAVEN, "use_container") ]) - def test_with_gradle(self, runtime, code_path, use_container): + def test_with_building_java(self, runtime, code_path, expected_files, use_container): overrides = {"Runtime": runtime, "CodeUri": code_path, "Handler": "aws.example.Hello::myHandler"} cmdlist = self.get_command_list(use_container=use_container, parameter_overrides=overrides) @@ -265,7 +269,7 @@ def test_with_gradle(self, runtime, code_path, use_container): process.wait() self._verify_built_artifact(self.default_build_dir, self.FUNCTION_LOGICAL_ID, - self.EXPECTED_FILES_PROJECT_MANIFEST, self.EXPECTED_DEPENDENCIES) + expected_files, self.EXPECTED_DEPENDENCIES) self._verify_resource_property(str(self.built_template), "OtherRelativePathResource", diff --git a/tests/integration/testdata/buildcmd/Java/maven/pom.xml b/tests/integration/testdata/buildcmd/Java/maven/pom.xml new file mode 100644 index 0000000000..d8d6558a12 --- /dev/null +++ b/tests/integration/testdata/buildcmd/Java/maven/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + helloworld + HelloWorld + 1.0 + jar + A sample Hello World created for SAM CLI. + + 1.8 + 1.8 + + + + + com.amazonaws + aws-lambda-java-core + 1.1.0 + + + software.amazon.awssdk + annotations + 2.1.0 + + + \ No newline at end of file diff --git a/tests/integration/testdata/buildcmd/Java/maven/src/main/java/aws/example/Hello.java b/tests/integration/testdata/buildcmd/Java/maven/src/main/java/aws/example/Hello.java new file mode 100644 index 0000000000..9f6e4084e9 --- /dev/null +++ b/tests/integration/testdata/buildcmd/Java/maven/src/main/java/aws/example/Hello.java @@ -0,0 +1,13 @@ +package aws.example; + + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.LambdaLogger; + +public class Hello { + public String myHandler(Context context) { + LambdaLogger logger = context.getLogger(); + logger.log("Function Invoked\n"); + return "Hello World"; + } +} \ No newline at end of file diff --git a/tests/unit/lib/build_module/test_workflow_config.py b/tests/unit/lib/build_module/test_workflow_config.py index 0722ea8a45..86cf41da14 100644 --- a/tests/unit/lib/build_module/test_workflow_config.py +++ b/tests/unit/lib/build_module/test_workflow_config.py @@ -50,20 +50,24 @@ def test_must_work_for_ruby(self, runtime): self.assertIsNone(result.executable_search_paths) @parameterized.expand([ - ("java8", "build.gradle") + ("java8", "build.gradle", "gradle"), + ("java8", "pom.xml", "maven") ]) @patch("samcli.lib.build.workflow_config.os") - def test_must_work_for_java(self, runtime, build_file, os_mock): - + def test_must_work_for_java(self, runtime, build_file, dep_manager, os_mock): os_mock.path.join.side_effect = lambda dirname, v: v os_mock.path.exists.side_effect = lambda v: v == build_file result = get_workflow_config(runtime, self.code_dir, self.project_dir) self.assertEquals(result.language, "java") - self.assertEquals(result.dependency_manager, "gradle") + self.assertEquals(result.dependency_manager, dep_manager) self.assertEquals(result.application_framework, None) - self.assertEquals(result.manifest_name, "build.gradle") - self.assertEquals(result.executable_search_paths, [self.code_dir, self.project_dir]) + self.assertEquals(result.manifest_name, build_file) + + if dep_manager == "gradle": + self.assertEquals(result.executable_search_paths, [self.code_dir, self.project_dir]) + else: + self.assertIsNone(result.executable_search_paths) @parameterized.expand([ ("java8", "unknown.manifest") From 43564aa89c6c9990eef11e84e2755012086b22f2 Mon Sep 17 00:00:00 2001 From: Vicky Wang Date: Fri, 8 Mar 2019 12:02:23 -0800 Subject: [PATCH 4/5] feat(publish): Add --semantic-version option to sam publish command (#1020) --- designs/sam_publish_cmd.rst | 21 +++-- requirements/base.txt | 2 +- samcli/commands/publish/command.py | 65 ++++++-------- .../publish/publish_app_integ_base.py | 5 +- .../integration/publish/test_command_integ.py | 18 ++++ tests/unit/commands/publish/test_command.py | 90 +++++++++++-------- 6 files changed, 119 insertions(+), 82 deletions(-) diff --git a/designs/sam_publish_cmd.rst b/designs/sam_publish_cmd.rst index 45b2de3dd2..305ba8f5ad 100644 --- a/designs/sam_publish_cmd.rst +++ b/designs/sam_publish_cmd.rst @@ -132,6 +132,11 @@ Create new version of an existing SAR application Click the link below to view your application in AWS console: https://console.aws.amazon.com/serverlessrepo/home?region=#/published-applications/ + Alternatively, you can provide the new version number through the --semantic-version option without manually modifying + the template. The command will publish a new application version using the specified value. + + >>> sam publish -t ./packaged.yaml --semantic-version 0.0.2 + Update the metadata of an existing application without creating new version Keep SemanticVersion unchanged, then modify metadata fields like Description or ReadmeUrl, and run ``sam publish -t ./packaged.yaml``. SAM CLI prints application metadata updated message, values of updated @@ -184,13 +189,15 @@ CLI Changes $ sam publish -t packaged.yaml --region Options: - -t, --template PATH AWS SAM template file [default: template.[yaml|yml]] - --profile TEXT Select a specific profile from your credential file to - get AWS credentials. - --region TEXT Set the AWS Region of the service (e.g. us-east-1). - --debug Turn on debug logging to print debug message generated - by SAM CLI. - --help Show this message and exit. + -t, --template PATH AWS SAM template file [default: template.[yaml|yml]] + --semantic-version TEXT Optional. The value provided here overrides SemanticVersion + in the template metadata. + --profile TEXT Select a specific profile from your credential file to + get AWS credentials. + --region TEXT Set the AWS Region of the service (e.g. us-east-1). + --debug Turn on debug logging to print debug message generated + by SAM CLI. + --help Show this message and exit. 2. Update ``sam package`` (``aws cloudformation package``) command to support uploading locally referenced readme and license files to S3. diff --git a/requirements/base.txt b/requirements/base.txt index 928877a0dd..5da6d74ca6 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -13,4 +13,4 @@ python-dateutil~=2.6 pathlib2~=2.3.2; python_version<"3.4" requests==2.20.1 aws_lambda_builders==0.2.0 -serverlessrepo==0.1.5 +serverlessrepo==0.1.8 diff --git a/samcli/commands/publish/command.py b/samcli/commands/publish/command.py index 77eebedab2..9587f0c863 100644 --- a/samcli/commands/publish/command.py +++ b/samcli/commands/publish/command.py @@ -1,19 +1,24 @@ """CLI command for "publish" command.""" import json +import logging import click import boto3 -from botocore.exceptions import ClientError from serverlessrepo import publish_application from serverlessrepo.publish import CREATE_APPLICATION -from serverlessrepo.exceptions import ServerlessRepoError +from serverlessrepo.parser import METADATA, SERVERLESS_REPO_APPLICATION +from serverlessrepo.exceptions import ServerlessRepoError, InvalidS3UriError from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options from samcli.commands._utils.options import template_common_option from samcli.commands._utils.template import get_template_data from samcli.commands.exceptions import UserException +LOG = logging.getLogger(__name__) + +SAM_PUBLISH_DOC = "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-publishing-applications.html" # noqa +SAM_PACKAGE_DOC = "https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-package.html" # noqa HELP_TEXT = """ Use this command to publish a packaged AWS SAM template to the AWS Serverless Application Repository to share within your team, @@ -22,29 +27,32 @@ This command expects the template's Metadata section to contain an AWS::ServerlessRepo::Application section with application metadata for publishing. For more details on this metadata section, see -https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-publishing-applications.html +{} \b Examples -------- To publish an application $ sam publish -t packaged.yaml --region -""" +""".format(SAM_PUBLISH_DOC) SHORT_HELP = "Publish a packaged AWS SAM template to the AWS Serverless Application Repository." SERVERLESSREPO_CONSOLE_URL = "https://console.aws.amazon.com/serverlessrepo/home?region={}#/published-applications/{}" +SEMANTIC_VERSION_HELP = "Optional. The value provided here overrides SemanticVersion in the template metadata." +SEMANTIC_VERSION = 'SemanticVersion' @click.command("publish", help=HELP_TEXT, short_help=SHORT_HELP) @template_common_option +@click.option('--semantic-version', help=SEMANTIC_VERSION_HELP) @aws_creds_options @cli_framework_options @pass_context -def cli(ctx, template): +def cli(ctx, template, semantic_version): # All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing - do_cli(ctx, template) # pragma: no cover + do_cli(ctx, template, semantic_version) # pragma: no cover -def do_cli(ctx, template): +def do_cli(ctx, template, semantic_version): """Publish the application based on command line inputs.""" try: template_data = get_template_data(template) @@ -52,16 +60,24 @@ def do_cli(ctx, template): click.secho("Publish Failed", fg='red') raise UserException(str(ex)) + # Override SemanticVersion in template metadata when provided in command input + if semantic_version and SERVERLESS_REPO_APPLICATION in template_data.get(METADATA, {}): + template_data.get(METADATA).get(SERVERLESS_REPO_APPLICATION)[SEMANTIC_VERSION] = semantic_version + try: publish_output = publish_application(template_data) click.secho("Publish Succeeded", fg="green") - click.secho(_gen_success_message(publish_output), fg="yellow") - except ServerlessRepoError as ex: + click.secho(_gen_success_message(publish_output)) + except InvalidS3UriError: click.secho("Publish Failed", fg='red') - raise UserException(str(ex)) - except ClientError as ex: + raise UserException( + "Your SAM template contains invalid S3 URIs. Please make sure that you have uploaded application " + "artifacts to S3 by packaging the template. See more details in {}".format(SAM_PACKAGE_DOC)) + except ServerlessRepoError as ex: click.secho("Publish Failed", fg='red') - raise _wrap_s3_uri_exception(ex) + LOG.debug("Failed to publish application to serverlessrepo", exc_info=True) + error_msg = '{}\nPlease follow the instructions in {}'.format(str(ex), SAM_PUBLISH_DOC) + raise UserException(error_msg) application_id = publish_output.get('application_id') _print_console_link(ctx.region, application_id) @@ -108,28 +124,3 @@ def _print_console_link(region, application_id): console_link = SERVERLESSREPO_CONSOLE_URL.format(region, application_id.replace('/', '~')) msg = "Click the link below to view your application in AWS console:\n{}".format(console_link) click.secho(msg, fg="yellow") - - -def _wrap_s3_uri_exception(ex): - """ - Wrap invalid S3 URI exception with a better error message. - - Parameters - ---------- - ex : ClientError - boto3 exception - - Returns - ------- - Exception - UserException if found invalid S3 URI or ClientError - """ - error_code = ex.response.get('Error').get('Code') - message = ex.response.get('Error').get('Message') - - if error_code == 'BadRequestException' and "Invalid S3 URI" in message: - return UserException( - "Your SAM template contains invalid S3 URIs. Please make sure that you have uploaded application " - "artifacts to S3 by packaging the template: 'sam package --template-file '.") - - return ex diff --git a/tests/integration/publish/publish_app_integ_base.py b/tests/integration/publish/publish_app_integ_base.py index 7c93de99d9..d2c689fb0d 100644 --- a/tests/integration/publish/publish_app_integ_base.py +++ b/tests/integration/publish/publish_app_integ_base.py @@ -90,7 +90,7 @@ def base_command(self): return command - def get_command_list(self, template_path=None, region=None, profile=None): + def get_command_list(self, template_path=None, region=None, profile=None, semantic_version=None): command_list = [self.base_command(), "publish"] if template_path: @@ -102,4 +102,7 @@ def get_command_list(self, template_path=None, region=None, profile=None): if profile: command_list = command_list + ["--profile", profile] + if semantic_version: + command_list = command_list + ["--semantic-version", semantic_version] + return command_list diff --git a/tests/integration/publish/test_command_integ.py b/tests/integration/publish/test_command_integ.py index d50c58ba15..b724d2c8bc 100644 --- a/tests/integration/publish/test_command_integ.py +++ b/tests/integration/publish/test_command_integ.py @@ -5,6 +5,7 @@ from unittest import skipIf +from samcli.commands.publish.command import SEMANTIC_VERSION from .publish_app_integ_base import PublishAppIntegBase # Publish tests require credentials and Travis will only add credentials to the env if the PR is from the same repo. @@ -59,6 +60,23 @@ def test_create_application_version(self): app_metadata = json.loads(app_metadata_text) self.assert_metadata_details(app_metadata, process_stdout.decode('utf-8')) + def test_create_application_version_with_semantic_version_option(self): + template_path = self.temp_dir.joinpath("template_create_app_version.yaml") + command_list = self.get_command_list( + template_path=template_path, region=self.region_name, semantic_version='0.1.0') + + process = Popen(command_list, stdout=PIPE) + process.wait() + process_stdout = b"".join(process.stdout.readlines()).strip() + + expected_msg = 'The following metadata of application "{}" has been updated:'.format(self.application_id) + self.assertIn(expected_msg, process_stdout.decode('utf-8')) + + app_metadata_text = self.temp_dir.joinpath("metadata_create_app_version.json").read_text() + app_metadata = json.loads(app_metadata_text) + app_metadata[SEMANTIC_VERSION] = '0.1.0' + self.assert_metadata_details(app_metadata, process_stdout.decode('utf-8')) + @skipIf(SKIP_PUBLISH_TESTS, "Skip publish tests in Travis only") class TestPublishNewApp(PublishAppIntegBase): diff --git a/tests/unit/commands/publish/test_command.py b/tests/unit/commands/publish/test_command.py index 72bdbc6623..f0f82c1c4d 100644 --- a/tests/unit/commands/publish/test_command.py +++ b/tests/unit/commands/publish/test_command.py @@ -3,11 +3,11 @@ from unittest import TestCase from mock import patch, call, Mock -from botocore.exceptions import ClientError -from serverlessrepo.exceptions import ServerlessRepoError +from serverlessrepo.exceptions import ServerlessRepoError, InvalidS3UriError from serverlessrepo.publish import CREATE_APPLICATION, UPDATE_APPLICATION +from serverlessrepo.parser import METADATA, SERVERLESS_REPO_APPLICATION -from samcli.commands.publish.command import do_cli as publish_cli +from samcli.commands.publish.command import do_cli as publish_cli, SEMANTIC_VERSION from samcli.commands.exceptions import UserException @@ -26,7 +26,7 @@ def setUp(self): def test_must_raise_if_value_error(self, click_mock, get_template_data_mock): get_template_data_mock.side_effect = ValueError("Template not found") with self.assertRaises(UserException) as context: - publish_cli(self.ctx_mock, self.template) + publish_cli(self.ctx_mock, self.template, None) message = str(context.exception) self.assertEqual("Template not found", message) @@ -38,42 +38,20 @@ def test_must_raise_if_value_error(self, click_mock, get_template_data_mock): def test_must_raise_if_serverlessrepo_error(self, click_mock, publish_application_mock): publish_application_mock.side_effect = ServerlessRepoError() with self.assertRaises(UserException): - publish_cli(self.ctx_mock, self.template) + publish_cli(self.ctx_mock, self.template, None) click_mock.secho.assert_called_with("Publish Failed", fg="red") @patch('samcli.commands.publish.command.get_template_data', Mock(return_value={})) @patch('samcli.commands.publish.command.publish_application') @patch('samcli.commands.publish.command.click') - def test_must_raise_if_s3_uri_error(self, click_mock, publish_application_mock): - publish_application_mock.side_effect = ClientError( - { - 'Error': { - 'Code': 'BadRequestException', - 'Message': 'Invalid S3 URI' - } - }, - 'create_application' - ) + def test_must_raise_if_invalid_S3_uri_error(self, click_mock, publish_application_mock): + publish_application_mock.side_effect = InvalidS3UriError(message="") with self.assertRaises(UserException) as context: - publish_cli(self.ctx_mock, self.template) + publish_cli(self.ctx_mock, self.template, None) message = str(context.exception) - self.assertIn("Please make sure that you have uploaded application artifacts " - "to S3 by packaging the template", message) - click_mock.secho.assert_called_with("Publish Failed", fg="red") - - @patch('samcli.commands.publish.command.get_template_data', Mock(return_value={})) - @patch('samcli.commands.publish.command.publish_application') - @patch('samcli.commands.publish.command.click') - def test_must_raise_if_not_s3_uri_error(self, click_mock, publish_application_mock): - publish_application_mock.side_effect = ClientError( - {'Error': {'Code': 'OtherError', 'Message': 'OtherMessage'}}, - 'other_operation' - ) - with self.assertRaises(ClientError): - publish_cli(self.ctx_mock, self.template) - + self.assertTrue("Your SAM template contains invalid S3 URIs" in message) click_mock.secho.assert_called_with("Publish Failed", fg="red") @patch('samcli.commands.publish.command.get_template_data', Mock(return_value={})) @@ -86,7 +64,7 @@ def test_must_succeed_to_create_application(self, click_mock, publish_applicatio 'actions': [CREATE_APPLICATION] } - publish_cli(self.ctx_mock, self.template) + publish_cli(self.ctx_mock, self.template, None) details_str = json.dumps({'attr1': 'value1'}, indent=2) expected_msg = "Created new application with the following metadata:\n{}" expected_link = self.console_link.format( @@ -95,7 +73,7 @@ def test_must_succeed_to_create_application(self, click_mock, publish_applicatio ) click_mock.secho.assert_has_calls([ call("Publish Succeeded", fg="green"), - call(expected_msg.format(details_str), fg="yellow"), + call(expected_msg.format(details_str)), call(expected_link, fg="yellow") ]) @@ -109,7 +87,7 @@ def test_must_succeed_to_update_application(self, click_mock, publish_applicatio 'actions': [UPDATE_APPLICATION] } - publish_cli(self.ctx_mock, self.template) + publish_cli(self.ctx_mock, self.template, None) details_str = json.dumps({'attr1': 'value1'}, indent=2) expected_msg = 'The following metadata of application "{}" has been updated:\n{}' expected_link = self.console_link.format( @@ -118,7 +96,7 @@ def test_must_succeed_to_update_application(self, click_mock, publish_applicatio ) click_mock.secho.assert_has_calls([ call("Publish Succeeded", fg="green"), - call(expected_msg.format(self.application_id, details_str), fg="yellow"), + call(expected_msg.format(self.application_id, details_str)), call(expected_link, fg="yellow") ]) @@ -139,9 +117,49 @@ def test_print_console_link_if_context_region_not_set(self, click_mock, boto3_mo session_mock.region_name = "us-west-1" boto3_mock.Session.return_value = session_mock - publish_cli(self.ctx_mock, self.template) + publish_cli(self.ctx_mock, self.template, None) expected_link = self.console_link.format( session_mock.region_name, self.application_id.replace('/', '~') ) click_mock.secho.assert_called_with(expected_link, fg="yellow") + + @patch('samcli.commands.publish.command.get_template_data') + @patch('samcli.commands.publish.command.publish_application') + def test_must_use_template_semantic_version(self, publish_application_mock, + get_template_data_mock): + template_data = { + METADATA: { + SERVERLESS_REPO_APPLICATION: {SEMANTIC_VERSION: '0.1'} + } + } + get_template_data_mock.return_value = template_data + publish_application_mock.return_value = { + 'application_id': self.application_id, + 'details': {}, 'actions': {} + } + publish_cli(self.ctx_mock, self.template, None) + publish_application_mock.assert_called_with(template_data) + + @patch('samcli.commands.publish.command.get_template_data') + @patch('samcli.commands.publish.command.publish_application') + def test_must_override_template_semantic_version(self, publish_application_mock, + get_template_data_mock): + template_data = { + METADATA: { + SERVERLESS_REPO_APPLICATION: {SEMANTIC_VERSION: '0.1'} + } + } + get_template_data_mock.return_value = template_data + publish_application_mock.return_value = { + 'application_id': self.application_id, + 'details': {}, 'actions': {} + } + + publish_cli(self.ctx_mock, self.template, '0.2') + expected_template_data = { + METADATA: { + SERVERLESS_REPO_APPLICATION: {SEMANTIC_VERSION: '0.2'} + } + } + publish_application_mock.assert_called_with(expected_template_data) From a6c845cc20fda84d3b94e197bc1337cfb2b21874 Mon Sep 17 00:00:00 2001 From: Sanath Kumar Ramesh Date: Mon, 11 Mar 2019 12:35:45 -0700 Subject: [PATCH 5/5] chore: bump version to 0.13.0 (#1052) --- samcli/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samcli/__init__.py b/samcli/__init__.py index dae37706fb..3deaf99f01 100644 --- a/samcli/__init__.py +++ b/samcli/__init__.py @@ -2,4 +2,4 @@ SAM CLI version """ -__version__ = '0.12.0' +__version__ = '0.13.0'