Skip to content

Commit

Permalink
Merge pull request hashicorp#34364 from dongho-jung/f-aws_lb_target_g…
Browse files Browse the repository at this point in the history
…roup-lb_arns

aws_lb_target_group: Add lb_arns attribute
  • Loading branch information
johnsonaj authored Feb 21, 2024
2 parents f197c8e + 3db1e2f commit 5af43cb
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .changelog/34364.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:enhancement
resource/aws_lb_target_group: Add `load_balancer_arns` attribute
```

```release-note:enhancement
data-source/aws_lb_target_group: Add `load_balancer_arns` attribute
```

```release-note:enhancement
resource/aws_alb_target_group: Add `load_balancer_arns` attribute
```

```release-note:enhancement
data-source/aws_alb_target_group: Add `load_balancer_arns` attribute
```
6 changes: 6 additions & 0 deletions internal/service/elbv2/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ func ResourceTargetGroup() *schema.Resource {
Optional: true,
Default: false,
},
"load_balancer_arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"load_balancing_algorithm_type": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -536,6 +541,7 @@ func resourceTargetGroupRead(ctx context.Context, d *schema.ResourceData, meta i
return sdkdiag.AppendErrorf(diags, "setting health_check: %s", err)
}
d.Set("ip_address_type", targetGroup.IpAddressType)
d.Set("load_balancer_arns", flex.FlattenStringSet(targetGroup.LoadBalancerArns))
d.Set("name", targetGroup.TargetGroupName)
d.Set("name_prefix", create.NamePrefixFromName(aws.StringValue(targetGroup.TargetGroupName)))
targetType := aws.StringValue(targetGroup.TargetType)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/elbv2/target_group_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ resource "aws_lambda_function" "test" {
function_name = %[1]q
role = aws_iam_role.test.arn
handler = "lambda_elb.lambda_handler"
runtime = "python3.7"
runtime = "python3.12"
}
resource "aws_lambda_alias" "test" {
Expand Down
10 changes: 10 additions & 0 deletions internal/service/elbv2/target_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

Expand Down Expand Up @@ -95,6 +96,11 @@ func DataSourceTargetGroup() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"load_balancer_arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"load_balancing_algorithm_type": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -227,6 +233,10 @@ func dataSourceTargetGroupRead(ctx context.Context, d *schema.ResourceData, meta
d.SetId(aws.StringValue(targetGroup.TargetGroupArn))
d.Set("arn", targetGroup.TargetGroupArn)
d.Set("arn_suffix", TargetGroupSuffixFromARN(targetGroup.TargetGroupArn))
d.Set("load_balancer_arns", flex.FlattenStringSet(targetGroup.LoadBalancerArns))
d.Set("name", targetGroup.TargetGroupName)
d.Set("target_type", targetGroup.TargetType)

if err := d.Set("health_check", flattenTargetGroupHealthCheck(targetGroup)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting health_check: %s", err)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/service/elbv2/target_group_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestAccELBV2TargetGroupDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(datasourceNameByARN, "name", rName),
resource.TestCheckResourceAttrSet(datasourceNameByARN, "arn"),
resource.TestCheckResourceAttrSet(datasourceNameByARN, "arn_suffix"),
resource.TestCheckResourceAttr(datasourceNameByARN, "load_balancer_arns.#", "0"),
resource.TestCheckResourceAttr(datasourceNameByARN, "port", "8080"),
resource.TestCheckResourceAttr(datasourceNameByARN, "protocol", "HTTP"),
resource.TestCheckResourceAttr(datasourceNameByARN, "protocol_version", "HTTP1"),
Expand All @@ -54,6 +55,7 @@ func TestAccELBV2TargetGroupDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(datasourceNameByName, "name", rName),
resource.TestCheckResourceAttrSet(datasourceNameByName, "arn"),
resource.TestCheckResourceAttrSet(datasourceNameByName, "arn_suffix"),
resource.TestCheckResourceAttr(datasourceNameByName, "load_balancer_arns.#", "0"),
resource.TestCheckResourceAttr(datasourceNameByName, "port", "8080"),
resource.TestCheckResourceAttr(datasourceNameByName, "protocol", "HTTP"),
resource.TestCheckResourceAttr(datasourceNameByName, "load_balancing_algorithm_type", "round_robin"),
Expand Down Expand Up @@ -95,6 +97,7 @@ func TestAccELBV2TargetGroupDataSource_appCookie(t *testing.T) {
resource.TestCheckResourceAttr(resourceNameArn, "name", rName),
resource.TestCheckResourceAttrSet(resourceNameArn, "arn"),
resource.TestCheckResourceAttrSet(resourceNameArn, "arn_suffix"),
resource.TestCheckResourceAttr(resourceNameArn, "load_balancer_arns.#", "0"),
resource.TestCheckResourceAttr(resourceNameArn, "port", "8080"),
resource.TestCheckResourceAttr(resourceNameArn, "protocol", "HTTP"),
resource.TestCheckResourceAttr(resourceNameArn, "protocol_version", "HTTP1"),
Expand Down Expand Up @@ -139,6 +142,7 @@ func TestAccELBV2TargetGroupDataSource_backwardsCompatibility(t *testing.T) {
resource.TestCheckResourceAttr(resourceNameArn, "name", rName),
resource.TestCheckResourceAttrSet(resourceNameArn, "arn"),
resource.TestCheckResourceAttrSet(resourceNameArn, "arn_suffix"),
resource.TestCheckResourceAttr(resourceNameArn, "load_balancer_arns.#", "0"),
resource.TestCheckResourceAttr(resourceNameArn, "port", "8080"),
resource.TestCheckResourceAttr(resourceNameArn, "protocol", "HTTP"),
resource.TestCheckResourceAttr(resourceNameArn, "protocol", "HTTP"),
Expand All @@ -160,6 +164,7 @@ func TestAccELBV2TargetGroupDataSource_backwardsCompatibility(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttrSet(resourceName, "arn_suffix"),
resource.TestCheckResourceAttr(resourceName, "load_balancer_arns.#", "0"),
resource.TestCheckResourceAttr(resourceName, "port", "8080"),
resource.TestCheckResourceAttr(resourceName, "protocol", "HTTP"),
resource.TestCheckResourceAttrSet(resourceName, "vpc_id"),
Expand Down Expand Up @@ -205,6 +210,7 @@ func TestAccELBV2TargetGroupDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "name", resourceTg1, "name"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "arn", resourceTg1, "arn"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "arn_suffix", resourceTg1, "arn_suffix"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "load_balancer_arns.#", resourceTg1, "load_balancer_arns.#"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTag, "id", resourceTg1, "id"),
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"),
Expand All @@ -220,6 +226,7 @@ func TestAccELBV2TargetGroupDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "name", resourceTg2, "name"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "arn", resourceTg2, "arn"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "arn_suffix", resourceTg2, "arn_suffix"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "load_balancer_arns.#", resourceTg2, "load_balancer_arns.#"),
resource.TestCheckResourceAttrPair(dataSourceMatchSecondTag, "id", resourceTg2, "id"),
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"),
Expand All @@ -235,6 +242,7 @@ func TestAccELBV2TargetGroupDataSource_tags(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "name", resourceTg1, "name"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "arn", resourceTg1, "arn"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "arn_suffix", resourceTg1, "arn_suffix"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "load_balancer_arns.#", resourceTg1, "load_balancer_arns.#"),
resource.TestCheckResourceAttrPair(dataSourceMatchFirstTagAndName, "id", resourceTg1, "id"),
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"),
Expand Down
35 changes: 29 additions & 6 deletions internal/service/elbv2/target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestAccELBV2TargetGroup_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "name_prefix", ""),
resource.TestCheckResourceAttr(resourceName, "load_balancer_arns.#", "0"),
resource.TestCheckResourceAttr(resourceName, "port", "443"),
resource.TestCheckResourceAttr(resourceName, "protocol", "HTTPS"),
resource.TestCheckResourceAttr(resourceName, "protocol_version", "HTTP1"),
Expand Down Expand Up @@ -328,6 +329,7 @@ func TestAccELBV2TargetGroup_backwardsCompatibility(t *testing.T) {
testAccCheckTargetGroupExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "load_balancer_arns.#", "0"),
resource.TestCheckResourceAttr(resourceName, "port", "443"),
resource.TestCheckResourceAttr(resourceName, "protocol", "HTTPS"),
resource.TestCheckResourceAttrSet(resourceName, "vpc_id"),
Expand Down Expand Up @@ -3625,6 +3627,7 @@ func TestAccELBV2TargetGroup_Instance_protocolVersion_MigrateV0(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy(ctx),
Steps: testAccMigrateTest{
PreviousVersion: "5.25.0",
NextVersion: "5.26.0",
Config: testAccTargetGroupConfig_Instance_protocolVersion(protocol, "HTTP1"),
PreCheck: preCheck,
PostCheck: postCheck,
Expand Down Expand Up @@ -3676,6 +3679,7 @@ func TestAccELBV2TargetGroup_Lambda_defaults_MigrateV0(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy(ctx),
Steps: testAccMigrateTest{
PreviousVersion: "5.25.0",
NextVersion: "5.26.0",
Config: testAccTargetGroupConfig_Lambda_basic(),
PreCheck: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup),
Expand Down Expand Up @@ -3739,6 +3743,7 @@ func TestAccELBV2TargetGroup_Lambda_vpc_MigrateV0(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy(ctx),
Steps: testAccMigrateTest{
PreviousVersion: "5.25.0",
NextVersion: "5.26.0",
Config: testAccTargetGroupConfig_Lambda_vpc(),
PreCheck: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup),
Expand Down Expand Up @@ -3804,6 +3809,7 @@ func TestAccELBV2TargetGroup_Lambda_protocol_MigrateV0(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy(ctx),
Steps: testAccMigrateTest{
PreviousVersion: "5.25.0",
NextVersion: "5.26.0",
Config: testAccTargetGroupConfig_Lambda_protocol(protocol),
PreCheck: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup),
Expand Down Expand Up @@ -3857,6 +3863,7 @@ func TestAccELBV2TargetGroup_Lambda_protocolVersion_MigrateV0(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy(ctx),
Steps: testAccMigrateTest{
PreviousVersion: "5.25.0",
NextVersion: "5.26.0",
Config: testAccTargetGroupConfig_Lambda_protocolVersion("GRPC"),
PreCheck: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup),
Expand Down Expand Up @@ -3908,6 +3915,7 @@ func TestAccELBV2TargetGroup_Lambda_port_MigrateV0(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy(ctx),
Steps: testAccMigrateTest{
PreviousVersion: "5.25.0",
NextVersion: "5.26.0",
Config: testAccTargetGroupConfig_Lambda_port("443"),
PreCheck: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup),
Expand Down Expand Up @@ -3967,6 +3975,7 @@ func TestAccELBV2TargetGroup_Lambda_HealthCheck_basic_MigrateV0(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy(ctx),
Steps: testAccMigrateTest{
PreviousVersion: "5.25.0",
NextVersion: "5.26.0",
Config: testAccTargetGroupConfig_Lambda_HealthCheck_basic(),
PreCheck: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(ctx, resourceName, &targetGroup),
Expand Down Expand Up @@ -4120,9 +4129,15 @@ func TestAccELBV2TargetGroup_Lambda_HealthCheck_protocol_MigrateV0(t *testing.T)
steps := []resource.TestStep{step}
if tc.warning {
steps = append(steps, resource.TestStep{
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: config,
PlanOnly: true,
// check that the plan is still valid with the IMMEDIATE next published version
ExternalProviders: map[string]resource.ExternalProvider{
"aws": {
Source: "hashicorp/aws",
VersionConstraint: "5.26.0",
},
},
Config: config,
PlanOnly: true,
})
}

Expand Down Expand Up @@ -4224,6 +4239,9 @@ type testAccMigrateTest struct {
// PreviousVersion is a version of the provider previous to the changes to be migrated
PreviousVersion string

// NextVersion is a version of the provider following the changes to be migrated
NextVersion string

// Config is the configuration to be deployed with the previous version and checked with the updated version
Config string

Expand All @@ -4246,9 +4264,14 @@ func (t testAccMigrateTest) Steps() []resource.TestStep {
Check: t.PreCheck,
},
{
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: t.Config,
PlanOnly: true,
ExternalProviders: map[string]resource.ExternalProvider{
"aws": {
Source: "hashicorp/aws",
VersionConstraint: t.NextVersion,
},
},
Config: t.Config,
PlanOnly: true,
},
{
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/lb_target_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ This resource exports the following attributes in addition to the arguments abov
* `arn` - ARN of the Target Group (matches `id`).
* `id` - ARN of the Target Group (matches `arn`).
* `name` - Name of the Target Group.
* `load_balancer_arns` - ARNs of the Load Balancers associated with the Target Group.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).

## Import
Expand Down

0 comments on commit 5af43cb

Please sign in to comment.