From 008eaf91b84b8cc8cb591dadfa877b87d43086a5 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 6 Oct 2023 12:25:22 -0400 Subject: [PATCH 1/3] r/aws_vpclattice_target_group: Add 'config.lambda_event_structure_version'. --- .changelog/#####.txt | 3 ++ internal/service/vpclattice/target_group.go | 26 +++++++--- .../service/vpclattice/target_group_test.go | 52 +++++++++++++++++++ .../r/vpclattice_target_group.html.markdown | 7 +-- 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 .changelog/#####.txt diff --git a/.changelog/#####.txt b/.changelog/#####.txt new file mode 100644 index 00000000000..46872119ef6 --- /dev/null +++ b/.changelog/#####.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_vpclattice_target_group: Add `config.lambda_event_structure_version` argument +``` \ No newline at end of file diff --git a/internal/service/vpclattice/target_group.go b/internal/service/vpclattice/target_group.go index 9c8b5dc173a..e199d2168c7 100644 --- a/internal/service/vpclattice/target_group.go +++ b/internal/service/vpclattice/target_group.go @@ -146,6 +146,13 @@ func ResourceTargetGroup() *schema.Resource { ForceNew: true, ValidateDiagFunc: enum.Validate[types.IpAddressType](), }, + "lambda_event_structure_version": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateDiagFunc: enum.Validate[types.LambdaEventStructureVersion](), + }, "port": { Type: schema.TypeInt, Required: true, @@ -438,9 +445,10 @@ func flattenTargetGroupConfig(apiObject *types.TargetGroupConfig) map[string]int } tfMap := map[string]interface{}{ - "ip_address_type": apiObject.IpAddressType, - "protocol": apiObject.Protocol, - "protocol_version": apiObject.ProtocolVersion, + "ip_address_type": apiObject.IpAddressType, + "lambda_event_structure_version": apiObject.LambdaEventStructureVersion, + "protocol": apiObject.Protocol, + "protocol_version": apiObject.ProtocolVersion, } if v := apiObject.HealthCheck; v != nil { @@ -530,6 +538,10 @@ func expandTargetGroupConfig(tfMap map[string]interface{}) *types.TargetGroupCon apiObject.IpAddressType = types.IpAddressType(v) } + if v, ok := tfMap["lambda_event_structure_version"].(string); ok && v != "" { + apiObject.LambdaEventStructureVersion = types.LambdaEventStructureVersion(v) + } + if v, ok := tfMap["port"].(int); ok && v != 0 { apiObject.Port = aws.Int32(int32(v)) } @@ -538,14 +550,14 @@ func expandTargetGroupConfig(tfMap map[string]interface{}) *types.TargetGroupCon apiObject.Protocol = types.TargetGroupProtocol(v) } - if v, ok := tfMap["vpc_identifier"].(string); ok && v != "" { - apiObject.VpcIdentifier = aws.String(v) - } - if v, ok := tfMap["protocol_version"].(string); ok && v != "" { apiObject.ProtocolVersion = types.TargetGroupProtocolVersion(v) } + if v, ok := tfMap["vpc_identifier"].(string); ok && v != "" { + apiObject.VpcIdentifier = aws.String(v) + } + return apiObject } diff --git a/internal/service/vpclattice/target_group_test.go b/internal/service/vpclattice/target_group_test.go index 1f4b86e495c..a5e773f7d23 100644 --- a/internal/service/vpclattice/target_group_test.go +++ b/internal/service/vpclattice/target_group_test.go @@ -58,6 +58,7 @@ func TestAccVPCLatticeTargetGroup_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "config.0.health_check.0.protocol_version", "HTTP1"), resource.TestCheckResourceAttr(resourceName, "config.0.health_check.0.unhealthy_threshold_count", "2"), resource.TestCheckResourceAttr(resourceName, "config.0.ip_address_type", ""), + resource.TestCheckResourceAttr(resourceName, "config.0.lambda_event_structure_version", ""), resource.TestCheckResourceAttr(resourceName, "config.0.port", "80"), resource.TestCheckResourceAttr(resourceName, "config.0.protocol", "HTTP"), resource.TestCheckResourceAttr(resourceName, "config.0.protocol_version", "HTTP1"), @@ -187,6 +188,44 @@ func TestAccVPCLatticeTargetGroup_lambda(t *testing.T) { }) } +func TestAccVPCLatticeTargetGroup_lambdaEventStructureVersion(t *testing.T) { + ctx := acctest.Context(t) + var targetGroup vpclattice.GetTargetGroupOutput + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_vpclattice_target_group.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, names.VPCLatticeEndpointID) + testAccPreCheck(ctx, t) + }, + ErrorCheck: acctest.ErrorCheck(t, names.VPCLatticeEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckTargetGroupDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccTargetGroupConfig_lambdaEventStructureVersion(rName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup), + acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "vpc-lattice", regexache.MustCompile("targetgroup/.+$")), + resource.TestCheckResourceAttr(resourceName, "config.#", "1"), + resource.TestCheckResourceAttr(resourceName, "config.0.lambda_event_structure_version", "V2"), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "status", "ACTIVE"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccVPCLatticeTargetGroup_ip(t *testing.T) { ctx := acctest.Context(t) var targetGroup vpclattice.GetTargetGroupOutput @@ -413,6 +452,19 @@ resource "aws_vpclattice_target_group" "test" { `, rName) } +func testAccTargetGroupConfig_lambdaEventStructureVersion(rName string) string { + return fmt.Sprintf(` +resource "aws_vpclattice_target_group" "test" { + name = %[1]q + type = "LAMBDA" + + config { + lambda_event_structure_version = "V2" + } +} +`, rName) +} + func testAccTargetGroupConfig_ip(rName string) string { return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 0), fmt.Sprintf(` resource "aws_vpclattice_target_group" "test" { diff --git a/website/docs/r/vpclattice_target_group.html.markdown b/website/docs/r/vpclattice_target_group.html.markdown index 81992d775d9..3044078c881 100644 --- a/website/docs/r/vpclattice_target_group.html.markdown +++ b/website/docs/r/vpclattice_target_group.html.markdown @@ -102,15 +102,16 @@ The following arguments are required: The following arguments are optional: -* `config` - (Optional) The target group configuration. If type is set to `LAMBDA,` this parameter should not be specified. +* `config` - (Optional) The target group configuration. * `tags` - (Optional) Key-value mapping of resource tags. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. Config (`config`) supports the following: * `health_check` - (Optional) The health check configuration. -* `ip_address_type` - (Optional) The type of IP address used for the target group. Valid values: `IPV4` | `IPV6` +* `ip_address_type` - (Optional) The type of IP address used for the target group. Valid values: `IPV4` | `IPV6`. +* `lambda_event_structure_version` - (Optional) The version of the event structure that the Lambda function receives. Supported only if `type` is `LAMBDA`. Valid Values are `V1` | `V2`. * `port` - (Required) The port on which the targets are listening. -* `protocol` - (Required) The protocol to use for routing traffic to the targets. Valid Values are `HTTP` | `HTTPS` +* `protocol` - (Required) The protocol to use for routing traffic to the targets. Valid Values are `HTTP` | `HTTPS`. * `protocol_version` - (Optional) The protocol version. Valid Values are `HTTP1` | `HTTP2` | `GRPC`. Default value is `HTTP1`. * `vpc_identifier` - (Required) The ID of the VPC. From 3138b8ff4aeb3454dc82b10f6b23c3e146cba41e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 6 Oct 2023 12:27:26 -0400 Subject: [PATCH 2/3] Correct CHANGELOG entry file name. --- .changelog/{#####.txt => 33804.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/{#####.txt => 33804.txt} (100%) diff --git a/.changelog/#####.txt b/.changelog/33804.txt similarity index 100% rename from .changelog/#####.txt rename to .changelog/33804.txt From 18892837153fdc3d22acbad6e306ee55bf51221f Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 6 Oct 2023 13:25:00 -0400 Subject: [PATCH 3/3] r/aws_vpclattice_target_group: Make `config.port`, `config.protocol` and `config.vpc_identifier` optional. --- .changelog/33804.txt | 4 +++ internal/service/vpclattice/target_group.go | 32 +++++-------------- .../service/vpclattice/target_group_test.go | 2 +- .../r/vpclattice_target_group.html.markdown | 6 ++-- 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/.changelog/33804.txt b/.changelog/33804.txt index 46872119ef6..5d2cdfbce61 100644 --- a/.changelog/33804.txt +++ b/.changelog/33804.txt @@ -1,3 +1,7 @@ ```release-note:enhancement resource/aws_vpclattice_target_group: Add `config.lambda_event_structure_version` argument +``` + +```release-note:enhancement +resource/aws_vpclattice_target_group: Make `config.port`, `config.protocol` and `config.vpc_identifier` optional ``` \ No newline at end of file diff --git a/internal/service/vpclattice/target_group.go b/internal/service/vpclattice/target_group.go index e199d2168c7..adde587216e 100644 --- a/internal/service/vpclattice/target_group.go +++ b/internal/service/vpclattice/target_group.go @@ -6,7 +6,6 @@ package vpclattice import ( "context" "errors" - "fmt" "log" "strings" "time" @@ -15,7 +14,6 @@ import ( "github.com/aws/aws-sdk-go-v2/service/vpclattice" "github.com/aws/aws-sdk-go-v2/service/vpclattice/types" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -155,21 +153,23 @@ func ResourceTargetGroup() *schema.Resource { }, "port": { Type: schema.TypeInt, - Required: true, + Optional: true, + Computed: true, ForceNew: true, ValidateFunc: validation.IsPortNumber, }, "protocol": { Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ForceNew: true, ValidateDiagFunc: enum.Validate[types.TargetGroupProtocol](), }, "protocol_version": { Type: schema.TypeString, Optional: true, + Computed: true, ForceNew: true, - Default: types.TargetGroupProtocolVersionHttp1, StateFunc: func(v interface{}) string { return strings.ToUpper(v.(string)) }, @@ -177,11 +177,12 @@ func ResourceTargetGroup() *schema.Resource { }, "vpc_identifier": { Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: true, }, }, }, + DiffSuppressFunc: verify.SuppressMissingOptionalConfigurationBlock, }, "name": { Type: schema.TypeString, @@ -203,24 +204,7 @@ func ResourceTargetGroup() *schema.Resource { names.AttrTagsAll: tftags.TagsSchemaComputed(), }, - CustomizeDiff: customdiff.All( - verify.SetTagsDiff, - func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error { - targetGroupType := types.TargetGroupType(d.Get("type").(string)) - - if v, ok := d.GetOk("config"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { - if targetGroupType == types.TargetGroupTypeLambda { - return fmt.Errorf(`config not supported for type = %q`, targetGroupType) - } - } else { - if targetGroupType != types.TargetGroupTypeLambda { - return fmt.Errorf(`config required for type = %q`, targetGroupType) - } - } - - return nil - }, - ), + CustomizeDiff: verify.SetTagsDiff, } } diff --git a/internal/service/vpclattice/target_group_test.go b/internal/service/vpclattice/target_group_test.go index a5e773f7d23..4739df09574 100644 --- a/internal/service/vpclattice/target_group_test.go +++ b/internal/service/vpclattice/target_group_test.go @@ -172,7 +172,7 @@ func TestAccVPCLatticeTargetGroup_lambda(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup), acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "vpc-lattice", regexache.MustCompile("targetgroup/.+$")), - resource.TestCheckResourceAttr(resourceName, "config.#", "0"), + resource.TestCheckResourceAttr(resourceName, "config.#", "1"), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "status", "ACTIVE"), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), diff --git a/website/docs/r/vpclattice_target_group.html.markdown b/website/docs/r/vpclattice_target_group.html.markdown index 3044078c881..d08e4128ee6 100644 --- a/website/docs/r/vpclattice_target_group.html.markdown +++ b/website/docs/r/vpclattice_target_group.html.markdown @@ -110,10 +110,10 @@ Config (`config`) supports the following: * `health_check` - (Optional) The health check configuration. * `ip_address_type` - (Optional) The type of IP address used for the target group. Valid values: `IPV4` | `IPV6`. * `lambda_event_structure_version` - (Optional) The version of the event structure that the Lambda function receives. Supported only if `type` is `LAMBDA`. Valid Values are `V1` | `V2`. -* `port` - (Required) The port on which the targets are listening. -* `protocol` - (Required) The protocol to use for routing traffic to the targets. Valid Values are `HTTP` | `HTTPS`. +* `port` - (Optional) The port on which the targets are listening. +* `protocol` - (Optional) The protocol to use for routing traffic to the targets. Valid Values are `HTTP` | `HTTPS`. * `protocol_version` - (Optional) The protocol version. Valid Values are `HTTP1` | `HTTP2` | `GRPC`. Default value is `HTTP1`. -* `vpc_identifier` - (Required) The ID of the VPC. +* `vpc_identifier` - (Optional) The ID of the VPC. Health Check (`health_check`) supports the following: