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

feat: Link V2 Stages to V2 APIs #5622

Merged
merged 7 commits into from
Jul 27, 2023
12 changes: 12 additions & 0 deletions samcli/hook_packages/terraform/hooks/prepare/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ class GatewayV2RouteToGatewayV2ApiLocalVariablesLinkingLimitationException(Local
"""


class OneGatewayV2StageToGatewayV2ApiLinkingLimitationException(OneResourceLinkingLimitationException):
"""
Exception specific for Gateway V2 Stage linking to more than one Gateway V2 API
"""


class GatewayV2StageToGatewayV2ApiLocalVariablesLinkingLimitationException(LocalVariablesLinkingLimitationException):
"""
Exception specific for Gateway V2 Stage linking to Gateway V2 API using locals.
"""


class InvalidSamMetadataPropertiesException(UserException):
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
GatewayV2IntegrationToLambdaFunctionLocalVariablesLinkingLimitationException,
GatewayV2RouteToGatewayV2ApiLocalVariablesLinkingLimitationException,
GatewayV2RouteToGatewayV2IntegrationLocalVariablesLinkingLimitationException,
GatewayV2StageToGatewayV2ApiLocalVariablesLinkingLimitationException,
InvalidResourceLinkingException,
LambdaFunctionToApiGatewayIntegrationLocalVariablesLinkingLimitationException,
LocalVariablesLinkingLimitationException,
Expand All @@ -34,6 +35,7 @@
OneGatewayV2IntegrationToLambdaFunctionLinkingLimitationException,
OneGatewayV2RouteToGatewayV2ApiLinkingLimitationException,
OneGatewayV2RouteToGatewayV2IntegrationLinkingLimitationException,
OneGatewayV2StageToGatewayV2ApiLinkingLimitationException,
OneLambdaFunctionResourceToApiGatewayIntegrationLinkingLimitationException,
OneLambdaLayerLinkingLimitationException,
OneResourceLinkingLimitationException,
Expand Down Expand Up @@ -2002,3 +2004,40 @@ def _link_gateway_v2_route_to_api(
linking_exceptions=exceptions,
)
ResourceLinker(resource_linking_pair).link_resources()


def _link_gateway_v2_stage_to_api(
gateway_stage_config_resources: Dict[str, TFResource],
gateway_stage_config_address_cfn_resources_map: Dict[str, List],
api_resources: Dict[str, Dict],
):
"""
Iterate through all the resources and link the corresponding
Gateway V2 Stage resources to each Gateway V2 Api

Parameters
----------
gateway_stage_config_resources: Dict[str, TFResource]
Dictionary of configuration Gateway Stages
gateway_stage_config_address_cfn_resources_map: Dict[str, List]
Dictionary containing resolved configuration addresses matched up to the cfn Gateway Stage
api_resources: Dict[str, Dict]
Dictionary of all Terraform Gateway V2 Api resources (not configuration resources).
The dictionary's key is the calculated logical id for each resource.
"""
exceptions = ResourcePairExceptions(
multiple_resource_linking_exception=OneGatewayV2StageToGatewayV2ApiLinkingLimitationException,
local_variable_linking_exception=GatewayV2StageToGatewayV2ApiLocalVariablesLinkingLimitationException,
)
resource_linking_pair = ResourceLinkingPair(
source_resource_cfn_resource=gateway_stage_config_address_cfn_resources_map,
source_resource_tf_config=gateway_stage_config_resources,
destination_resource_tf=api_resources,
tf_destination_attribute_name="id",
terraform_link_field_name="api_id",
cfn_link_field_name="ApiId",
terraform_resource_type_prefix=API_GATEWAY_V2_API_RESOURCE_ADDRESS_PREFIX,
cfn_resource_update_call_back_function=_link_gateway_v2_resource_to_api_callback,
linking_exceptions=exceptions,
)
ResourceLinker(resource_linking_pair).link_resources()
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ def __init__(self):
super(ApiGatewayV2IntegrationProperties, self).__init__()


class ApiGatewayV2StageProperties(ResourceProperties):
"""
Contains the collection logic of the required properties for linking the aws_api_gateway_v2_stage resources.
"""

def __init__(self):
super(ApiGatewayV2StageProperties, self).__init__()


def add_integrations_to_methods(
gateway_methods_cfn: Dict[str, List], gateway_integrations_cfn: Dict[str, List]
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TF_AWS_API_GATEWAY_V2_API,
TF_AWS_API_GATEWAY_V2_INTEGRATION,
TF_AWS_API_GATEWAY_V2_ROUTE,
TF_AWS_API_GATEWAY_V2_STAGE,
TF_AWS_LAMBDA_FUNCTION,
TF_AWS_LAMBDA_LAYER_VERSION,
)
Expand All @@ -31,6 +32,7 @@
_link_gateway_v2_integration_to_lambda_function,
_link_gateway_v2_route_to_api,
_link_gateway_v2_route_to_integration,
_link_gateway_v2_stage_to_api,
_link_lambda_functions_to_layers,
)
from samcli.hook_packages.terraform.hooks.prepare.types import LinkingPairCaller
Expand Down Expand Up @@ -117,4 +119,9 @@
dest=TF_AWS_API_GATEWAY_V2_API,
linking_func=_link_gateway_v2_route_to_api,
),
LinkingPairCaller(
source=TF_AWS_API_GATEWAY_V2_STAGE,
dest=TF_AWS_API_GATEWAY_V2_API,
linking_func=_link_gateway_v2_stage_to_api,
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TF_AWS_API_GATEWAY_V2_API,
TF_AWS_API_GATEWAY_V2_INTEGRATION,
TF_AWS_API_GATEWAY_V2_ROUTE,
TF_AWS_API_GATEWAY_V2_STAGE,
TF_AWS_LAMBDA_FUNCTION,
TF_AWS_LAMBDA_LAYER_VERSION,
)
Expand All @@ -24,6 +25,7 @@
ApiGatewayV2ApiProperties,
ApiGatewayV2IntegrationProperties,
ApiGatewayV2RouteProperties,
ApiGatewayV2StageProperties,
)
from samcli.hook_packages.terraform.hooks.prepare.resources.internal import (
InternalApiGatewayIntegrationProperties,
Expand Down Expand Up @@ -58,4 +60,5 @@ def get_resource_property_mapping() -> Dict[str, ResourceProperties]:
TF_AWS_API_GATEWAY_V2_ROUTE: ApiGatewayV2RouteProperties(),
TF_AWS_API_GATEWAY_V2_INTEGRATION: ApiGatewayV2IntegrationProperties(),
TF_AWS_API_GATEWAY_V2_API: ApiGatewayV2ApiProperties(),
TF_AWS_API_GATEWAY_V2_STAGE: ApiGatewayV2StageProperties(),
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
GatewayV2IntegrationToGatewayV2ApiLocalVariablesLinkingLimitationException,
OneGatewayV2RouteToGatewayV2ApiLinkingLimitationException,
GatewayV2RouteToGatewayV2ApiLocalVariablesLinkingLimitationException,
OneGatewayV2StageToGatewayV2ApiLinkingLimitationException,
GatewayV2StageToGatewayV2ApiLocalVariablesLinkingLimitationException,
)

from samcli.hook_packages.terraform.hooks.prepare.resource_linking import (
Expand Down Expand Up @@ -99,6 +101,7 @@
API_GATEWAY_V2_API_RESOURCE_ADDRESS_PREFIX,
_link_gateway_v2_resource_to_api_callback,
_link_gateway_v2_route_to_api,
_link_gateway_v2_stage_to_api,
)
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 @@ -2661,6 +2664,42 @@ def test_link_gateway_v2_route_to_gateway_v2_api(

mock_resource_linker.assert_called_once_with(mock_resource_linking_pair())

@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking._link_gateway_v2_resource_to_api_callback")
@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking.ResourceLinker")
@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking.ResourceLinkingPair")
@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking.ResourcePairExceptions")
def test_link_gateway_v2_stage_to_gateway_v2_api(
self,
mock_resource_linking_exceptions,
mock_resource_linking_pair,
mock_resource_linker,
mock_link_gateway_v2_stage_to_api_callback,
):
stages_v2_cfn_resources = Mock()
stages_v2_config_resources = Mock()
apis_v2_tf_resources = Mock()

_link_gateway_v2_stage_to_api(stages_v2_config_resources, stages_v2_cfn_resources, apis_v2_tf_resources)

mock_resource_linking_exceptions.assert_called_once_with(
multiple_resource_linking_exception=OneGatewayV2StageToGatewayV2ApiLinkingLimitationException,
local_variable_linking_exception=GatewayV2StageToGatewayV2ApiLocalVariablesLinkingLimitationException,
)

mock_resource_linking_pair.assert_called_once_with(
source_resource_cfn_resource=stages_v2_cfn_resources,
source_resource_tf_config=stages_v2_config_resources,
destination_resource_tf=apis_v2_tf_resources,
tf_destination_attribute_name="id",
terraform_link_field_name="api_id",
cfn_link_field_name="ApiId",
terraform_resource_type_prefix=API_GATEWAY_V2_API_RESOURCE_ADDRESS_PREFIX,
cfn_resource_update_call_back_function=mock_link_gateway_v2_stage_to_api_callback,
linking_exceptions=mock_resource_linking_exceptions(),
)

mock_resource_linker.assert_called_once_with(mock_resource_linking_pair())

@parameterized.expand(
[
(
Expand Down
Loading