Skip to content

feat(cli): add lightweight transformer cli - #459

Merged
brettstack merged 15 commits into
aws:developfrom
brettstack:feature/sqs-event
Jun 7, 2018
Merged

brettstack merged 15 commits into
aws:developfrom
brettstack:feature/sqs-event

Conversation

@brettstack

Copy link
Copy Markdown
Contributor

Issue #, if available:

Description of changes:
bin/sam-translate.py --input-file=sam-template.yaml outputs a cfn-template.json file (can specify output with --output-file=

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

ylynn
ylynn previously approved these changes Jun 7, 2018
Comment thread bin/sam-translate.py Outdated
--output-file=<o> Location to store resulting CloudFormation template [default: cfn-template.json].

"""
import os

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure if we're trying to follow PEP8 imports style guide, but if we are, the imports should be separated into 3 groups.

Imports should be grouped in the following order:

  1. Standard library imports.
  2. Related third party imports.
  3. Local application/library specific imports.

You should put a blank line between each group of imports.

Comment thread bin/sam-translate.py
#!/usr/bin/env python2

"""Convert SAM templates to CloudFormation templates.
Known limitations: cannot transform CodeUri pointing at local directory.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread bin/sam-translate.py Outdated
cli_options = docopt(__doc__, sys.argv[1:])
iam_client = boto3.client('iam')

if __name__ == '__main__':

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Put the logic in a main() method and then call it from the conditional:

if __name__ == '__main__':
    main()

Comment thread bin/sam-translate.py Outdated
output_file_option = cli_options.get('--output-file')
input_file_path = os.path.join(cwd, input_file_option)
output_file_path = os.path.join(cwd, output_file_option)
samTemplate = yaml_parse(open(input_file_path, 'r'))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

samTemplate -> sam_template

Also use blank lines to separate logical sections (arg parsing, transform template, write output)

Comment thread bin/sam-translate.py Outdated
samTemplate = yaml_parse(open(input_file_path, 'r'))
cloud_formation_template = transform(samTemplate, {}, ManagedPolicyLoader(iam_client))
cloud_formation_template_prettified = json.dumps(cloud_formation_template, indent=2)
print(cloud_formation_template_prettified)

@jlhood jlhood Jun 7, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove? Seems weird to print and write to an output file. If you really want to print it also, you should add a --quiet,-q option to suppress this behavior or conversely a --print option to enable it.

Comment thread bin/sam-translate.py Outdated
cloud_formation_template = transform(samTemplate, {}, ManagedPolicyLoader(iam_client))
cloud_formation_template_prettified = json.dumps(cloud_formation_template, indent=2)
print(cloud_formation_template_prettified)
cloud_formation_template_file = open(output_file_path, 'w')

@jlhood jlhood Jun 7, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unless there's a good reason not to, always use the with statement to ensure files are closed after use:

with open(output_file_path, 'w') as f:
    f.write(cloud_formation_template_prettified)

Comment thread DEVELOPMENT_GUIDE.rst
.. _installation instructions: https://github.com/pyenv/pyenv#installation No newline at end of file
.. _installation instructions: https://github.com/pyenv/pyenv#installation

Profiling

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Really appreciate you documenting this!

@jlhood jlhood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice. This is a super useful tool! Mostly style/readability comments.

Comment thread bin/sam-translate.py

import boto3

from docopt import docopt

@jlhood jlhood Jun 7, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

docopt should go in the same section as boto3, right?

Comment thread bin/sam-translate.py Outdated
cwd = os.getcwd()


def getInputOutputFilePaths():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

getInputOutputFilePaths -> get_input_output_file_paths

Comment thread bin/sam-translate.py Outdated
from samtranslator.translator.transform import transform
from samtranslator.yaml_helper import yaml_parse

cli_options = docopt(__doc__, sys.argv[1:])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do you need to pass the additional param?

Comment thread bin/sam-translate.py Outdated

def main():
input_file_path, output_file_path = getInputOutputFilePaths()
sam_template = yaml_parse(open(input_file_path, 'r'))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does yaml_parse close this file handle? Hopefully it doesn't and you should:

with open(input_file_path, 'r') as f:
    sam_template = yaml_parse(f)

@jlhood jlhood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a few more minor things, then it should be good to go.

@jlhood jlhood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great! Thanks for iterating!

@brettstack
brettstack merged commit 0e89b42 into aws:develop Jun 7, 2018
brettstack added a commit that referenced this pull request Jun 11, 2018
* doc: update DESIGN.md to include an Overview and entry point for SAM (#370)

* Update DESIGN.md to include an Overview and entry point for SAM

* Updated wording in DESIGN.md based on PR feedback

* docs(example): improve microservice-http-endpoint-python3 example (#415)

* Remove unnecessary index.js"

* Moved common config to template Globals and uncomment debug log.

* Add sample payload and commands for testing.

* Rename test script as a script.

* Exit from script after command.

* Simplify use case for easier startup.

- Move table creation into template.
- Eliminates need to provide table name in every REST call.
- Fix GET with no params.
- Split test.sh script from output log.

* Address requested changes.

* doc(example): update api_backend example to use ES6 arrow function immediate return (#446)

Since this code is using ES6 we can use the short version of returning a param

* feat(event-source): add SQS as Lambda event source (#451)

* feat(cli): add lightweight transformer cli (#459)

* bug: fix ANY method ARN generation using wildcard (#449)

* fix(event-source): fix SQS Queue schema to allow intrinsic functions (#465)

* fix: log a warning on invalid schema validation (#466)
brettstack added a commit that referenced this pull request Jun 12, 2018
* doc: update DESIGN.md to include an Overview and entry point for SAM (#370)

* Update DESIGN.md to include an Overview and entry point for SAM

* Updated wording in DESIGN.md based on PR feedback

* docs(example): improve microservice-http-endpoint-python3 example (#415)

* Remove unnecessary index.js"

* Moved common config to template Globals and uncomment debug log.

* Add sample payload and commands for testing.

* Rename test script as a script.

* Exit from script after command.

* Simplify use case for easier startup.

- Move table creation into template.
- Eliminates need to provide table name in every REST call.
- Fix GET with no params.
- Split test.sh script from output log.

* Address requested changes.

* doc(example): update api_backend example to use ES6 arrow function immediate return (#446)

Since this code is using ES6 we can use the short version of returning a param

* feat(event-source): add SQS as Lambda event source (#451)

* feat(cli): add lightweight transformer cli (#459)

* bug: fix ANY method ARN generation using wildcard (#449)

* fix(event-source): fix SQS Queue schema to allow intrinsic functions (#465)

* fix: log a warning on invalid schema validation (#466)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants