Skip to content

Commit

Permalink
Merge pull request #17845 from github-vincent-miszczak/issue-17797
Browse files Browse the repository at this point in the history
Changing aws_lb_target_group protocol_version should force new resource
  • Loading branch information
ewbankkit authored Jan 12, 2023
2 parents 5fd8df3 + 21d98f9 commit bdb25a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/17845.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lb_target_group: Change `protocol_version` to [ForceNew](https://developer.hashicorp.com/terraform/plugin/sdkv2/schemas/schema-behaviors#forcenew)
```
1 change: 1 addition & 0 deletions internal/service/elbv2/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func ResourceTargetGroup() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
return strings.ToUpper(v.(string))
},
Expand Down
36 changes: 29 additions & 7 deletions internal/service/elbv2/target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestAccELBV2TargetGroup_backwardsCompatibility(t *testing.T) {
}

func TestAccELBV2TargetGroup_ProtocolVersion_basic(t *testing.T) {
var conf elbv2.TargetGroup
var before, after elbv2.TargetGroup
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lb_target_group.test"

Expand All @@ -138,9 +138,9 @@ func TestAccELBV2TargetGroup_ProtocolVersion_basic(t *testing.T) {
CheckDestroy: testAccCheckTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccTargetGroupConfig_protocolVersion(rName),
Config: testAccTargetGroupConfig_protocolVersion(rName, "HTTP2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &conf),
testAccCheckTargetGroupExists(resourceName, &before),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "port", "443"),
Expand All @@ -167,6 +167,14 @@ func TestAccELBV2TargetGroup_ProtocolVersion_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.Name", rName),
),
},
{
Config: testAccTargetGroupConfig_protocolVersion(rName, "HTTP1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &after),
testAccCheckTargetGroupRecreated(&after, &before),
resource.TestCheckResourceAttr(resourceName, "protocol_version", "HTTP1"),
),
},
},
})
}
Expand Down Expand Up @@ -473,6 +481,7 @@ func TestAccELBV2TargetGroup_ForceNew_name(t *testing.T) {
Config: testAccTargetGroupConfig_basic(rNameAfter, 200),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &after),
testAccCheckTargetGroupRecreated(&after, &before),
resource.TestCheckResourceAttr(resourceName, "name", rNameAfter),
),
},
Expand Down Expand Up @@ -502,6 +511,7 @@ func TestAccELBV2TargetGroup_ForceNew_port(t *testing.T) {
Config: testAccTargetGroupConfig_updatedPort(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &after),
testAccCheckTargetGroupRecreated(&after, &before),
resource.TestCheckResourceAttr(resourceName, "port", "442"),
),
},
Expand Down Expand Up @@ -531,6 +541,7 @@ func TestAccELBV2TargetGroup_ForceNew_protocol(t *testing.T) {
Config: testAccTargetGroupConfig_updatedProtocol(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupExists(resourceName, &after),
testAccCheckTargetGroupRecreated(&after, &before),
resource.TestCheckResourceAttr(resourceName, "protocol", "HTTP"),
),
},
Expand Down Expand Up @@ -558,6 +569,7 @@ func TestAccELBV2TargetGroup_ForceNew_vpc(t *testing.T) {
{
Config: testAccTargetGroupConfig_updatedVPC(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckTargetGroupRecreated(&after, &before),
testAccCheckTargetGroupExists(resourceName, &after),
),
},
Expand Down Expand Up @@ -2440,13 +2452,13 @@ resource "aws_vpc" "test" {
`, rName)
}

func testAccTargetGroupConfig_protocolVersion(rName string) string {
func testAccTargetGroupConfig_protocolVersion(rName, protocolVersion string) string {
return fmt.Sprintf(`
resource "aws_lb_target_group" "test" {
name = %[1]q
port = 443
protocol = "HTTPS"
protocol_version = "HTTP2"
protocol_version = %[2]q
vpc_id = aws_vpc.test.id
deregistration_delay = 200
Expand Down Expand Up @@ -2480,7 +2492,7 @@ resource "aws_vpc" "test" {
Name = %[1]q
}
}
`, rName)
`, rName, protocolVersion)
}

func testAccTargetGroupConfig_ipAddressType(rName string) string {
Expand Down Expand Up @@ -3266,7 +3278,17 @@ func testAccCheckTargetGroupExists(n string, res *elbv2.TargetGroup) resource.Te
func testAccCheckTargetGroupNotRecreated(i, j *elbv2.TargetGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
if aws.StringValue(i.TargetGroupArn) != aws.StringValue(j.TargetGroupArn) {
return fmt.Errorf("ELBv2 Target Group (%s) unexpectedly recreated (%s)", 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
Expand Down

0 comments on commit bdb25a5

Please sign in to comment.