Skip to content

Commit bb272e0

Browse files
committed
fix APIGW regression in moto
1 parent 81f6575 commit bb272e0

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

localstack-core/localstack/services/apigateway/helpers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from jsonpatch import apply_patch
1515
from jsonpointer import JsonPointerException
1616
from moto.apigateway import models as apigw_models
17-
from moto.apigateway.models import Integration, Resource, apigateway_backends
17+
from moto.apigateway.models import APIGatewayBackend, Integration, Resource, apigateway_backends
1818
from moto.apigateway.models import RestAPI as MotoRestAPI
1919
from moto.apigateway.utils import create_id as create_resource_id
2020
from requests.models import Response
@@ -139,6 +139,10 @@ def get_apigateway_store_for_invocation(context: ApiInvocationContext) -> ApiGat
139139
return apigateway_stores[account_id][region_name]
140140

141141

142+
def get_moto_backend(account_id: str, region: str) -> APIGatewayBackend:
143+
return apigw_models.apigateway_backends[account_id][region]
144+
145+
142146
def get_moto_rest_api(context: RequestContext, rest_api_id: str) -> MotoRestAPI:
143147
moto_backend = apigw_models.apigateway_backends[context.account_id][context.region]
144148
if rest_api := moto_backend.apis.get(rest_api_id):

localstack-core/localstack/services/apigateway/provider.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
from copy import deepcopy
77
from datetime import datetime
8+
from operator import itemgetter
89
from typing import IO, Any
910

1011
from moto.apigateway import models as apigw_models
@@ -99,6 +100,7 @@
99100
OpenAPIExt,
100101
apply_json_patch_safe,
101102
get_apigateway_store,
103+
get_moto_backend,
102104
get_moto_rest_api,
103105
get_regional_domain_name,
104106
get_rest_api_container,
@@ -725,7 +727,7 @@ def put_method(
725727
**kwargs,
726728
) -> Method:
727729
# TODO: add missing validation? check order of validation as well
728-
moto_backend = apigw_models.apigateway_backends[context.account_id][context.region]
730+
moto_backend = get_moto_backend(context.account_id, context.region)
729731
moto_rest_api: MotoRestAPI = moto_backend.apis.get(rest_api_id)
730732
if not moto_rest_api or not (moto_resource := moto_rest_api.resources.get(resource_id)):
731733
raise NotFoundException("Invalid Resource identifier specified")
@@ -792,7 +794,7 @@ def update_method(
792794
) -> Method:
793795
# see https://www.linkedin.com/pulse/updating-aws-cli-patch-operations-rest-api-yitzchak-meirovich/
794796
# for path construction
795-
moto_backend = apigw_models.apigateway_backends[context.account_id][context.region]
797+
moto_backend = get_moto_backend(context.account_id, context.region)
796798
moto_rest_api: MotoRestAPI = moto_backend.apis.get(rest_api_id)
797799
if not moto_rest_api or not (moto_resource := moto_rest_api.resources.get(resource_id)):
798800
raise NotFoundException("Invalid Resource identifier specified")
@@ -907,7 +909,7 @@ def delete_method(
907909
http_method: String,
908910
**kwargs,
909911
) -> None:
910-
moto_backend = apigw_models.apigateway_backends[context.account_id][context.region]
912+
moto_backend = get_moto_backend(context.account_id, context.region)
911913
moto_rest_api: MotoRestAPI = moto_backend.apis.get(rest_api_id)
912914
if not moto_rest_api or not (moto_resource := moto_rest_api.resources.get(resource_id)):
913915
raise NotFoundException("Invalid Resource identifier specified")
@@ -929,7 +931,7 @@ def get_method_response(
929931
**kwargs,
930932
) -> MethodResponse:
931933
# this could probably be easier in a patch?
932-
moto_backend = apigw_models.apigateway_backends[context.account_id][context.region]
934+
moto_backend = get_moto_backend(context.account_id, context.region)
933935
moto_rest_api: MotoRestAPI = moto_backend.apis.get(rest_api_id)
934936
# TODO: snapshot test different possibilities
935937
if not moto_rest_api or not (moto_resource := moto_rest_api.resources.get(resource_id)):
@@ -1002,7 +1004,7 @@ def update_stage(
10021004
) -> Stage:
10031005
call_moto(context)
10041006

1005-
moto_backend = apigw_models.apigateway_backends[context.account_id][context.region]
1007+
moto_backend = get_moto_backend(context.account_id, context.region)
10061008
moto_rest_api: MotoRestAPI = moto_backend.apis.get(rest_api_id)
10071009
if not (moto_stage := moto_rest_api.stages.get(stage_name)):
10081010
raise NotFoundException("Invalid Stage identifier specified")
@@ -2076,25 +2078,26 @@ def get_api_keys(
20762078
include_values: NullableBoolean = None,
20772079
**kwargs,
20782080
) -> ApiKeys:
2079-
moto_response: ApiKeys = call_moto(context=context)
2080-
item_list = PaginatedList(moto_response["items"])
2081+
# TODO: migrate API keys in our store
2082+
moto_backend = get_moto_backend(context.account_id, context.region)
2083+
api_keys = [api_key.to_json() for api_key in moto_backend.keys.values()]
2084+
if not include_values:
2085+
for api_key in api_keys:
2086+
api_key.pop("value")
20812087

2082-
def token_generator(item):
2083-
return item["id"]
2088+
item_list = PaginatedList(api_keys)
20842089

20852090
def filter_function(item):
20862091
return item["name"].startswith(name_query)
20872092

20882093
paginated_list, next_token = item_list.get_page(
2089-
token_generator=token_generator,
2094+
token_generator=itemgetter("id"),
20902095
next_token=position,
20912096
page_size=limit,
20922097
filter_function=filter_function if name_query else None,
20932098
)
20942099

2095-
return ApiKeys(
2096-
items=paginated_list, warnings=moto_response.get("warnings"), position=next_token
2097-
)
2100+
return ApiKeys(items=paginated_list, position=next_token)
20982101

20992102
def update_api_key(
21002103
self,

0 commit comments

Comments
 (0)