Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 83 additions & 84 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ def to_cloudformation(self, **kwargs):
resources.extend(self._get_permissions(kwargs))

explicit_api = kwargs["explicit_api"]
self._add_swagger_integration(explicit_api, function)
if explicit_api.get("__MANAGE_SWAGGER"):
self._add_swagger_integration(explicit_api, function)

return resources

Expand Down Expand Up @@ -619,21 +620,21 @@ def _add_swagger_integration(self, api, function):
)

editor = SwaggerEditor(swagger_body)
if api.get("__MANAGE_SWAGGER"):
if editor.has_integration(self.Path, self.Method):
# Cannot add the Lambda Integration, if it is already present
raise InvalidEventException(
self.relative_id,
'API method "{method}" defined multiple times for path "{path}".'.format(
method=self.Method, path=self.Path
),
)

condition = None
if CONDITION in function.resource_attributes:
condition = function.resource_attributes[CONDITION]
if editor.has_integration(self.Path, self.Method):
# Cannot add the Lambda Integration, if it is already present
raise InvalidEventException(
self.relative_id,
'API method "{method}" defined multiple times for path "{path}".'.format(
method=self.Method, path=self.Path
),
)

condition = None
if CONDITION in function.resource_attributes:
condition = function.resource_attributes[CONDITION]

editor.add_lambda_integration(self.Path, self.Method, uri, self.Auth, api.get("Auth"), condition=condition)
editor.add_lambda_integration(self.Path, self.Method, uri, self.Auth, api.get("Auth"), condition=condition)

if self.Auth:
method_authorizer = self.Auth.get("Authorizer")
Expand Down Expand Up @@ -690,100 +691,98 @@ def _add_swagger_integration(self, api, function):
)

if method_authorizer or apikey_required_setting is not None:
if editor.has_path(self.Path):
editor.add_auth_to_method(api=api, path=self.Path, method_name=self.Method, auth=self.Auth)
editor.add_auth_to_method(api=api, path=self.Path, method_name=self.Method, auth=self.Auth)

if self.Auth.get("ResourcePolicy"):
resource_policy = self.Auth.get("ResourcePolicy")
editor.add_resource_policy(
resource_policy=resource_policy, path=self.Path, api_id=self.RestApiId.get("Ref"), stage=self.Stage
)
if api.get("__MANAGE_SWAGGER"):
if self.RequestModel:
method_model = self.RequestModel.get("Model")

if method_model:
api_models = api.get("Models")
if not api_models:
raise InvalidEventException(
self.relative_id,
"Unable to set RequestModel [{model}] on API method [{method}] for path [{path}] "
"because the related API does not define any Models.".format(
model=method_model, method=self.Method, path=self.Path
),
)

if not api_models.get(method_model):
raise InvalidEventException(
self.relative_id,
"Unable to set RequestModel [{model}] on API method [{method}] for path [{path}] "
"because it wasn't defined in the API's Models.".format(
model=method_model, method=self.Method, path=self.Path
),
)

editor.add_request_model_to_method(
path=self.Path, method_name=self.Method, request_model=self.RequestModel
if self.RequestModel:
method_model = self.RequestModel.get("Model")

if method_model:
api_models = api.get("Models")
if not api_models:
raise InvalidEventException(
self.relative_id,
"Unable to set RequestModel [{model}] on API method [{method}] for path [{path}] "
"because the related API does not define any Models.".format(
model=method_model, method=self.Method, path=self.Path
),
)

if self.RequestParameters:
if not api_models.get(method_model):
raise InvalidEventException(
self.relative_id,
"Unable to set RequestModel [{model}] on API method [{method}] for path [{path}] "
"because it wasn't defined in the API's Models.".format(
model=method_model, method=self.Method, path=self.Path
),
)

default_value = {"Required": False, "Caching": False}
editor.add_request_model_to_method(
path=self.Path, method_name=self.Method, request_model=self.RequestModel
)

parameters = []
for parameter in self.RequestParameters:
if self.RequestParameters:

if isinstance(parameter, dict):
default_value = {"Required": False, "Caching": False}

parameter_name, parameter_value = next(iter(parameter.items()))
parameters = []
for parameter in self.RequestParameters:

if not re.match("method\.request\.(querystring|path|header)\.", parameter_name):
raise InvalidEventException(
self.relative_id,
"Invalid value for 'RequestParameters' property. Keys must be in the format "
"'method.request.[querystring|path|header].{value}', "
"e.g 'method.request.header.Authorization'.",
)
if isinstance(parameter, dict):

if not isinstance(parameter_value, dict) or not all(
key in REQUEST_PARAMETER_PROPERTIES for key in parameter_value.keys()
):
raise InvalidEventException(
self.relative_id,
"Invalid value for 'RequestParameters' property. Values must be an object, "
"e.g { Required: true, Caching: false }",
)

settings = default_value.copy()
settings.update(parameter_value)
settings.update({"Name": parameter_name})
parameter_name, parameter_value = next(iter(parameter.items()))

parameters.append(settings)
if not re.match("method\.request\.(querystring|path|header)\.", parameter_name):
raise InvalidEventException(
self.relative_id,
"Invalid value for 'RequestParameters' property. Keys must be in the format "
"'method.request.[querystring|path|header].{value}', "
"e.g 'method.request.header.Authorization'.",
)

elif isinstance(parameter, string_types):
if not re.match("method\.request\.(querystring|path|header)\.", parameter):
raise InvalidEventException(
self.relative_id,
"Invalid value for 'RequestParameters' property. Keys must be in the format "
"'method.request.[querystring|path|header].{value}', "
"e.g 'method.request.header.Authorization'.",
)
if not isinstance(parameter_value, dict) or not all(
key in REQUEST_PARAMETER_PROPERTIES for key in parameter_value.keys()
):
raise InvalidEventException(
self.relative_id,
"Invalid value for 'RequestParameters' property. Values must be an object, "
"e.g { Required: true, Caching: false }",
)

settings = default_value.copy()
settings.update({"Name": parameter})
settings = default_value.copy()
settings.update(parameter_value)
settings.update({"Name": parameter_name})

parameters.append(settings)
parameters.append(settings)

else:
elif isinstance(parameter, string_types):
if not re.match("method\.request\.(querystring|path|header)\.", parameter):
raise InvalidEventException(
self.relative_id,
"Invalid value for 'RequestParameters' property. "
"Property must be either a string or an object",
"Invalid value for 'RequestParameters' property. Keys must be in the format "
"'method.request.[querystring|path|header].{value}', "
"e.g 'method.request.header.Authorization'.",
)

editor.add_request_parameters_to_method(
path=self.Path, method_name=self.Method, request_parameters=parameters
)
settings = default_value.copy()
settings.update({"Name": parameter})

parameters.append(settings)

else:
raise InvalidEventException(
self.relative_id,
"Invalid value for 'RequestParameters' property. Property must be either a string or an object",
)

editor.add_request_parameters_to_method(
path=self.Path, method_name=self.Method, request_parameters=parameters
)

api["DefinitionBody"] = editor.swagger

Expand Down
117 changes: 0 additions & 117 deletions tests/translator/input/api_with_swagger_authorizer_none.yaml

This file was deleted.

1 change: 0 additions & 1 deletion tests/translator/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ class TestTranslatorEndToEnd(TestCase):
"api_with_apikey_required",
"api_with_path_parameters",
"function_with_event_source_mapping",
"api_with_swagger_authorizer_none",
"function_with_event_dest",
"function_with_event_dest_basic",
"function_with_event_dest_conditional",
Expand Down