10000 APIGW: move current provider in legacy module by bentsku · Pull Request #11651 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

APIGW: move current provider in legacy module #11651

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 7 commits into from
Oct 14, 2024
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
710 changes: 6 additions & 704 deletions localstack-core/localstack/services/apigateway/helpers.py

Large diffs are not rendered by default.

Empty file.
711 changes: 711 additions & 0 deletions localstack-core/localstack/services/apigateway/legacy/helpers.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
dump_dto,
)
from localstack.constants import APPLICATION_JSON, HEADER_CONTENT_TYPE
from localstack.services.apigateway import helpers
from localstack.services.apigateway.context import ApiInvocationContext
from localstack.services.apigateway.helpers import (
from localstack.services.apigateway.legacy.context import ApiInvocationContext
from localstack.services.apigateway.legacy.helpers import (
ApiGatewayIntegrationError,
IntegrationParameters,
RequestParametersResolver,
ResponseParametersResolver,
extract_path_params,
extract_query_string_params,
get_event_request_context,
get_stage_variables,
make_error_response,
multi_value_dict_for_list,
)
from localstack.services.apigateway.templates import (
from localstack.services.apigateway.legacy.templates import (
MappingTemplates,
RequestTemplates,
ResponseTemplates,
Expand Down Expand Up @@ -437,8 +437,7 @@ def invoke(self, invocation_context: ApiInvocationContext):

class LambdaIntegration(BackendIntegration):
def invoke(self, invocation_context: ApiInvocationContext):
# invocation_context.context = helpers.get_event_request_context(invocation_context)
invocation_context.stage_variables = helpers.get_stage_variables(invocation_context)
invocation_context.stage_variables = get_stage_variables(invocation_context)
headers = invocation_context.headers

# resolve integration parameters
Expand Down Expand Up @@ -530,8 +529,8 @@ def invoke(self, invocation_context: ApiInvocationContext):
# want to refactor this into a model class.
# I'd argue we should not make a decision on the event_request_context inside the integration because,
# it's different between API types (REST, HTTP, WebSocket) and per event version
invocation_context.context = helpers.get_event_request_context(invocation_context)
invocation_context.stage_variables = helpers.get_stage_variables(invocation_context)
invocation_context.context = get_event_request_context(invocation_context)
invocation_context.stage_variables = get_stage_variables(invocation_context)

# integration type "AWS" is only supported for WebSocket APIs and REST
# API (v1), but the template selection expression is only supported for
Expand Down Expand Up @@ -787,8 +786,8 @@ def invoke(self, invocation_context: ApiInvocationContext):
uri = "http://%s/%s" % (instance["Id"], invocation_path.lstrip("/"))

# apply custom request template
invocation_context.context = helpers.get_event_request_context(invocation_context)
invocation_context.stage_variables = helpers.get_stage_variables(invocation_context)
invocation_context.context = get_event_request_context(invocation_context)
invocation_context.stage_variables = get_stage_variables(invocation_context)
payload = self.request_templates.render(invocation_context)

if isinstance(payload, dict):
Expand Down Expand Up @@ -875,7 +874,7 @@ class SNSIntegration(BackendIntegration):
def invoke(self, invocation_context: ApiInvocationContext) -> Response:
# TODO: check if the logic below is accurate - cover with snapshot tests!
invocation_context.context = get_event_request_context(invocation_context)
invocation_context.stage_variables = helpers.get_stage_variables(invocation_context)
invocation_context.stage_variables = get_stage_variables(invocation_context)
integration = invocation_context.integration
uri = integration.get("uri") or integration.get("integrationUri") or ""

10000 Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@

from localstack.aws.connect import connect_to
from localstack.constants import APPLICATION_JSON
from localstack.services.apigateway import helpers
from localstack.services.apigateway.context import ApiInvocationContext
from localstack.services.apigateway.helpers import (
EMPTY_MODEL,
ModelResolver,
get_apigateway_store_for_invocation,
)
from localstack.services.apigateway.legacy.context import ApiInvocationContext
from localstack.services.apigateway.legacy.helpers import (
get_cors_response,
get_event_request_context,
get_target_resource_details,
make_error_response,
set_api_id_stage_invocation_path,
)
from localstack.services.apigateway.integration import (
from localstack.services.apigateway.legacy.integration import (
ApiGatewayIntegrationError,
DynamoDBIntegration,
EventBridgeIntegration,
Expand Down Expand Up @@ -254,7 +258,7 @@ def update_content_length(response: Response):


def invoke_rest_api_from_request(invocation_context: ApiInvocationContext):
helpers.set_api_id_stage_invocation_path(invocation_context)
set_api_id_stage_invocation_path(invocation_context)
try:
return invoke_rest_api(invocation_context)
except AuthorizationError as e:
Expand All @@ -273,7 +277,7 @@ def invoke_rest_api(invocation_context: ApiInvocationContext):
method = invocation_context.method
headers = invocation_context.headers

extracted_path, resource = helpers.get_target_resource_details(invocation_context)
extracted_path, resource = get_target_resource_details(invocation_context)
if not resource:
return make_error_response("Unable to find path %s" % invocation_context.path, 404)

Expand Down Expand Up @@ -349,7 +353,7 @@ def invoke_rest_api_integration_backend(invocation_context: ApiInvocationContext
if (re.match(f"{ARN_PARTITION_REGEX}:apigateway:", uri) and ":lambda:path" in uri) or re.match(
f"{ARN_PARTITION_REGEX}:lambda", uri
):
invocation_context.context = helpers.get_event_request_context(invocation_context)
invocation_context.context = get_event_request_context(invocation_context)
if integration_type == "AWS_PROXY":
return LambdaProxyIntegration().invoke(invocation_context)
elif integration_type == "AWS":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@
is_greedy_path,
is_variable_path,
log_template,
multi_value_dict_for_list,
resolve_references,
)
from localstack.services.apigateway.invocations import invoke_rest_api_from_request
from localstack.services.apigateway.legacy.helpers import multi_value_dict_for_list
from localstack.services.apigateway.legacy.invocations import invoke_rest_api_from_request
from localstack.services.apigateway.legacy.router_asf import ApigatewayRouter, to_invocation_context
from localstack.services.apigateway.models import ApiGatewayStore, RestApiContainer
from localstack.services.apigateway.next_gen.execute_api.router import (
ApiGatewayRouter as ApiGatewayRouterNextGen,
)
from localstack.services.apigateway.patches import apply_patches
from localstack.services.apigateway.router_asf import ApigatewayRouter, to_invocation_context
from localstack.services.edge import ROUTER
from localstack.services.moto import call_moto, call_moto_with_request
from localstack.services.plugins import ServiceLifecycleHook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from localstack.http import Request, Response, Router
from localstack.http.dispatcher import Handler
from localstack.http.request import restore_payload
from localstack.services.apigateway.context import ApiInvocationContext
from localstack.services.apigateway.helpers import get_api_account_id_and_region
from localstack.services.apigateway.invocations import invoke_rest_api_from_request
from localstack.services.apigateway.legacy.context import ApiInvocationContext
from localstack.services.apigateway.legacy.helpers import get_api_account_id_and_region
from localstack.services.apigateway.legacy.invocations import invoke_rest_api_from_request
from localstack.utils.aws.aws_responses import LambdaResponse
from localstack.utils.strings import remove_leading_extra_slashes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

from localstack import config
from localstack.constants import APPLICATION_JSON, APPLICATION_XML
from localstack.services.apigateway.context import ApiInvocationContext
from localstack.services.apigateway.helpers import select_integration_response
from localstack.services.apigateway.legacy.context import ApiInvocationContext
from localstack.services.apigateway.legacy.helpers import select_integration_response
from localstack.utils.aws.templating import VelocityUtil, VtlTemplate
from localstack.utils.json import extract_jsonpath, json_safe, try_json
from localstack.utils.strings import to_str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
Stage,
StatusCode,
String,
TestInvokeMethodRequest,
TestInvokeMethodResponse,
)
from localstack.constants import AWS_REGION_US_EAST_1, DEFAULT_AWS_ACCOUNT_ID
from localstack.services.apigateway.helpers import (
get_apigateway_store,
get_moto_rest_api,
get_rest_api_container,
)
from localstack.services.apigateway.legacy.provider import ApigatewayProvider
from localstack.services.apigateway.patches import apply_patches
from localstack.services.apigateway.provider import ApigatewayProvider
from localstack.services.edge import ROUTER
from localstack.services.moto import call_moto

Expand Down Expand Up @@ -239,6 +241,12 @@ def get_gateway_responses(
]
return GatewayResponses(items=gateway_responses)

def test_invoke_method(
self, context: RequestContext, request: TestInvokeMethodRequest
) -> TestInvokeMethodResponse:
# TODO: rewrite and migrate to NextGen
return super().test_invoke_method(context, request)


def _get_gateway_response_or_default(
response_type: GatewayResponseType,
Expand Down
2 changes: 1 addition & 1 deletion localstack-core/localstack/services/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def acm():

@aws_provider(api="apigateway")
def apigateway():
from localstack.services.apigateway.provider import ApigatewayProvider
from localstack.services.apigateway.legacy.provider import ApigatewayProvider
from localstack.services.moto import MotoFallbackDispatcher

provider = ApigatewayProvider()
Expand Down
2 changes: 1 addition & 1 deletion localstack-core/localstack/utils/aws/message_forwarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from moto.events.models import events_backends

from localstack.aws.connect import connect_to
from localstack.services.apigateway.helpers import extract_query_string_params
from localstack.services.apigateway.legacy.helpers import extract_query_string_params
from localstack.utils import collections
from localstack.utils.aws.arns import (
extract_account_id_from_arn,
Expand Down
6 changes: 4 additions & 2 deletions tests/aws/services/apigateway/test_apigateway_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
from localstack.aws.handlers import cors
from localstack.constants import TAG_KEY_CUSTOM_ID
from localstack.services.apigateway.helpers import (
get_resource_for_path,
get_rest_api_paths,
host_based_url,
localstack_path_based_url,
path_based_url,
)
from localstack.services.apigateway.legacy.helpers import (
get_resource_for_path,
get_rest_api_paths,
)
from localstack.testing.aws.util import in_default_partition
from localstack.testing.config import (
TEST_AWS_ACCESS_KEY_ID,
Expand Down
2 changes: 1 addition & 1 deletion tests/aws/test_multiregion.py
A377
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from localstack import config
from localstack.constants import PATH_USER_REQUEST
from localstack.services.apigateway.helpers import connect_api_gateway_to_sqs
from localstack.services.apigateway.legacy.helpers import connect_api_gateway_to_sqs
from localstack.testing.pytest import markers
from localstack.utils.aws import arns, queries
from localstack.utils.common import short_uid, to_str
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/test_apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@
from localstack.services.apigateway.helpers import (
ModelResolver,
OpenAPISpecificationResolver,
RequestParametersResolver,
apply_json_patch_safe,
)
from localstack.services.apigateway.legacy.helpers import (
RequestParametersResolver,
extract_path_params,
extract_query_string_params,
get_resource_for_path,
)
from localstack.services.apigateway.integration import (
from localstack.services.apigateway.legacy.integration import (
LambdaProxyIntegration,
apply_request_parameters,
)
from localstack.services.apigateway.invocations import (
from localstack.services.apigateway.legacy.invocations import (
ApiInvocationContext,
BadRequestBody,
RequestValidator,
)
from localstack.services.apigateway.models import ApiGatewayStore, RestApiContainer
from localstack.services.apigateway.templates import (
from localstack.services.apigateway.legacy.templates import (
RequestTemplates,
ResponseTemplates,
VelocityUtilApiGateway,
)
from localstack.services.apigateway.models import ApiGatewayStore, RestApiContainer
from localstack.testing.config import TEST_AWS_REGION_NAME
from localstack.utils.aws.aws_responses import requests_response
from localstack.utils.common import clone
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_templating.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import re

from localstack.services.apigateway.templates import ApiGatewayVtlTemplate
from localstack.services.apigateway.legacy.templates import ApiGatewayVtlTemplate
from localstack.utils.aws.templating import render_velocity_template

# template used to transform incoming requests at the API Gateway (forward to Kinesis)
Expand Down
Loading
0