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: Read APIGW V2 stage and integration resources #5443

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
18ba6ed
chore(deps): bump actions/setup-go from 3 to 4 (#5418)
dependabot[bot] Jun 27, 2023
00b262d
chore(deps-dev): bump filelock from 3.12.0 to 3.12.2 in /requirements…
dependabot[bot] Jun 27, 2023
663c88d
feat: updating app templates repo hash with (bb905c379830c3d8edbc196b…
github-actions[bot] Jun 27, 2023
9a71591
fix: add a table for package help text. (#5298)
sriram-mv Jun 28, 2023
743d389
fix: Handle BROKEN_PIPE_ERROR (#5386)
lucashuy Jun 28, 2023
3952ff6
fix: remove circular dependency by moving parse_s3 method to its own …
mndeveci Jun 29, 2023
46f7e1f
chore(deps): bump sympy from 1.10.1 to 1.12 in /requirements (#5338)
dependabot[bot] Jun 29, 2023
30336bc
chore(deps): bump websocket-client from 1.5.1 to 1.6.1 in /requiremen…
dependabot[bot] Jun 29, 2023
4336c77
chore(deps): bump ruamel-yaml from 0.17.21 to 0.17.32 in /requirement…
dependabot[bot] Jun 29, 2023
359e43b
Updated package formatter to import package options instead of deploy…
hnnasit Jun 30, 2023
5e8df69
chore(deps): bump importlib-metadata in /requirements (#5437)
dependabot[bot] Jul 3, 2023
9877db2
feat: `sam logs` help text (#5397)
sriram-mv Jul 3, 2023
c53db02
feat: enable terraform support for local start-api command (#5389)
moelasmar Jul 3, 2023
6c9939e
Updated warning message about missing function in template (#5347)
lucashuy Jul 3, 2023
ed93c2a
chore(deps-dev): bump types-pywin32 in /requirements (#5436)
dependabot[bot] Jul 3, 2023
a105438
Merge changes from upstream
mildaniel Jul 4, 2023
028abe9
feat: Read APIGW V2 stage and integration resources
mildaniel Jul 4, 2023
6b27f69
Format files
mildaniel Jul 4, 2023
b6fd3e2
Merge branch 'feat/terraform-v2-api' into read-apigatewayv2-stage-int…
mildaniel Jul 4, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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": {
Expand All @@ -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,
]
}
}
Expand All @@ -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,
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down