Skip to content

Commit

Permalink
r/aws_lb_target_group - when creating a new target group check that
Browse files Browse the repository at this point in the history
there are no target groups with the same name

Fixes an issue where target groups with an identical configuration
could both point to the same ARN
  • Loading branch information
mtt88 committed Sep 26, 2022
1 parent af99b4f commit 45baa6e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/26976.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lb_target_group: when creating a new target group check for an existing target group with the same name
```
16 changes: 16 additions & 0 deletions internal/service/elbv2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,19 @@ func FindTargetGroupByARN(conn *elbv2.ELBV2, arn string) (*elbv2.TargetGroup, er

return nil, nil
}

func FindTargetGroupByName(conn *elbv2.ELBV2, name string) (*elbv2.TargetGroup, error) {
output, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{
Names: aws.StringSlice([]string{name}),
})

if err != nil {
return nil, err
}

if len(output.TargetGroups) == 0 {
return nil, nil
}

return output.TargetGroups[0], nil
}
8 changes: 8 additions & 0 deletions internal/service/elbv2/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ func resourceTargetGroupCreate(d *schema.ResourceData, meta interface{}) error {
groupName = resource.PrefixedUniqueId("tf-")
}

existingTg, err := FindTargetGroupByName(conn, groupName)
if err != nil && !tfawserr.ErrCodeEquals(err, elbv2.ErrCodeTargetGroupNotFoundException) {
return fmt.Errorf("finding target group by name (%s): %w", groupName, err)
}
if existingTg != nil {
return fmt.Errorf("target group with name (%s) already exists", groupName)
}

params := &elbv2.CreateTargetGroupInput{
Name: aws.String(groupName),
TargetType: aws.String(d.Get("target_type").(string)),
Expand Down
83 changes: 83 additions & 0 deletions internal/service/elbv2/target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,32 @@ func TestAccELBV2TargetGroup_ALBAlias_updateStickinessEnabled(t *testing.T) {
})
}

func TestAccELBV2TargetGroup_Name_noDuplicates(t *testing.T) {
var targetGroup elbv2.TargetGroup
tgName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lb_target_group.first"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, elbv2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccTargetGroupConfig_duplicateNameFirst(tgName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &targetGroup),
resource.TestCheckResourceAttr(resourceName, "name", tgName),
),
},
{
Config: testAccTargetGroupConfig_duplicateNameFirstAndSecond(tgName),
ExpectError: regexp.MustCompile("target group with name (.*?) already exist"),
},
},
})
}

func testAccTargetGroupConfig_albDefaults(rName string) string {
return fmt.Sprintf(`
resource "aws_lb_target_group" "test" {
Expand Down Expand Up @@ -3723,6 +3749,63 @@ resource "aws_vpc" "test" {
}`, rName)
}

func testAccTargetGroupConfig_duplicateNameFirst(rName string) string {
return fmt.Sprintf(`
resource "aws_lb_target_group" "first" {
name = %[1]q
port = 443
protocol = "HTTPS"
vpc_id = aws_vpc.test.id
tags = {
Name = %[1]q
}
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = %[1]q
}
}
`, rName)
}

func testAccTargetGroupConfig_duplicateNameFirstAndSecond(rName string) string {
return fmt.Sprintf(`
resource "aws_lb_target_group" "first" {
name = %[1]q
port = 443
protocol = "HTTPS"
vpc_id = aws_vpc.test.id
tags = {
Name = %[1]q
}
}
resource "aws_lb_target_group" "second" {
name = %[1]q
port = 443
protocol = "HTTPS"
vpc_id = aws_vpc.test.id
tags = {
Name = %[1]q
}
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = %[1]q
}
}
`, rName)
}

func testAccCheckATargetGroupDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).ELBV2Conn

Expand Down

0 comments on commit 45baa6e

Please sign in to comment.