-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
…tchSize from 200 to 10 (max)
bin/sam-translate.py
Outdated
--output-file=<o> Location to store resulting CloudFormation template [default: cfn-template.json]. | ||
|
||
""" | ||
import os |
There was a problem hiding this comment.
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:
- Standard library imports.
- Related third party imports.
- Local application/library specific imports.
You should put a blank line between each group of imports.
bin/sam-translate.py
Outdated
#!/usr/bin/env python2 | ||
|
||
"""Convert SAM templates to CloudFormation templates. | ||
Known limitations: cannot transform CodeUri pointing at local directory. |
There was a problem hiding this comment.
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
bin/sam-translate.py
Outdated
cli_options = docopt(__doc__, sys.argv[1:]) | ||
iam_client = boto3.client('iam') | ||
|
||
if __name__ == '__main__': |
There was a problem hiding this comment.
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()
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')) |
There was a problem hiding this comment.
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)
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) |
There was a problem hiding this comment.
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.
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') |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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!
There was a problem hiding this 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.
bin/sam-translate.py
Outdated
|
||
import boto3 | ||
|
||
from docopt import docopt |
There was a problem hiding this comment.
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?
bin/sam-translate.py
Outdated
cwd = os.getcwd() | ||
|
||
|
||
def getInputOutputFilePaths(): |
There was a problem hiding this comment.
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
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:]) |
There was a problem hiding this comment.
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?
bin/sam-translate.py
Outdated
|
||
def main(): | ||
input_file_path, output_file_path = getInputOutputFilePaths() | ||
sam_template = yaml_parse(open(input_file_path, 'r')) |
There was a problem hiding this comment.
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)
There was a problem hiding this 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.
There was a problem hiding this 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!
* 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)
* 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)
Issue #, if available:
Description of changes:
bin/sam-translate.py --input-file=sam-template.yaml
outputs acfn-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.