Skip to content

Commit

Permalink
Merge pull request #29920 from teru01/target-group-cross-zone-load-ba…
Browse files Browse the repository at this point in the history
…lancing

Add disable cross zone load balancing option to target group
  • Loading branch information
ewbankkit authored Mar 14, 2023
2 parents 4ffa2b0 + 9ddbebd commit 6c5e3d0
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 105 deletions.
3 changes: 3 additions & 0 deletions .changelog/29920.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_lb_target_group: Add `load_balancing_cross_zone_enabled` argument
```
67 changes: 47 additions & 20 deletions internal/service/elbv2/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ const (
// @SDKResource("aws_lb_target_group")
func ResourceTargetGroup() *schema.Resource {
return &schema.Resource{
// NLBs have restrictions on them at this time
CustomizeDiff: customdiff.Sequence(
resourceTargetGroupCustomizeDiff,
verify.SetTagsDiff,
),

CreateWithoutTimeout: resourceTargetGroupCreate,
ReadWithoutTimeout: resourceTargetGroupRead,
UpdateWithoutTimeout: resourceTargetGroupUpdate,
Expand All @@ -48,6 +42,12 @@ func ResourceTargetGroup() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

// NLBs have restrictions on them at this time
CustomizeDiff: customdiff.Sequence(
resourceTargetGroupCustomizeDiff,
verify.SetTagsDiff,
),

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Expand All @@ -57,6 +57,11 @@ func ResourceTargetGroup() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"connection_termination": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"deregistration_delay": {
Type: nullable.TypeNullableInt,
Optional: true,
Expand Down Expand Up @@ -133,6 +138,13 @@ func ResourceTargetGroup() *schema.Resource {
},
},
},
"ip_address_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(elbv2.TargetGroupIpAddressTypeEnum_Values(), false),
},
"lambda_multi_value_headers_enabled": {
Type: schema.TypeBool,
Optional: true,
Expand All @@ -147,6 +159,16 @@ func ResourceTargetGroup() *schema.Resource {
"least_outstanding_requests",
}, false),
},
"load_balancing_cross_zone_enabled": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"true",
"false",
"use_load_balancer_configuration",
}, false),
},
"name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -210,11 +232,6 @@ func ResourceTargetGroup() *schema.Resource {
Optional: true,
Default: false,
},
"connection_termination": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"slow_start": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -264,13 +281,8 @@ func ResourceTargetGroup() *schema.Resource {
},
},
},
"ip_address_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(elbv2.TargetGroupIpAddressTypeEnum_Values(), false),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"target_failover": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -303,8 +315,6 @@ func ResourceTargetGroup() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringInSlice(elbv2.TargetTypeEnum_Values(), false),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -475,6 +485,13 @@ func resourceTargetGroupCreate(ctx context.Context, d *schema.ResourceData, meta
})
}

if v, ok := d.GetOk("load_balancing_cross_zone_enabled"); ok {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("load_balancing.cross_zone.enabled"),
Value: aws.String(v.(string)),
})
}

if v, ok := d.GetOk("preserve_client_ip"); ok {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("preserve_client_ip.enabled"),
Expand Down Expand Up @@ -767,6 +784,13 @@ func resourceTargetGroupUpdate(ctx context.Context, d *schema.ResourceData, meta
})
}

if d.HasChange("load_balancing_cross_zone_enabled") {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("load_balancing.cross_zone.enabled"),
Value: aws.String(d.Get("load_balancing_cross_zone_enabled").(string)),
})
}

if d.HasChange("target_failover") {
failoverBlock := d.Get("target_failover").([]interface{})
if len(failoverBlock) == 1 {
Expand Down Expand Up @@ -1093,6 +1117,9 @@ func flattenTargetGroupResource(ctx context.Context, d *schema.ResourceData, met
case "load_balancing.algorithm.type":
loadBalancingAlgorithm := aws.StringValue(attr.Value)
d.Set("load_balancing_algorithm_type", loadBalancingAlgorithm)
case "load_balancing.cross_zone.enabled":
loadBalancingCrossZoneEnabled := aws.StringValue(attr.Value)
d.Set("load_balancing_cross_zone_enabled", loadBalancingCrossZoneEnabled)
case "preserve_client_ip.enabled":
_, err := strconv.ParseBool(aws.StringValue(attr.Value))
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion internal/service/elbv2/target_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func DataSourceTargetGroup() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"load_balancing_cross_zone_enabled": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -150,11 +154,11 @@ func DataSourceTargetGroup() *schema.Resource {
},
},
},
"tags": tftags.TagsSchemaComputed(),
"target_type": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -277,6 +281,9 @@ func dataSourceTargetGroupRead(ctx context.Context, d *schema.ResourceData, meta
case "load_balancing.algorithm.type":
loadBalancingAlgorithm := aws.StringValue(attr.Value)
d.Set("load_balancing_algorithm_type", loadBalancingAlgorithm)
case "load_balancing.cross_zone.enabled":
loadBalancingCrossZoneEnabled := aws.StringValue(attr.Value)
d.Set("load_balancing_cross_zone_enabled", loadBalancingCrossZoneEnabled)
case "preserve_client_ip.enabled":
_, err := strconv.ParseBool(aws.StringValue(attr.Value))
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions internal/service/elbv2/target_group_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestAccELBV2TargetGroupDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(datasourceNameByARN, "protocol_version", "HTTP1"),
resource.TestCheckResourceAttrSet(datasourceNameByARN, "vpc_id"),
resource.TestCheckResourceAttrSet(datasourceNameByARN, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrSet(datasourceNameByARN, "load_balancing_cross_zone_enabled"),
resource.TestCheckResourceAttr(datasourceNameByARN, "deregistration_delay", "300"),
resource.TestCheckResourceAttr(datasourceNameByARN, "slow_start", "0"),
resource.TestCheckResourceAttr(datasourceNameByARN, "tags.%", "1"),
Expand All @@ -52,6 +53,7 @@ func TestAccELBV2TargetGroupDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(datasourceNameByName, "port", "8080"),
resource.TestCheckResourceAttr(datasourceNameByName, "protocol", "HTTP"),
resource.TestCheckResourceAttrSet(datasourceNameByName, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrSet(datasourceNameByName, "load_balancing_cross_zone_enabled"),
resource.TestCheckResourceAttrSet(datasourceNameByName, "vpc_id"),
resource.TestCheckResourceAttr(datasourceNameByName, "deregistration_delay", "300"),
resource.TestCheckResourceAttr(datasourceNameByName, "slow_start", "0"),
Expand Down Expand Up @@ -93,6 +95,7 @@ func TestAccELBV2TargetGroupDataSource_appCookie(t *testing.T) {
resource.TestCheckResourceAttr(resourceNameArn, "protocol_version", "HTTP1"),
resource.TestCheckResourceAttrSet(resourceNameArn, "vpc_id"),
resource.TestCheckResourceAttrSet(resourceNameArn, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrSet(resourceNameArn, "load_balancing_cross_zone_enabled"),
resource.TestCheckResourceAttr(resourceNameArn, "deregistration_delay", "300"),
resource.TestCheckResourceAttr(resourceNameArn, "slow_start", "0"),
resource.TestCheckResourceAttr(resourceNameArn, "tags.%", "1"),
Expand Down Expand Up @@ -199,7 +202,7 @@ func TestAccELBV2TargetGroupDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "arn_suffix", resourceTg1, "arn_suffix"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "id", resourceTg1, "id"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "load_balancing_algorithm_type", resourceTg1, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "load_balancing_algorithm_type", resourceTg1, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "load_balancing_cross_zone_enabled", resourceTg1, "load_balancing_cross_zone_enabled"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "health_check.#", resourceTg1, "health_check.#"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "health_check.0.path", resourceTg1, "health_check.0.path"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "health_check.0.port", resourceTg1, "health_check.0.port"),
Expand All @@ -214,7 +217,7 @@ func TestAccELBV2TargetGroupDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "arn_suffix", resourceTg2, "arn_suffix"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "id", resourceTg2, "id"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "load_balancing_algorithm_type", resourceTg2, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "load_balancing_algorithm_type", resourceTg2, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "load_balancing_cross_zone_enabled", resourceTg2, "load_balancing_cross_zone_enabled"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "health_check.#", resourceTg2, "health_check.#"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "health_check.0.path", resourceTg2, "health_check.0.path"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "health_check.0.port", resourceTg2, "health_check.0.port"),
Expand All @@ -229,7 +232,7 @@ func TestAccELBV2TargetGroupDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "arn_suffix", resourceTg1, "arn_suffix"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "id", resourceTg1, "id"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "load_balancing_algorithm_type", resourceTg1, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "load_balancing_algorithm_type", resourceTg1, "load_balancing_algorithm_type"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "load_balancing_cross_zone_enabled", resourceTg1, "load_balancing_cross_zone_enabled"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "health_check.#", resourceTg1, "health_check.#"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "health_check.0.path", resourceTg1, "health_check.0.path"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "health_check.0.port", resourceTg1, "health_check.0.port"),
Expand Down
Loading

0 comments on commit 6c5e3d0

Please sign in to comment.