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

r/aws_vpclattice_target_group: Add config.lambda_event_structure_version #33804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .changelog/33804.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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
```
58 changes: 27 additions & 31 deletions internal/service/vpclattice/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package vpclattice
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -146,35 +144,45 @@ 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,
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))
},
ValidateDiagFunc: enum.Validate[types.TargetGroupProtocolVersion](),
},
"vpc_identifier": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
},
},
},
DiffSuppressFunc: verify.SuppressMissingOptionalConfigurationBlock,
},
"name": {
Type: schema.TypeString,
Expand All @@ -196,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,
}
}

Expand Down Expand Up @@ -438,9 +429,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 {
Expand Down Expand Up @@ -530,6 +522,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))
}
Expand All @@ -538,14 +534,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
}

Expand Down
54 changes: 53 additions & 1 deletion internal/service/vpclattice/target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -171,7 +172,45 @@ 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"),
resource.TestCheckResourceAttr(resourceName, "type", "LAMBDA"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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"),
Expand Down Expand Up @@ -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" {
Expand Down
11 changes: 6 additions & 5 deletions website/docs/r/vpclattice_target_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,18 @@ 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`
* `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`
* `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` - (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:

Expand Down
Loading