Skip to content

Commit 1216a74

Browse files
committed
populate contexts
1 parent 0fb41de commit 1216a74

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

localstack-core/localstack/services/apigateway/next_gen/execute_api/context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class RestApiInvocationContext(RequestContext):
6565
"""The REST API identifier of the invoked API"""
6666
stage: Optional[str]
6767
"""The REST API stage linked to this invocation"""
68+
deployment_id: Optional[str]
69+
"""The REST API deployment linked to this invocation"""
6870
region: Optional[str]
6971
"""The region the REST API is living in."""
7072
account_id: Optional[str]
@@ -87,6 +89,7 @@ def __init__(self, request: Request):
8789
self.deployment = None
8890
self.api_id = None
8991
self.stage = None
92+
self.deployment_id = None
9093
self.account_id = None
9194
self.region = None
9295
self.invocation_request = None

localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/parse.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import logging
23
from collections import defaultdict
34
from typing import Optional
@@ -7,6 +8,9 @@
78
from werkzeug.datastructures import Headers, MultiDict
89

910
from localstack.http import Response
11+
from localstack.services.apigateway.helpers import REQUEST_TIME_DATE_FORMAT
12+
from localstack.utils.strings import short_uid
13+
from localstack.utils.time import timestamp
1014

1115
from ..api import RestApiGatewayHandler, RestApiGatewayHandlerChain
1216
from ..context import InvocationRequest, RestApiInvocationContext
@@ -32,7 +36,7 @@ def parse_and_enrich(self, context: RestApiInvocationContext):
3236
context.invocation_request = self.create_invocation_request(context.request)
3337
# then we can create the ContextVariables, used throughout the invocation as payload and to render authorizer
3438
# payload, mapping templates and such.
35-
context.context_variables = self.create_context_variables(context.invocation_request)
39+
context.context_variables = self.create_context_variables(context)
3640
# then populate the stage variables
3741
context.stage_variables = self.fetch_stage_variables(context)
3842

@@ -108,8 +112,28 @@ def _get_single_and_multi_values_from_headers(
108112

109113
return single_values, multi_values
110114

111-
def create_context_variables(self, invocation_request: InvocationRequest) -> ContextVariables:
112-
context_variables = ContextVariables()
115+
@staticmethod
116+
def create_context_variables(context: RestApiInvocationContext) -> ContextVariables:
117+
invocation_request: InvocationRequest = context.invocation_request
118+
domain_name = invocation_request["headers"].get("Host", "")
119+
domain_prefix = domain_name.split(".")[0]
120+
now = datetime.datetime.now()
121+
122+
context_variables = ContextVariables(
123+
accountId=context.account_id,
124+
apiId=context.api_id,
125+
deploymentId=context.deployment_id,
126+
domainName=domain_name,
127+
domainPrefix=domain_prefix,
128+
extendedRequestId=short_uid(),
129+
httpMethod=invocation_request["http_method"],
130+
path=invocation_request["path"],
131+
protocol="HTTP/1.1",
132+
requestId=short_uid(),
133+
requestTime=timestamp(time=now, format=REQUEST_TIME_DATE_FORMAT),
134+
requestTimeEpoch=int(now.timestamp() * 1000),
135+
stage=context.stage,
136+
)
113137
return context_variables
114138

115139
@staticmethod

localstack-core/localstack/services/apigateway/next_gen/execute_api/handlers/resource_router.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from ..api import RestApiGatewayHandler, RestApiGatewayHandlerChain
2020
from ..context import RestApiInvocationContext
21+
from ..variables import ContextVariables
2122

2223
LOG = logging.getLogger(__name__)
2324

@@ -84,6 +85,7 @@ def route_and_enrich(self, context: RestApiInvocationContext):
8485
router = self.get_router_for_deployment(context.deployment)
8586

8687
resource, path_parameters = router.match(context)
88+
resource: Resource
8789

8890
context.invocation_request["path_parameters"] = path_parameters
8991
context.resource = resource
@@ -94,6 +96,16 @@ def route_and_enrich(self, context: RestApiInvocationContext):
9496
)
9597
context.resource_method = method
9698

99+
self.update_context_variables_with_resource(context.context_variables, resource)
100+
101+
@staticmethod
102+
def update_context_variables_with_resource(
103+
context_variables: ContextVariables, resource: Resource
104+
):
105+
# TODO: log updating the context_variables?
106+
context_variables["resourcePath"] = resource["path"]
107+
context_variables["resourceId"] = resource["id"]
108+
97109
@staticmethod
98110
@cache
99111
def get_router_for_deployment(deployment: RestApiDeployment) -> RestAPIResourceRouter:

localstack-core/localstack/services/apigateway/next_gen/execute_api/router.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def populate_rest_api_invocation_context(
6464
context.deployment = frozen_deployment
6565
context.api_id = api_id
6666
context.stage = stage
67+
context.deployment_id = deployment_id
6768

6869

6970
class ApiGatewayRouter:

tests/unit/services/apigateway/test_handler_request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from moto.apigateway.models import APIGatewayBackend, apigateway_backends
2+
from moto.apigateway.models import APIGatewayBackend, Stage, apigateway_backends
33
from moto.apigateway.models import RestAPI as MotoRestAPI
44
from werkzeug.datastructures import Headers
55

@@ -35,6 +35,7 @@ def dummy_deployment():
3535
)
3636

3737
moto_backend.apis[TEST_API_ID] = moto_rest_api
38+
moto_rest_api.stages[TEST_API_STAGE] = Stage(name=TEST_API_STAGE)
3839

3940
yield freeze_rest_api(
4041
account_id=TEST_AWS_ACCOUNT_ID,

0 commit comments

Comments
 (0)