-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(plugins): added DefaultDefinitionBody plugin #541
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
samtranslator/plugins/api/default_definition_body_plugin.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from samtranslator.plugins import BasePlugin | ||
| from samtranslator.swagger.swagger import SwaggerEditor | ||
| from samtranslator.public.sdk.resource import SamResourceType | ||
| from samtranslator.public.sdk.template import SamTemplate | ||
|
|
||
|
|
||
| class DefaultDefinitionBodyPlugin(BasePlugin): | ||
| """ | ||
| If the user does not provide a DefinitionBody or DefinitionUri | ||
| on an AWS::Serverless::Api resource, the Swagger constructed by | ||
| SAM is used. It accomplishes this by simply setting DefinitionBody | ||
| to a minimum Swagger definition and sets `__MANAGE_SWAGGER: true`. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| """ | ||
| Initialize the plugin. | ||
| """ | ||
|
|
||
| super(DefaultDefinitionBodyPlugin, self).__init__(DefaultDefinitionBodyPlugin.__name__) | ||
|
|
||
| def on_before_transform_template(self, template_dict): | ||
| """ | ||
| Hook method that gets called before the SAM template is processed. | ||
| The template has pass the validation and is guaranteed to contain a non-empty "Resources" section. | ||
| :param dict template_dict: Dictionary of the SAM template | ||
| :return: Nothing | ||
| """ | ||
| template = SamTemplate(template_dict) | ||
|
|
||
| for logicalId, api in template.iterate(SamResourceType.Api.value): | ||
| if api.properties.get('DefinitionBody') or api.properties.get('DefinitionUri'): | ||
| continue | ||
|
|
||
| api.properties['DefinitionBody'] = SwaggerEditor.gen_skeleton() | ||
| api.properties['__MANAGE_SWAGGER'] = True | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from unittest import TestCase | ||
| from mock import Mock, patch | ||
keetonian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from samtranslator.plugins.api.default_definition_body_plugin import DefaultDefinitionBodyPlugin | ||
| from samtranslator.public.plugins import BasePlugin | ||
|
|
||
| IMPLICIT_API_LOGICAL_ID = "ServerlessRestApi" | ||
|
|
||
|
|
||
| class TestDefaultDefinitionBodyPlugin_init(TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.plugin = DefaultDefinitionBodyPlugin() | ||
|
|
||
| def test_plugin_must_setup_correct_name(self): | ||
| # Name is the class name | ||
| expected_name = "DefaultDefinitionBodyPlugin" | ||
|
|
||
| self.assertEquals(self.plugin.name, expected_name) | ||
|
|
||
| def test_plugin_must_be_instance_of_base_plugin_class(self): | ||
| self.assertTrue(isinstance(self.plugin, BasePlugin)) | ||
|
|
||
|
|
||
| class TestDefaultDefinitionBodyPlugin_on_before_transform_template(TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.plugin = DefaultDefinitionBodyPlugin() | ||
|
|
||
| @patch("samtranslator.plugins.api.default_definition_body_plugin.SamTemplate") | ||
| def test_must_process_functions(self, SamTemplateMock): | ||
|
|
||
| template_dict = {"a": "b"} | ||
| api_resources = [("id1", ApiResource()), ("id2", ApiResource()), ("id3", ApiResource())] | ||
|
|
||
| sam_template = Mock() | ||
| SamTemplateMock.return_value = sam_template | ||
| sam_template.iterate = Mock() | ||
| sam_template.iterate.return_value = api_resources | ||
|
|
||
| self.plugin.on_before_transform_template(template_dict) | ||
|
|
||
| SamTemplateMock.assert_called_with(template_dict) | ||
|
|
||
| # Make sure this is called only for Apis | ||
| sam_template.iterate.assert_called_with("AWS::Serverless::Api") | ||
|
|
||
|
|
||
| class ApiResource(object): | ||
| def __init__(self): | ||
| self.properties = {} | ||
34 changes: 34 additions & 0 deletions
34
tests/translator/input/api_with_cors_no_definitionbody.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| AWSTemplateFormatVersion: '2010-09-09' | ||
| Transform: AWS::Serverless-2016-10-31 | ||
| Globals: | ||
| Api: | ||
| # If we skip AllowMethods, then SAM will auto generate a list of methods scoped to each path | ||
| Cors: "origins" | ||
|
|
||
| Resources: | ||
| ImplicitApiFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| CodeUri: s3://sam-demo-bucket/member_portal.zip | ||
| Handler: index.gethtml | ||
| Runtime: nodejs4.3 | ||
| Events: | ||
| GetHtml: | ||
| Type: Api | ||
| Properties: | ||
| RestApiId: !Ref ExplicitApi | ||
| Path: / | ||
| Method: get | ||
|
|
||
| PostHtml: | ||
| Type: Api | ||
| Properties: | ||
| RestApiId: !Ref ExplicitApi | ||
| Path: / | ||
| Method: post | ||
|
|
||
|
|
||
| ExplicitApi: | ||
| Type: AWS::Serverless::Api | ||
| Properties: | ||
| StageName: Prod |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.