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

tests: Added limitation test cases for V2 resources #5708

Merged
merged 11 commits into from
Aug 5, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def _add_property(cfn_prop, tf_prop):

AWS_API_GATEWAY_V2_API_PROPERTY_BUILDER_MAPPING: PropertyBuilderMapping = {
"Name": _get_property_extractor("name"),
"Body": _get_property_extractor("body"),
"Body": _get_json_body,
"Target": _get_property_extractor("target"),
"ProtocolType": _get_property_extractor("protocol_type"),
"RouteKey": _get_property_extractor("route_key"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
API_GATEWAY_REST_API_RESOURCE_ADDRESS_PREFIX = "aws_api_gateway_rest_api."
API_GATEWAY_RESOURCE_RESOURCE_ADDRESS_PREFIX = "aws_api_gateway_resource."
API_GATEWAY_AUTHORIZER_RESOURCE_ADDRESS_PREFIX = "aws_api_gateway_authorizer."
API_GATEWAY_V2_INTEGRATION_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_integration"
API_GATEWAY_V2_API_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_api"
API_GATEWAY_V2_INTEGRATION_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_integration."
API_GATEWAY_V2_API_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_api."
TERRAFORM_LOCAL_VARIABLES_ADDRESS_PREFIX = "local."
DATA_RESOURCE_ADDRESS_PREFIX = "data."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,3 @@
linking_func=_link_gateway_resources_to_parents,
),
]

MULTIPLE_DESTINATIONS_RESOURCE_LINKS: List[LinkingMultipleDestinationsOptionsCaller] = [
LinkingMultipleDestinationsOptionsCaller(
source=TF_AWS_API_GATEWAY_RESOURCE,
destinations=[TF_AWS_API_GATEWAY_REST_API, TF_AWS_API_GATEWAY_RESOURCE],
linking_func=_link_gateway_resources_to_parents,
),
]
2 changes: 2 additions & 0 deletions samcli/hook_packages/terraform/hooks/prepare/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TF_AWS_API_GATEWAY_INTEGRATION_RESPONSE,
TF_AWS_API_GATEWAY_METHOD,
TF_AWS_API_GATEWAY_REST_API,
TF_AWS_API_GATEWAY_V2_API,
)
from samcli.hook_packages.terraform.hooks.prepare.enrich import enrich_resources_and_generate_makefile
from samcli.hook_packages.terraform.hooks.prepare.property_builder import (
Expand Down Expand Up @@ -68,6 +69,7 @@

TRANSLATION_VALIDATORS: Dict[str, Type[ResourceTranslationValidator]] = {
TF_AWS_API_GATEWAY_REST_API: RESTAPITranslationValidator,
TF_AWS_API_GATEWAY_V2_API: RESTAPITranslationValidator,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@ def tearDownClass(cls) -> None:
not CI_OVERRIDE,
"Skip Terraform test cases unless running in CI",
)
@parameterized_class(
[
{
"terraform_application": "terraform-v1-nested-apis",
"testing_urls": ["parent/hello", "parent"],
},
{
"terraform_application": "terraform-v1-api-simple",
"testing_urls": ["hello"],
},
]
)
@pytest.mark.flaky(reruns=3)
@parameterized_class(
[
Expand Down Expand Up @@ -161,6 +149,21 @@ def test_successful_request(self):
"expected_error_message": "Error: AWS SAM CLI could not process a Terraform project that uses local "
"variables to define linked resources.",
},
{
"terraform_application": "terraform-v2-api-simple-multi-resource-link",
"expected_error_message": "Error: AWS SAM CLI could not process a Terraform project that contains a source "
"resource that is linked to more than one destination resource.",
},
{
"terraform_application": "terraform-v2-api-simple-local-resource-link",
"expected_error_message": "Error: AWS SAM CLI could not process a Terraform project that uses local "
"variables to define linked resources.",
},
{
"terraform_application": "terraform-v2-openapi",
"expected_error_message": "Error: AWS SAM CLI is unable to process a Terraform project that uses an OpenAPI"
" specification to define the API Gateway resource.",
},
]
)
class TestStartApiTerraformApplicationLimitations(TerraformStartApiIntegrationBase):
Expand Down Expand Up @@ -219,6 +222,26 @@ def test_unsupported_limitations(self):
"terraform_application": "terraform-api-simple-local-variables-limitation",
"testing_urls": ["hello"],
},
{
"terraform_application": "terraform-v2-api-simple-multi-resource-link",
"testing_urls": ["hello"],
},
{
"terraform_application": "terraform-v2-api-simple-local-resource-link",
"testing_urls": ["hello"],
},
{
"terraform_application": "terraform-v2-openapi",
"testing_urls": ["hello"],
},
{
"terraform_application": "terraform-v2-api-simple",
"testing_urls": ["hello"],
},
{
"terraform_application": "terraform-v2-api-quick-create",
"testing_urls": ["hello"],
},
]
)
class TestStartApiTerraformApplicationLimitationsAfterApply(TerraformStartApiIntegrationApplyBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ provider "aws" {}

data "aws_region" "current" {}

resource "random_uuid" "unique_id" {
keepers = {
my_key = "my_key"
}
}

resource "aws_api_gateway_authorizer" "header_authorizer" {
name = "header-authorizer-open-api"
name = "header-authorizer-open-api_${random_uuid.unique_id.result}"
rest_api_id = aws_api_gateway_rest_api.api.id
authorizer_uri = aws_lambda_function.authorizer.invoke_arn
authorizer_credentials = aws_iam_role.invocation_role.arn
Expand All @@ -13,7 +19,7 @@ resource "aws_api_gateway_authorizer" "header_authorizer" {

resource "aws_lambda_function" "authorizer" {
filename = "lambda-functions.zip"
function_name = "authorizer-open-api"
function_name = "authorizer-open-api_${random_uuid.unique_id.result}"
role = aws_iam_role.invocation_role.arn
handler = "handlers.auth_handler"
runtime = "python3.8"
Expand All @@ -22,15 +28,15 @@ resource "aws_lambda_function" "authorizer" {

resource "aws_lambda_function" "hello_endpoint" {
filename = "lambda-functions.zip"
function_name = "hello-lambda-open-api"
function_name = "hello-lambda-open-api_${random_uuid.unique_id.result}"
role = aws_iam_role.invocation_role.arn
handler = "handlers.hello_handler"
runtime = "python3.8"
source_code_hash = filebase64sha256("lambda-functions.zip")
}

resource "aws_api_gateway_rest_api" "api" {
name = "api-open-api"
name = "api-open-api_${random_uuid.unique_id.result}"
body = jsonencode({
swagger = "2.0"
info = {
Expand Down Expand Up @@ -92,7 +98,7 @@ resource "aws_api_gateway_rest_api" "api" {
}

resource "aws_iam_role" "invocation_role" {
name = "iam-lambda-open-api"
name = "iam-lambda-open-api_${random_uuid.unique_id.result}"
path = "/"
assume_role_policy = <<EOF
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ resource "aws_s3_object" "s3_lambda_code" {

resource "aws_lambda_layer_version" "MyAwesomeLayer" {
filename = "HelloWorldFunction.zip"
layer_name = "MyAwesomeLayer"
layer_name = "MyAwesomeLayer_${random_uuid.unique_id.result}"
compatible_runtimes = ["python3.8"]
}

Expand All @@ -49,26 +49,27 @@ resource "aws_lambda_function" "HelloWorldFunction" {
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction-${random_uuid.unique_id.result}"
function_name = "HelloWorldFunction_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
layers = [aws_lambda_layer_version.MyAwesomeLayer.arn]
depends_on = [aws_s3_object.s3_lambda_code]
depends_on = [aws_s3_bucket.lambda_code_bucket, aws_s3_object.s3_lambda_code]
}

resource "aws_lambda_function" "HelloWorldFunction2" {
s3_bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction2-${random_uuid.unique_id.result}"
function_name = "HelloWorldFunction2_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
depends_on = [aws_s3_object.s3_lambda_code]
layers = [aws_lambda_layer_version.MyAwesomeLayer.arn]
depends_on = [aws_s3_bucket.lambda_code_bucket, aws_s3_object.s3_lambda_code]
}

resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
name = "MyDemoAPI-${random_uuid.unique_id.result}"
binary_media_types = [ "utf-8" ]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
provider "aws" {
}

resource "random_uuid" "unique_id" {
keepers = {
my_key = "my_key"
}
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
name = "iam_for_lambda_${random_uuid.unique_id.result}"

assume_role_policy = <<EOF
{
Expand All @@ -22,27 +28,29 @@ EOF
}

resource "aws_s3_bucket" "lambda_code_bucket" {
bucket = "lambda_code_bucket"
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
}

resource "aws_s3_object" "s3_lambda_code" {
bucket = "lambda_code_bucket"
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
key = "s3_lambda_code_key"
source = "HelloWorldFunction.zip"
depends_on = [aws_s3_bucket.lambda_code_bucket]
}

resource "aws_lambda_function" "HelloWorldFunction" {
s3_bucket = "lambda_code_bucket"
s3_bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction"
function_name = "HelloWorldFunction_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
depends_on = [aws_s3_bucket.lambda_code_bucket]
}

resource "aws_apigatewayv2_api" "quick_create_api" {
name = "quick_create_api"
name = "quick_create_api_${random_uuid.unique_id.result}"
protocol_type = "HTTP"
target = aws_lambda_function.HelloWorldFunction.invoke_arn
route_key = "GET /hello"
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
provider "aws" {
}

locals {
api_function = aws_lambda_function.HelloWorldFunction.invoke_arn
}

resource "random_uuid" "unique_id" {
keepers = {
my_key = "my_key"
}
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda_${random_uuid.unique_id.result}"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_s3_bucket" "lambda_code_bucket" {
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
}

resource "aws_s3_object" "s3_lambda_code" {
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
key = "s3_lambda_code_key"
source = "HelloWorldFunction.zip"
depends_on = [aws_s3_bucket.lambda_code_bucket]
}

resource "aws_lambda_function" "HelloWorldFunction" {
s3_bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
depends_on = [aws_s3_bucket.lambda_code_bucket]
}

resource "aws_apigatewayv2_api" "my_api" {
name = "my_api_${random_uuid.unique_id.result}"
protocol_type = "HTTP"
}

resource "aws_apigatewayv2_route" "example" {
api_id = aws_apigatewayv2_api.my_api.id
target = "integrations/${aws_apigatewayv2_integration.example.id}"
route_key = "GET /hello"
operation_name = "my_operation"
depends_on = [aws_apigatewayv2_integration.example]
}

resource "aws_apigatewayv2_deployment" "example" {
api_id = aws_apigatewayv2_api.my_api.id
depends_on = [aws_apigatewayv2_integration.example, aws_apigatewayv2_route.example]
}

resource "aws_apigatewayv2_stage" "example" {
api_id = aws_apigatewayv2_api.my_api.id
deployment_id = aws_apigatewayv2_deployment.example.id
name = "example-stage-${random_uuid.unique_id.result}"
}

resource "aws_apigatewayv2_integration" "example" {
api_id = aws_apigatewayv2_api.my_api.id
integration_type = "AWS_PROXY"
integration_method = "POST"
integration_uri = local.api_function
payload_format_version = "2.0"
}
Binary file not shown.
Loading
Loading