Skip to content
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

chore: fix the v2 gateway integration to route resources linking #5478

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
62 changes: 57 additions & 5 deletions samcli/hook_packages/terraform/hooks/prepare/resource_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@
LOG = logging.getLogger(__name__)


def _default_tf_destination_value_id_extractor(value: str) -> str:
"""
The default function to extract the Terraform destination resource id from the linking property value. The logic of
this function is to return the same input value.

Parameters
----------
value: str
linking property value

Returns
--------
str:
the extracted destination resource value.
"""
return value


@dataclass
class ReferenceType:
"""
Expand Down Expand Up @@ -110,6 +128,10 @@ class ResourceLinkingPair:
terraform_resource_type_prefix: str
cfn_resource_update_call_back_function: Callable[[Dict, List[ReferenceType]], None]
linking_exceptions: ResourcePairExceptions
# function to extract the terraform destination value from the linking field value
tf_destination_value_extractor_from_link_field_value_function: Callable[
[str], str
] = _default_tf_destination_value_id_extractor


class ResourceLinker:
Expand Down Expand Up @@ -274,6 +296,12 @@ def _link_using_linking_fields(self, cfn_resource: Dict) -> None:
if not isinstance(values, List):
values = [values]

# loop on the linking field values and call the Logical Id extractor function to extrac the destination resource
# logical id.
values = [
self._resource_pair.tf_destination_value_extractor_from_link_field_value_function(value) for value in values
]

# build map between the destination linking field property values, and resources' logical ids
child_resources_linking_attributes_logical_id_mapping = {}
for logical_id, destination_resource in self._resource_pair.destination_resource_tf.items():
Expand Down Expand Up @@ -1713,11 +1741,34 @@ def _link_gateway_v2_route_to_integration_callback(
return

logical_id = gateway_v2_integration_resources[0]
gateway_v2_route_cfn_resource["Properties"]["Target"] = (
{"Fn::Join": ["/", ["integrations", {"Ref": logical_id.value}]]}
if isinstance(logical_id, LogicalIdReference)
else logical_id.value
)
if isinstance(logical_id, LogicalIdReference):
gateway_v2_route_cfn_resource["Properties"]["Target"] = {
"Fn::Join": ["/", ["integrations", {"Ref": logical_id.value}]]
}
elif not logical_id.value.startswith("integrations/"):
gateway_v2_route_cfn_resource["Properties"]["Target"] = f"integrations/{logical_id.value}"
else:
gateway_v2_route_cfn_resource["Properties"]["Target"] = logical_id.value


def _extract_gateway_v2_integration_id_from_route_target_value(value: str) -> str:
"""
Function to extract the Gateway V2 Integration id value from the Gateway V2 Route resource target property value.
The Route Target value should be in the format `integrations/<Gateway Ve Integration resource id>`

Parameters
----------
value: str
Gateway V2 Route target property value

Returns
--------
str:
The Gateway V2 integration id extracted from the route target property
"""
if value.startswith("integrations/"):
return value[len("integrations/") :]
return value


def _link_gateway_v2_route_to_integration(
Expand Down Expand Up @@ -1753,5 +1804,6 @@ def _link_gateway_v2_route_to_integration(
terraform_resource_type_prefix=API_GATEWAY_V2_INTEGRATION_RESOURCE_ADDRESS_PREFIX,
cfn_resource_update_call_back_function=_link_gateway_v2_route_to_integration_callback,
linking_exceptions=exceptions,
tf_destination_value_extractor_from_link_field_value_function=_extract_gateway_v2_integration_id_from_route_target_value,
)
ResourceLinker(resource_linking_pair).link_resources()
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
_link_gateway_v2_route_to_integration,
API_GATEWAY_V2_INTEGRATION_RESOURCE_ADDRESS_PREFIX,
_link_gateway_v2_route_to_integration_callback,
_extract_gateway_v2_integration_id_from_route_target_value,
)
from samcli.hook_packages.terraform.hooks.prepare.utilities import get_configuration_address
from samcli.hook_packages.terraform.hooks.prepare.types import (
Expand Down Expand Up @@ -2386,6 +2387,9 @@ def test_link_gateway_method_to_gateway_authorizer(

mock_resource_linker.assert_called_once_with(mock_resource_linking_pair())

@patch(
"samcli.hook_packages.terraform.hooks.prepare.resource_linking._extract_gateway_v2_integration_id_from_route_target_value"
)
@patch(
"samcli.hook_packages.terraform.hooks.prepare.resource_linking._link_gateway_v2_route_to_integration_callback"
)
Expand All @@ -2398,6 +2402,7 @@ def test_link_gateway_v2_route_to_gateway_v2_integration(
mock_resource_linking_pair,
mock_resource_linker,
mock_link_gateway_v2_route_to_integration_callback,
mock_integration_id_extractor_function,
):
routes_v2_cfn_resources = Mock()
routes_v2_config_resources = Mock()
Expand All @@ -2422,6 +2427,7 @@ def test_link_gateway_v2_route_to_gateway_v2_integration(
terraform_resource_type_prefix=API_GATEWAY_V2_INTEGRATION_RESOURCE_ADDRESS_PREFIX,
cfn_resource_update_call_back_function=mock_link_gateway_v2_route_to_integration_callback,
linking_exceptions=mock_resource_linking_exceptions(),
tf_destination_value_extractor_from_link_field_value_function=mock_integration_id_extractor_function,
)

mock_resource_linker.assert_called_once_with(mock_resource_linking_pair())
Expand All @@ -2442,7 +2448,15 @@ def test_link_gateway_v2_route_to_gateway_v2_integration(
"Properties": {"Target": "invoke_arn"},
},
[ExistingResourceReference("invoke_arn")],
"invoke_arn",
"integrations/invoke_arn",
),
(
{
"Type": "AWS::ApiGatewayV2::Route",
"Properties": {"Target": "invoke_arn"},
},
[ExistingResourceReference("integrations/invoke_arn")],
"integrations/invoke_arn",
),
(
{
Expand All @@ -2459,3 +2473,13 @@ def test__link_gateway_v2_route_to_integration_callback(self, input_gateway_v2_r
_link_gateway_v2_route_to_integration_callback(gateway_resource, logical_ids)
input_gateway_v2_route["Properties"]["Target"] = expected_route
self.assertEqual(gateway_resource, input_gateway_v2_route)

@parameterized.expand(
[
("integrations/invokeArn", "invokeArn"),
("invokeArn", "invokeArn"),
]
)
def test_extract_gateway_v2_integration_id_from_route_target_value(self, input_value, expected_output):
output = _extract_gateway_v2_integration_id_from_route_target_value(input_value)
self.assertEqual(output, expected_output)