diff --git a/samcli/hook_packages/terraform/hooks/prepare/property_builder.py b/samcli/hook_packages/terraform/hooks/prepare/property_builder.py index 25d1760771..e0aef64fb5 100644 --- a/samcli/hook_packages/terraform/hooks/prepare/property_builder.py +++ b/samcli/hook_packages/terraform/hooks/prepare/property_builder.py @@ -25,7 +25,9 @@ from samcli.lib.utils.resources import AWS_APIGATEWAY_RESTAPI as CFN_AWS_APIGATEWAY_RESTAPI from samcli.lib.utils.resources import AWS_APIGATEWAY_STAGE as CFN_AWS_APIGATEWAY_STAGE from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_API as CFN_AWS_APIGATEWAY_V2_API +from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_INTEGRATION as CFN_AWS_APIGATEWAY_V2_INTEGRATION from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_ROUTE as CFN_AWS_APIGATEWAY_V2_ROUTE +from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_STAGE as CFN_AWS_APIGATEWAY_V2_STAGE from samcli.lib.utils.resources import AWS_LAMBDA_FUNCTION as CFN_AWS_LAMBDA_FUNCTION from samcli.lib.utils.resources import AWS_LAMBDA_LAYERVERSION as CFN_AWS_LAMBDA_LAYER_VERSION @@ -45,6 +47,8 @@ TF_AWS_API_GATEWAY_V2_API = "aws_apigatewayv2_api" TF_AWS_API_GATEWAY_V2_ROUTE = "aws_apigatewayv2_route" +TF_AWS_API_GATEWAY_V2_STAGE = "aws_apigatewayv2_stage" +TF_AWS_API_GATEWAY_V2_INTEGRATION = "aws_apigatewayv2_integration" def _build_code_property(tf_properties: dict, resource: TFResource) -> Any: @@ -394,6 +398,20 @@ def _add_property(cfn_prop, tf_prop): "OperationName": _get_property_extractor("operation_name"), } +AWS_API_GATEWAY_V2_STAGE_PROPERTY_BUILDER_MAPPING: PropertyBuilderMapping = { + "ApiId": _get_property_extractor("api_id"), + "StageName": _get_property_extractor("name"), + "StageVariables": _get_property_extractor("stage_variables"), +} + +AWS_API_GATEWAY_V2_INTEGRATION_PROPERTY_BUILDER_MAPPING: PropertyBuilderMapping = { + "ApiId": _get_property_extractor("api_id"), + "IntegrationType": _get_property_extractor("integration_type"), + "IntegrationMethod": _get_property_extractor("integration_method"), + "IntegrationUri": _get_property_extractor("integration_uri"), + "PayloadFormatVersion": _get_property_extractor("payload_format_version"), +} + RESOURCE_TRANSLATOR_MAPPING: Dict[str, ResourceTranslator] = { TF_AWS_LAMBDA_FUNCTION: ResourceTranslator(CFN_AWS_LAMBDA_FUNCTION, AWS_LAMBDA_FUNCTION_PROPERTY_BUILDER_MAPPING), TF_AWS_LAMBDA_LAYER_VERSION: ResourceTranslator( @@ -426,4 +444,10 @@ def _add_property(cfn_prop, tf_prop): TF_AWS_API_GATEWAY_V2_ROUTE: ResourceTranslator( CFN_AWS_APIGATEWAY_V2_ROUTE, AWS_API_GATEWAY_V2_ROUTE_PROPERTY_BUILDER_MAPPING ), + TF_AWS_API_GATEWAY_V2_STAGE: ResourceTranslator( + CFN_AWS_APIGATEWAY_V2_STAGE, AWS_API_GATEWAY_V2_STAGE_PROPERTY_BUILDER_MAPPING + ), + TF_AWS_API_GATEWAY_V2_INTEGRATION: ResourceTranslator( + CFN_AWS_APIGATEWAY_V2_INTEGRATION, AWS_API_GATEWAY_V2_INTEGRATION_PROPERTY_BUILDER_MAPPING + ), } diff --git a/tests/unit/hook_packages/terraform/hooks/prepare/prepare_base.py b/tests/unit/hook_packages/terraform/hooks/prepare/prepare_base.py index 366ec081bd..b34196781d 100644 --- a/tests/unit/hook_packages/terraform/hooks/prepare/prepare_base.py +++ b/tests/unit/hook_packages/terraform/hooks/prepare/prepare_base.py @@ -14,6 +14,8 @@ AWS_APIGATEWAY_AUTHORIZER, AWS_APIGATEWAY_V2_API, AWS_APIGATEWAY_V2_ROUTE, + AWS_APIGATEWAY_V2_STAGE, + AWS_APIGATEWAY_V2_INTEGRATION, ) from samcli.hook_packages.terraform.hooks.prepare.resources.internal import ( INTERNAL_API_GATEWAY_INTEGRATION, @@ -57,6 +59,8 @@ def setUp(self) -> None: self.apigwv2_api_name = "my_apigwv2_api" self.apigwv2_api_quick_create_name = "my_apigwv2_api_quick_create" self.apigwv2_route_name = "my_apigwv2_route" + self.apigwv2_stage_name = "my_apigwv2_stage" + self.apigwv2_integration_name = "my_apigwv2_integration" self.tf_function_common_properties: dict = { "function_name": self.zip_function_name, @@ -362,6 +366,16 @@ def setUp(self) -> None: "provider_name": AWS_PROVIDER_NAME, } + self.tf_apigwv2_stage_common_attributes: dict = { + "type": "aws_apigatewayv2_stage", + "provider_name": AWS_PROVIDER_NAME, + } + + self.tf_apigwv2_integration_common_attributes: dict = { + "type": "aws_apigatewayv2_integration", + "provider_name": AWS_PROVIDER_NAME, + } + self.tf_lambda_function_resource_common_attributes: dict = { "type": "aws_lambda_function", "provider_name": AWS_PROVIDER_NAME, @@ -873,6 +887,60 @@ def setUp(self) -> None: "Metadata": {"SamResourceId": f"aws_apigatewayv2_route.{self.apigwv2_route_name}"}, } + self.tf_apigwv2_stage_properties: dict = { + "api_id": "aws_apigatewayv2_api.my_api.id", + "name": "example-stage", + "stage_variables": {"foo": "bar"}, + } + + self.expected_cfn_apigwv2_stage_properties: dict = { + "ApiId": "aws_apigatewayv2_api.my_api.id", + "StageName": "example-stage", + "StageVariables": {"foo": "bar"}, + } + + self.tf_apigwv2_stage_resource: dict = { + **self.tf_apigwv2_stage_common_attributes, + "values": self.tf_apigwv2_stage_properties, + "address": f"aws_apigatewayv2_stage.{self.apigwv2_stage_name}", + "name": self.apigwv2_stage_name, + } + + self.expected_cfn_apigwv2_stage: dict = { + "Type": AWS_APIGATEWAY_V2_STAGE, + "Properties": self.expected_cfn_apigwv2_stage_properties, + "Metadata": {"SamResourceId": f"aws_apigatewayv2_stage.{self.apigwv2_stage_name}"}, + } + + self.tf_apigwv2_integration_properties: dict = { + "api_id": "aws_apigatewayv2_api.my_api.id", + "integration_type": "AWS_PROXY", + "integration_method": "POST", + "integration_uri": "aws_lambda_function.HelloWorldFunction.invoke_arn", + "payload_format_version": "2.0", + } + + self.expected_cfn_apigwv2_integration_properties: dict = { + "ApiId": "aws_apigatewayv2_api.my_api.id", + "IntegrationType": "AWS_PROXY", + "IntegrationMethod": "POST", + "IntegrationUri": "aws_lambda_function.HelloWorldFunction.invoke_arn", + "PayloadFormatVersion": "2.0", + } + + self.tf_apigwv2_integration_resource: dict = { + **self.tf_apigwv2_integration_common_attributes, + "values": self.tf_apigwv2_integration_properties, + "address": f"aws_apigatewayv2_integration.{self.apigwv2_integration_name}", + "name": self.apigwv2_integration_name, + } + + self.expected_cfn_apigwv2_integration: dict = { + "Type": AWS_APIGATEWAY_V2_INTEGRATION, + "Properties": self.expected_cfn_apigwv2_integration_properties, + "Metadata": {"SamResourceId": f"aws_apigatewayv2_integration.{self.apigwv2_integration_name}"}, + } + self.tf_json_with_root_module_only: dict = { "planned_values": { "root_module": { @@ -891,6 +959,8 @@ def setUp(self) -> None: self.tf_apigwv2_api_resource, self.tf_apigwv2_api_quick_create_resource, self.tf_apigwv2_route_resource, + self.tf_apigwv2_stage_resource, + self.tf_apigwv2_integration_resource, ] } } @@ -910,6 +980,8 @@ def setUp(self) -> None: f"AwsApigatewayv2ApiMyApigwv2Api{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_api, f"AwsApigatewayv2ApiMyApigwv2ApiQuickCreate{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_api_quick_create, f"AwsApigatewayv2RouteMyApigwv2Route{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_route, + f"AwsApigatewayv2StageMyApigwv2Stage{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_stage, + f"AwsApigatewayv2IntegrationMyApigwv2Integration{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_integration, }, } diff --git a/tests/unit/hook_packages/terraform/hooks/prepare/test_translate.py b/tests/unit/hook_packages/terraform/hooks/prepare/test_translate.py index 19c8c7d487..41c3f36b7f 100644 --- a/tests/unit/hook_packages/terraform/hooks/prepare/test_translate.py +++ b/tests/unit/hook_packages/terraform/hooks/prepare/test_translate.py @@ -26,6 +26,8 @@ AWS_API_GATEWAY_INTEGRATION_RESPONSE_PROPERTY_BUILDER_MAPPING, AWS_API_GATEWAY_V2_API_PROPERTY_BUILDER_MAPPING, AWS_API_GATEWAY_V2_ROUTE_PROPERTY_BUILDER_MAPPING, + AWS_API_GATEWAY_V2_STAGE_PROPERTY_BUILDER_MAPPING, + AWS_API_GATEWAY_V2_INTEGRATION_PROPERTY_BUILDER_MAPPING, ) from samcli.hook_packages.terraform.hooks.prepare.types import ( SamMetadataResource, @@ -1126,12 +1128,24 @@ def test_translating_apigwv2_api_quick_create(self): ) self.assertEqual(translated_cfn_properties, self.expected_cfn_apigwv2_api_quick_create_properties) - def test_translating_apigwv2_route_quick_create(self): + def test_translating_apigwv2_route(self): translated_cfn_properties = _translate_properties( self.tf_apigwv2_route_properties, AWS_API_GATEWAY_V2_ROUTE_PROPERTY_BUILDER_MAPPING, Mock() ) self.assertEqual(translated_cfn_properties, self.expected_cfn_apigwv2_route_properties) + def test_translating_apigwv2_stage(self): + translated_cfn_properties = _translate_properties( + self.tf_apigwv2_stage_properties, AWS_API_GATEWAY_V2_STAGE_PROPERTY_BUILDER_MAPPING, Mock() + ) + self.assertEqual(translated_cfn_properties, self.expected_cfn_apigwv2_stage_properties) + + def test_translating_apigwv2_integration(self): + translated_cfn_properties = _translate_properties( + self.tf_apigwv2_integration_properties, AWS_API_GATEWAY_V2_INTEGRATION_PROPERTY_BUILDER_MAPPING, Mock() + ) + self.assertEqual(translated_cfn_properties, self.expected_cfn_apigwv2_integration_properties) + class TestUnresolvableAttributeCheck(TestCase): @patch("samcli.hook_packages.terraform.hooks.prepare.translate.RESOURCE_TRANSLATOR_MAPPING")