From 6a6906c793435d12812f6fcfcd43fbb980d7c4e0 Mon Sep 17 00:00:00 2001 From: teruya <27873650+teru01@users.noreply.github.com> Date: Sat, 11 Mar 2023 11:43:38 +0900 Subject: [PATCH 1/7] r/aws_lb_target_group: add cross_zone_enabled option --- internal/service/elbv2/target_group.go | 27 ++++++++ internal/service/elbv2/target_group_test.go | 69 +++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/internal/service/elbv2/target_group.go b/internal/service/elbv2/target_group.go index bbcfc60af0f..68f20cf2388 100644 --- a/internal/service/elbv2/target_group.go +++ b/internal/service/elbv2/target_group.go @@ -147,6 +147,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, @@ -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"), @@ -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 { @@ -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 { diff --git a/internal/service/elbv2/target_group_test.go b/internal/service/elbv2/target_group_test.go index dcbbf0dec81..03bdf7889fb 100644 --- a/internal/service/elbv2/target_group_test.go +++ b/internal/service/elbv2/target_group_test.go @@ -2122,6 +2122,49 @@ func TestAccELBV2TargetGroup_ALBAlias_updateLoadBalancingAlgorithmType(t *testin }) } +func TestAccELBV2TargetGroup_ALBAlias_updateLoadBalancingCrossZoneEnabled(t *testing.T) { + ctx := acctest.Context(t) + var conf elbv2.TargetGroup + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_alb_target_group.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, elbv2.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckTargetGroupDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccTargetGroupConfig_albLoadBalancingCrossZoneEnabled(rName, false, false), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckTargetGroupExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttrSet(resourceName, "arn"), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "load_balancing_cross_zone_enabled", "use_load_balancer_configuration"), + ), + }, + { + Config: testAccTargetGroupConfig_albLoadBalancingCrossZoneEnabled(rName, true, true), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckTargetGroupExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttrSet(resourceName, "arn"), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "load_balancing_cross_zone_enabled", "true"), + ), + }, + { + Config: testAccTargetGroupConfig_albLoadBalancingCrossZoneEnabled(rName, true, false), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckTargetGroupExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttrSet(resourceName, "arn"), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "load_balancing_cross_zone_enabled", "false"), + ), + }, + }, + }) +} + func TestAccELBV2TargetGroup_ALBAlias_updateStickinessEnabled(t *testing.T) { ctx := acctest.Context(t) var conf elbv2.TargetGroup @@ -3512,6 +3555,32 @@ resource "aws_vpc" "test" { }`, rName, algoTypeParam) } +func testAccTargetGroupConfig_albLoadBalancingCrossZoneEnabled(rName string, nonDefault bool, enabled bool) string { + var crossZoneParam string + + if nonDefault { + crossZoneParam = fmt.Sprintf(`load_balancing_cross_zone_enabled = "%v"`, enabled) + } + + return fmt.Sprintf(` +resource "aws_alb_target_group" "test" { + name = %[1]q + port = 443 + protocol = "HTTPS" + vpc_id = aws_vpc.test.id + + %[2]s +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags = { + Name = %[1]q + } +}`, rName, crossZoneParam) +} + func testAccTargetGroupConfig_albMissingPort(rName string) string { return fmt.Sprintf(` resource "aws_alb_target_group" "test" { From 57a998a61a287cf036a57923beb2e71a02e8b2ac Mon Sep 17 00:00:00 2001 From: teruya <27873650+teru01@users.noreply.github.com> Date: Sat, 11 Mar 2023 12:07:57 +0900 Subject: [PATCH 2/7] d/aws_lb_target_group: add cross_zone_enabled option --- internal/service/elbv2/target_group_data_source.go | 7 +++++++ internal/service/elbv2/target_group_data_source_test.go | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/service/elbv2/target_group_data_source.go b/internal/service/elbv2/target_group_data_source.go index f724e07e561..c5c07a321d0 100644 --- a/internal/service/elbv2/target_group_data_source.go +++ b/internal/service/elbv2/target_group_data_source.go @@ -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, @@ -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 { diff --git a/internal/service/elbv2/target_group_data_source_test.go b/internal/service/elbv2/target_group_data_source_test.go index e2d6ab54aa2..28caeeb14d2 100644 --- a/internal/service/elbv2/target_group_data_source_test.go +++ b/internal/service/elbv2/target_group_data_source_test.go @@ -31,6 +31,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"), @@ -51,6 +52,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"), @@ -91,6 +93,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"), @@ -195,7 +198,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"), @@ -210,7 +213,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"), @@ -225,7 +228,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"), From 6274a0387a581b88efd72b449175f5f30419e6ab Mon Sep 17 00:00:00 2001 From: teruya <27873650+teru01@users.noreply.github.com> Date: Mon, 13 Mar 2023 22:00:37 +0900 Subject: [PATCH 3/7] add docs --- .changelog/29920.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/29920.txt diff --git a/.changelog/29920.txt b/.changelog/29920.txt new file mode 100644 index 00000000000..1b4e609b6a5 --- /dev/null +++ b/.changelog/29920.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_lb_target_group: Add `load_balancing_cross_zone_enabled` argument +``` From 22087cf838af6c175eb232807954be7ceb0c87d2 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 14 Mar 2023 09:54:06 -0400 Subject: [PATCH 4/7] r/aws_alb_target_group: Alphabetize attributes. --- internal/service/elbv2/target_group.go | 40 +++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/internal/service/elbv2/target_group.go b/internal/service/elbv2/target_group.go index 68f20cf2388..8e4983ed66c 100644 --- a/internal/service/elbv2/target_group.go +++ b/internal/service/elbv2/target_group.go @@ -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, @@ -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, @@ -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, @@ -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, @@ -220,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, @@ -274,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, @@ -313,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, From 97807539a42f005fe6253ea0bb51248f6bcd5dd5 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 14 Mar 2023 09:59:21 -0400 Subject: [PATCH 5/7] r/aws_alb_target_group: Document 'load_balancing_cross_zone_enabled'. --- website/docs/r/lb_target_group.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/lb_target_group.html.markdown b/website/docs/r/lb_target_group.html.markdown index ce2e26fae90..e94e1886f27 100644 --- a/website/docs/r/lb_target_group.html.markdown +++ b/website/docs/r/lb_target_group.html.markdown @@ -75,6 +75,7 @@ The following arguments are supported: * `health_check` - (Optional, Maximum of 1) Health Check configuration block. Detailed below. * `lambda_multi_value_headers_enabled` - (Optional) Whether the request and response headers exchanged between the load balancer and the Lambda function include arrays of values or strings. Only applies when `target_type` is `lambda`. Default is `false`. * `load_balancing_algorithm_type` - (Optional) Determines how the load balancer selects targets when routing requests. Only applicable for Application Load Balancer Target Groups. The value is `round_robin` or `least_outstanding_requests`. The default is `round_robin`. +* `load_balancing_cross_zone_enabled` - (Optional) Indicates whether cross zone load balancing is enabled. The value is `"true"`, `"false"` or `"use_load_balancer_configuration"`. The default is `"use_load_balancer_configuration"`. * `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`. Cannot be longer than 6 characters. * `name` - (Optional, Forces new resource) Name of the target group. If omitted, Terraform will assign a random, unique name. This name must be unique per region per account, can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. * `port` - (May be required, Forces new resource) Port on which targets receive traffic, unless overridden when registering a specific target. Required when `target_type` is `instance`, `ip` or `alb`. Does not apply when `target_type` is `lambda`. From d7289174bed4c231715f1892651bb13ee86ad718 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 14 Mar 2023 11:18:29 -0400 Subject: [PATCH 6/7] d/aws_alb_target_group: Alphabetize attributes. --- internal/service/elbv2/target_group_data_source.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/elbv2/target_group_data_source.go b/internal/service/elbv2/target_group_data_source.go index c5c07a321d0..a011be85096 100644 --- a/internal/service/elbv2/target_group_data_source.go +++ b/internal/service/elbv2/target_group_data_source.go @@ -154,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, From 26940ab92f574c8231b255a22aea598cd8e54d47 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 14 Mar 2023 11:25:38 -0400 Subject: [PATCH 7/7] r/aws_alb_target_group: Tidy up acceptance tests. --- internal/service/elbv2/target_group_test.go | 162 ++++++++++---------- 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/internal/service/elbv2/target_group_test.go b/internal/service/elbv2/target_group_test.go index 03bdf7889fb..16ce952fd04 100644 --- a/internal/service/elbv2/target_group_test.go +++ b/internal/service/elbv2/target_group_test.go @@ -1364,7 +1364,7 @@ func TestAccELBV2TargetGroup_tags(t *testing.T) { func TestAccELBV2TargetGroup_Stickiness_updateAppEnabled(t *testing.T) { ctx := acctest.Context(t) var conf elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", sdkacctest.RandString(10)) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_lb_target_group.test" resource.ParallelTest(t, resource.TestCase{ @@ -1374,11 +1374,11 @@ func TestAccELBV2TargetGroup_Stickiness_updateAppEnabled(t *testing.T) { CheckDestroy: testAccCheckTargetGroupDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccTargetGroupConfig_appStickiness(targetGroupName, false, false), + Config: testAccTargetGroupConfig_appStickiness(rName, false, false), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckTargetGroupExists(ctx, resourceName, &conf), resource.TestCheckResourceAttrSet(resourceName, "arn"), - resource.TestCheckResourceAttr(resourceName, "name", targetGroupName), + resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "port", "443"), resource.TestCheckResourceAttr(resourceName, "protocol", "HTTPS"), resource.TestCheckResourceAttrSet(resourceName, "vpc_id"), @@ -1395,11 +1395,11 @@ func TestAccELBV2TargetGroup_Stickiness_updateAppEnabled(t *testing.T) { ), }, { - Config: testAccTargetGroupConfig_appStickiness(targetGroupName, true, true), + Config: testAccTargetGroupConfig_appStickiness(rName, true, true), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckTargetGroupExists(ctx, resourceName, &conf), resource.TestCheckResourceAttrSet(resourceName, "arn"), - resource.TestCheckResourceAttr(resourceName, "name", targetGroupName), + resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "port", "443"), resource.TestCheckResourceAttr(resourceName, "protocol", "HTTPS"), resource.TestCheckResourceAttrSet(resourceName, "vpc_id"), @@ -1421,11 +1421,11 @@ func TestAccELBV2TargetGroup_Stickiness_updateAppEnabled(t *testing.T) { ), }, { - Config: testAccTargetGroupConfig_appStickiness(targetGroupName, true, false), + Config: testAccTargetGroupConfig_appStickiness(rName, true, false), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckTargetGroupExists(ctx, resourceName, &conf), resource.TestCheckResourceAttrSet(resourceName, "arn"), - resource.TestCheckResourceAttr(resourceName, "name", targetGroupName), + resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "port", "443"), resource.TestCheckResourceAttr(resourceName, "protocol", "HTTPS"), resource.TestCheckResourceAttrSet(resourceName, "vpc_id"), @@ -2279,6 +2279,77 @@ func TestAccELBV2TargetGroup_Name_noDuplicates(t *testing.T) { }) } +func testAccCheckTargetGroupDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).ELBV2Conn() + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_lb_target_group" && rs.Type != "aws_alb_target_group" { + continue + } + + _, err := tfelbv2.FindTargetGroupByARN(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("ELBv2 Target Group %s still exists", rs.Primary.ID) + } + + return nil + } +} + +func testAccCheckTargetGroupExists(ctx context.Context, n string, v *elbv2.TargetGroup) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return errors.New("No ELBv2 Target Group ID is set") + } + + conn := acctest.Provider.Meta().(*conns.AWSClient).ELBV2Conn() + + output, err := tfelbv2.FindTargetGroupByARN(ctx, conn, rs.Primary.ID) + + if err != nil { + return err + } + + *v = *output + + return nil + } +} + +func testAccCheckTargetGroupNotRecreated(i, j *elbv2.TargetGroup) resource.TestCheckFunc { + return func(s *terraform.State) error { + if aws.StringValue(i.TargetGroupArn) != aws.StringValue(j.TargetGroupArn) { + return errors.New("ELBv2 Target Group was recreated") + } + + return nil + } +} + +func testAccCheckTargetGroupRecreated(i, j *elbv2.TargetGroup) resource.TestCheckFunc { + return func(s *terraform.State) error { + if aws.StringValue(i.TargetGroupArn) == aws.StringValue(j.TargetGroupArn) { + return errors.New("ELBv2 Target Group was not recreated") + } + + return nil + } +} + func testAccTargetGroupConfig_albDefaults(rName string) string { return fmt.Sprintf(` resource "aws_lb_target_group" "test" { @@ -2685,7 +2756,7 @@ resource "aws_lb_target_group" "test" { `, rName) } -func testAccTargetGroupConfig_appStickiness(targetGroupName string, addAppStickinessBlock bool, enabled bool) string { +func testAccTargetGroupConfig_appStickiness(rName string, addAppStickinessBlock bool, enabled bool) string { var appSstickinessBlock string if addAppStickinessBlock { @@ -2726,10 +2797,10 @@ resource "aws_vpc" "test" { cidr_block = "10.0.0.0/16" tags = { - Name = "terraform-testacc-lb-target-group-stickiness" + Name = %[1]q } } -`, targetGroupName, appSstickinessBlock) +`, rName, appSstickinessBlock) } func testAccTargetGroupConfig_basic(rName string, deregDelay int) string { @@ -3344,77 +3415,6 @@ resource "aws_lb_target_group" "test" { `, rName) } -func testAccCheckTargetGroupDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).ELBV2Conn() - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_lb_target_group" && rs.Type != "aws_alb_target_group" { - continue - } - - _, err := tfelbv2.FindTargetGroupByARN(ctx, conn, rs.Primary.ID) - - if tfresource.NotFound(err) { - continue - } - - if err != nil { - return err - } - - return fmt.Errorf("ELBv2 Target Group %s still exists", rs.Primary.ID) - } - - return nil - } -} - -func testAccCheckTargetGroupExists(ctx context.Context, n string, v *elbv2.TargetGroup) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return errors.New("No ELBv2 Target Group ID is set") - } - - conn := acctest.Provider.Meta().(*conns.AWSClient).ELBV2Conn() - - output, err := tfelbv2.FindTargetGroupByARN(ctx, conn, rs.Primary.ID) - - if err != nil { - return err - } - - *v = *output - - return nil - } -} - -func testAccCheckTargetGroupNotRecreated(i, j *elbv2.TargetGroup) resource.TestCheckFunc { - return func(s *terraform.State) error { - if aws.StringValue(i.TargetGroupArn) != aws.StringValue(j.TargetGroupArn) { - return errors.New("ELBv2 Target Group was recreated") - } - - return nil - } -} - -func testAccCheckTargetGroupRecreated(i, j *elbv2.TargetGroup) resource.TestCheckFunc { - return func(s *terraform.State) error { - if aws.StringValue(i.TargetGroupArn) == aws.StringValue(j.TargetGroupArn) { - return errors.New("ELBv2 Target Group was not recreated") - } - - return nil - } -} - func testAccTargetGroupConfig_nlbDefaults(rName, healthCheckBlock string) string { return fmt.Sprintf(` resource "aws_lb_target_group" "test" {