Skip to content

Commit

Permalink
Merge pull request #26977 from mtt88/b-aws_lb_target_group-duplicate-…
Browse files Browse the repository at this point in the history
…names

r/aws_lb_target_group - check no target group with the same name exists
  • Loading branch information
ewbankkit committed Jan 12, 2023
2 parents bdb25a5 + 9a0d0f9 commit 165566e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/26977.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, return an error if there is an existing target group with the same name. Use [`terraform import`](https://developer.hashicorp.com/terraform/cli/commands/import) for existing target groups
```
16 changes: 16 additions & 0 deletions internal/service/elbv2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,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 @@ -334,6 +334,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 @@ -2154,6 +2154,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 @@ -3779,6 +3805,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 165566e

Please sign in to comment.