Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add lightweight transformer cli #459

Merged
merged 15 commits into from
Jun 7, 2018
Merged

feat(cli): add lightweight transformer cli #459

merged 15 commits into from
Jun 7, 2018

Conversation

brettstack
Copy link
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
--output-file=<o> Location to store resulting CloudFormation template [default: cfn-template.json].

"""
import os
Copy link
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.

#!/usr/bin/env python2

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

Choose a reason for hiding this comment

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

Put a blank line between the summary and this line: https://www.python.org/dev/peps/pep-0257/#multi-line-docstrings

cli_options = docopt(__doc__, sys.argv[1:])
iam_client = boto3.client('iam')

if __name__ == '__main__':
Copy link
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()

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
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)

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)
Copy link
Contributor

@jlhood jlhood Jun 7, 2018

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.

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')
Copy link
Contributor

@jlhood jlhood Jun 7, 2018

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)

.. _installation instructions: https://github.com/pyenv/pyenv#installation

Profiling
Copy link
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!

Copy link
Contributor

@jlhood jlhood left a comment

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.


import boto3

from docopt import docopt
Copy link
Contributor

@jlhood jlhood Jun 7, 2018

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?

cwd = os.getcwd()


def getInputOutputFilePaths():
Copy link
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

from samtranslator.translator.transform import transform
from samtranslator.yaml_helper import yaml_parse

cli_options = docopt(__doc__, sys.argv[1:])
Copy link
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?


def main():
input_file_path, output_file_path = getInputOutputFilePaths()
sam_template = yaml_parse(open(input_file_path, 'r'))
Copy link
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)

Copy link
Contributor

@jlhood jlhood left a comment

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.

Copy link
Contributor

@jlhood jlhood left a comment

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