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
move import_rest_api patch into ASF handler #6048
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,15 @@ | |
import re | ||
from copy import deepcopy | ||
|
||
from localstack.aws.api import RequestContext, handler | ||
from localstack.aws.api import RequestContext, ServiceRequest, handler | ||
from localstack.aws.api.apigateway import ( | ||
Account, | ||
ApigatewayApi, | ||
Authorizer, | ||
Authorizers, | ||
BasePathMapping, | ||
BasePathMappings, | ||
Blob, | ||
Boolean, | ||
ClientCertificate, | ||
ClientCertificates, | ||
|
@@ -24,6 +25,7 @@ | |
MapOfStringToString, | ||
NotFoundException, | ||
NullableInteger, | ||
PutRestApiRequest, | ||
RequestValidator, | ||
RequestValidators, | ||
RestApi, | ||
|
@@ -32,6 +34,7 @@ | |
VpcLink, | ||
VpcLinks, | ||
) | ||
from localstack.aws.forwarder import create_aws_request_context | ||
from localstack.aws.proxy import AwsApiListener | ||
from localstack.constants import HEADER_LOCALSTACK_EDGE_URL | ||
from localstack.services.apigateway import helpers | ||
|
@@ -53,7 +56,7 @@ | |
from localstack.utils.aws.aws_responses import requests_response | ||
from localstack.utils.collections import ensure_list | ||
from localstack.utils.json import parse_json_or_yaml | ||
from localstack.utils.strings import short_uid, to_str | ||
from localstack.utils.strings import short_uid, str_to_bool, to_str | ||
from localstack.utils.time import now_utc | ||
|
||
|
||
|
@@ -638,12 +641,56 @@ def untag_resource( | |
for key in tag_keys: | ||
resource_tags.pop(key, None) | ||
|
||
def import_rest_api( | ||
self, | ||
context: RequestContext, | ||
body: Blob, | ||
fail_on_warnings: Boolean = None, | ||
parameters: MapOfStringToString = None, | ||
) -> RestApi: | ||
|
||
openapi_spec = parse_json_or_yaml(to_str(body)) | ||
response = _call_moto( | ||
context, | ||
"CreateRestApi", | ||
CreateRestApiRequest(name=openapi_spec.get("info").get("title")), | ||
) | ||
|
||
return _call_moto( | ||
context, | ||
"PutRestApi", | ||
PutRestApiRequest( | ||
restApiId=response.get("id"), | ||
failOnWarnings=str_to_bool(fail_on_warnings) or False, | ||
parameters=parameters or {}, | ||
body=body, | ||
), | ||
) | ||
|
||
|
||
# --------------- | ||
# UTIL FUNCTIONS | ||
# --------------- | ||
|
||
|
||
def _call_moto(context: RequestContext, operation_name: str, parameters: ServiceRequest): | ||
""" | ||
Not necessarily the pattern we want to follow in the future, but this makes possible to nest | ||
moto call and still be interface compatible. | ||
|
||
Ripped :call_moto_with_request: from moto.py but applicable to any operation (operation_name). | ||
""" | ||
local_context = create_aws_request_context( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! Looks quite useful - since this is generally applicable, I'd probably move this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this was result of a conversation with @thrau, we don't necessarilly think this will be the pattern to use, hence kept it close where it's used. Moving it to the |
||
service_name=context.service.service_name, | ||
action=operation_name, | ||
parameters=parameters, | ||
region=context.region, | ||
) | ||
|
||
local_context.request.headers.extend(context.request.headers) | ||
return call_moto(local_context) | ||
|
||
|
||
def normalize_authorizer(data): | ||
is_list = isinstance(data, list) | ||
entries = ensure_list(data) | ||
|
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.
Out of curiosity - can the runtime type of
fail_on_warnings
bestr
here, did you actually observe this behavior?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.
It's a string and easy observable running the following command.
awslocal apigateway import-rest-api --cli-binary-format raw-in-base64-out --fail-on-warnings --body 'file:///spec.json