Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRemis committed Nov 13, 2024
1 parent 664e37c commit 3523d09
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
6 changes: 4 additions & 2 deletions botocore/endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
logger = logging.getLogger(__name__)

TEMPLATE_STRING_RE = re.compile(r"\{[a-zA-Z#]+\}")
GET_ATTR_RE = re.compile(r"(\w+)\[(\d+)\]")
GET_ATTR_RE = re.compile(r"(\w*)\[(\d+)\]")
VALID_HOST_LABEL_RE = re.compile(
r"^(?!-)[a-zA-Z\d-]{1,63}(?<!-)$",
)
Expand Down Expand Up @@ -178,7 +178,8 @@ def get_attr(self, value, path):
if match is not None:
name, index = match.groups()
index = int(index)
value = value.get(name)
if name:
value = value.get(name)
if value is None or index >= len(value):
return None
return value[index]
Expand Down Expand Up @@ -576,6 +577,7 @@ class ParameterType(Enum):

string = str
boolean = bool
stringarray = tuple


class ParameterDefinition:
Expand Down
4 changes: 4 additions & 0 deletions botocore/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ def context_parameters(self):
and 'name' in shape.metadata['contextParam']
]

@CachedProperty
def operation_context_parameters(self):
return self._operation_model.get('operationContextParams', [])

@CachedProperty
def request_compression(self):
return self._operation_model.get('requestcompression')
Expand Down
15 changes: 15 additions & 0 deletions botocore/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import copy
import logging
import re
import jmespath

from enum import Enum

from botocore import UNSIGNED, xform_name
Expand Down Expand Up @@ -578,6 +580,11 @@ def _resolve_param_from_context(
)
if dynamic is not None:
return dynamic
operation_context_params = self._resolve_param_as_operation_context_param(
param_name, operation_model, call_args
)
if operation_context_params is not None:
return operation_context_params
return self._resolve_param_as_client_context_param(param_name)

def _resolve_param_as_static_context_param(
Expand All @@ -600,6 +607,14 @@ def _resolve_param_as_client_context_param(self, param_name):
client_ctx_varname = client_ctx_params[param_name]
return self._client_context.get(client_ctx_varname)

def _resolve_param_as_operation_context_param(self, param_name, operation_model,
call_args):
operation_ctx_params = operation_model.operation_context_parameters
if param_name in operation_ctx_params:
#TODO confirm this will always exist
path = operation_ctx_params[param_name]['path']
return tuple(jmespath.search(path, call_args))

def _resolve_param_as_builtin(self, builtin_name, builtins):
if builtin_name not in EndpointResolverBuiltins.__members__.values():
raise UnknownEndpointResolutionBuiltInName(name=builtin_name)
Expand Down
2 changes: 1 addition & 1 deletion botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ def lru_cache_weakref(*cache_args, **cache_kwargs):
functools implementation which offers ``max_size`` and ``typed`` properties.
lru_cache is a global cache even when used on a method. The cache's
reference to ``self`` will prevent garbace collection of the object. This
reference to ``self`` will prevent garbage collection of the object. This
wrapper around functools.lru_cache replaces the reference to ``self`` with
a weak reference to not interfere with garbage collection.
"""
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/data/endpoints/test-cases/array-index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "1.0",
"testCases": [
{
"documentation": "Access an array index at index 0",
"params": {
"Input": ["first index"]
},
"expect": {
"endpoint": {
"url": "https://www.example.com",
"headers": {
"x-uri": [
"https://www.example.com"
],
"x-arn-region": [
"us-east-2"
]
}
}
}
}
]
}
37 changes: 37 additions & 0 deletions tests/unit/data/endpoints/valid-rules/array-index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"version": "1.3",
"parameters": {
"ResourceList": {
"type": "stringArray"
}
},
"rules": [
{
"documentation": "Array is set, retrieve index 0",
"conditions": [
{
"fn": "isSet",
"argv": [
{
"ref": "ResourceList"
}
]
},
{
"fn": "getAttr",
"argv": [
{
"ref": "bucketArn"
},
"resourceId[2]"
],
"assign": "outpostId"
}
],
"endpoint": {
"url": "https://{firstResourceId}.example.com"
},
"type": "endpoint"
}
]
}
4 changes: 4 additions & 0 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def endpoint_rule():

def ruleset_testcases():
filenames = [
"array-index",
"aws-region",
"default-values",
"eventbridge",
Expand All @@ -161,6 +162,9 @@ def ruleset_testcases():

for test in tests["testCases"]:
input_params = test["params"]
for key, value in input_params.items():
if type(value) == list:
input_params[key] = tuple(value)
expected_object = test["expect"]
if "error" in expected_object:
error_cases.append(
Expand Down

0 comments on commit 3523d09

Please sign in to comment.