Skip to content

Commit

Permalink
resolving merge conflicts with pr aws#1104
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreya Gangishetty committed Sep 4, 2019
2 parents ff2e46a + fd718bb commit c1352fb
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 106 deletions.
2 changes: 1 addition & 1 deletion docs/internals/generated_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CloudFormation Resources Generated By SAM
:local:
:backlinks: none

When you create a Serverless Function or a Serverlesss API, SAM will create additional AWS resources to wire everything up.
When you create a Serverless Function or a Serverless API, SAM will create additional AWS resources to wire everything up.
For example, when you create a ``AWS::Serverless::Function``, SAM will create a Lambda Function resource
along with an IAM Role resource to give appropriate permissions for your function. This document describes all
such generated resources, how they are named, and how to refer to them in your SAM template.
Expand Down
18 changes: 9 additions & 9 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from collections import namedtuple
from six import string_types
import re

from samtranslator.model.intrinsics import ref
from samtranslator.model.apigateway import (ApiGatewayDeployment, ApiGatewayRestApi,
ApiGatewayStage, ApiGatewayAuthorizer,
Expand Down Expand Up @@ -107,9 +105,10 @@ def _construct_rest_api(self):
"Specify either 'DefinitionUri' or 'DefinitionBody' property and not both")

if self.open_api_version:
if re.match(SwaggerEditor.get_openapi_versions_supported_regex(), self.open_api_version) is None:
raise InvalidResourceException(
self.logical_id, "The OpenApiVersion value must be of the format 3.0.0")
if not SwaggerEditor.safe_compare_regex_with_string(SwaggerEditor.get_openapi_versions_supported_regex(),
self.open_api_version):
raise InvalidResourceException(self.logical_id,
"The OpenApiVersion value must be of the format \"3.0.0\"")

self._add_cors()
self._add_auth()
Expand Down Expand Up @@ -412,11 +411,12 @@ def _openapi_postprocess(self, definition_body):
if definition_body.get('swagger') is not None:
return definition_body

if definition_body.get('openapi') is not None:
if self.open_api_version is None:
self.open_api_version = definition_body.get('openapi')
if definition_body.get('openapi') is not None and self.open_api_version is None:
self.open_api_version = definition_body.get('openapi')

if self.open_api_version and re.match(SwaggerEditor.get_openapi_version_3_regex(), self.open_api_version):
if self.open_api_version and \
SwaggerEditor.safe_compare_regex_with_string(SwaggerEditor.get_openapi_version_3_regex(),
self.open_api_version):
if definition_body.get('securityDefinitions'):
components = definition_body.get('components', {})
components['securitySchemes'] = definition_body['securityDefinitions']
Expand Down
9 changes: 6 additions & 3 deletions samtranslator/plugins/globals/globals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from samtranslator.public.sdk.resource import SamResourceType
from samtranslator.public.intrinsics import is_intrinsics
from samtranslator.swagger.swagger import SwaggerEditor
import re
from six import string_types


class Globals(object):
Expand Down Expand Up @@ -135,8 +135,11 @@ def fix_openapi_definitions(cls, template):
if ("Type" in resource) and (resource["Type"] == cls._API_TYPE):
properties = resource["Properties"]
if (cls._OPENAPIVERSION in properties) and (cls._MANAGE_SWAGGER in properties) and \
(re.match(SwaggerEditor.get_openapi_version_3_regex(),
properties[cls._OPENAPIVERSION]) is not None):
SwaggerEditor.safe_compare_regex_with_string(
SwaggerEditor.get_openapi_version_3_regex(), properties[cls._OPENAPIVERSION]):
if not isinstance(properties[cls._OPENAPIVERSION], string_types):
properties[cls._OPENAPIVERSION] = str(properties[cls._OPENAPIVERSION])
resource["Properties"] = properties
if "DefinitionBody" in properties:
definition_body = properties['DefinitionBody']
definition_body['openapi'] = properties[cls._OPENAPIVERSION]
Expand Down
12 changes: 8 additions & 4 deletions samtranslator/swagger/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,8 @@ def add_request_model_to_method(self, path, method_name, request_model):
method_definition['parameters'] = existing_parameters

elif self._doc.get("openapi") and \
re.search(SwaggerEditor.get_openapi_version_3_regex(), self._doc["openapi"]) is not None:

SwaggerEditor.safe_compare_regex_with_string(
SwaggerEditor.get_openapi_version_3_regex(), self._doc["openapi"]):
method_definition['requestBody'] = {
'content': {
"application/json": {
Expand Down Expand Up @@ -901,8 +901,8 @@ def is_valid(data):
if bool(data.get("swagger")):
return True
elif bool(data.get("openapi")):
return re.search(SwaggerEditor.get_openapi_version_3_regex(), data["openapi"]) is not None
return False
return SwaggerEditor.safe_compare_regex_with_string(
SwaggerEditor.get_openapi_version_3_regex(), data["openapi"])
return False

@staticmethod
Expand Down Expand Up @@ -951,3 +951,7 @@ def get_openapi_versions_supported_regex():
def get_openapi_version_3_regex():
openapi_version_3_regex = r"\A3(\.\d)(\.\d)?$"
return openapi_version_3_regex

@staticmethod
def safe_compare_regex_with_string(regex, data):
return re.match(regex, str(data)) is not None
1 change: 0 additions & 1 deletion samtranslator/translator/translator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import copy

from samtranslator.model import ResourceTypeResolver, sam_resources
from samtranslator.translator.verify_logical_id import verify_unique_logical_id
from samtranslator.model.preferences.deployment_preference_collection import DeploymentPreferenceCollection
Expand Down
2 changes: 1 addition & 1 deletion tests/translator/input/api_request_model_openapi_3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Resources:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
OpenApiVersion: '3.0.1'
OpenApiVersion: 3.0
Models:
User:
type: object
Expand Down
2 changes: 1 addition & 1 deletion tests/translator/input/api_with_open_api_version.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Globals:
Api:
OpenApiVersion: '3.0.1'
OpenApiVersion: 3.0.1
Cors: '*'

Resources:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
DefinitionBody: {}
OpenApiVersion: 3
StageName: 'prod'
Function:
Type: AWS::Serverless::Function
Properties:
Handler: lambda.handler
CodeUri: s3://bucket/api
Runtime: nodejs8.10
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId:
Ref: MyApi
Path: "/"
Method: ANY
22 changes: 11 additions & 11 deletions tests/translator/output/api_request_model_openapi_3.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{
"Resources": {
"HtmlApiDeploymentefb667b26e": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HtmlApi"
},
"Description": "RestApi deployment id: efb667b26e8a0b0f733f5dfc27d039c1a2867db0"
}
},
"HtmlFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -37,7 +28,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "HtmlApiDeploymentefb667b26e"
"Ref": "HtmlApiDeployment59eeb787ee"
},
"RestApiId": {
"Ref": "HtmlApi"
Expand Down Expand Up @@ -128,7 +119,7 @@
}
}
},
"openapi": "3.0.1",
"openapi": "3.0",
"components": {
"securitySchemes": {
"AWS_IAM": {
Expand All @@ -151,6 +142,15 @@
}
}
}
},
"HtmlApiDeployment59eeb787ee": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HtmlApi"
},
"Description": "RestApi deployment id: 59eeb787ee1561329a07e10162ac3718998e9f91"
}
},
"HtmlFunctionGetHtmlPermissionProd": {
"Type": "AWS::Lambda::Permission",
Expand Down
14 changes: 7 additions & 7 deletions tests/translator/output/aws-cn/api_request_model_openapi_3.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"Resources": {
"HtmlApiDeploymentbe02cbff83": {
"Type": "AWS::ApiGateway::Deployment",
"HtmlApiDeployment5ae7e42cea": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HtmlApi"
},
"Description": "RestApi deployment id: be02cbff831c2acb5672650bc54b204366bef429"
},
"Description": "RestApi deployment id: 5ae7e42cea0640a3318bf508535850cd792a52dc"
}
},
},
"HtmlFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -37,7 +37,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "HtmlApiDeploymentbe02cbff83"
"Ref": "HtmlApiDeployment5ae7e42cea"
},
"RestApiId": {
"Ref": "HtmlApi"
Expand Down Expand Up @@ -128,7 +128,7 @@
}
}
},
"openapi": "3.0.1",
"openapi": "3.0",
"components": {
"securitySchemes": {
"AWS_IAM": {
Expand Down
Loading

0 comments on commit c1352fb

Please sign in to comment.