Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/lb_target_group: Fix validation rules for LB's healthcheck #2906

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions aws/resource_aws_lb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,15 @@ func resourceAwsLbTargetGroupCustomizeDiff(diff *schema.ResourceDiff, v interfac
if len(stickinessBlocks) != 0 {
return fmt.Errorf("Network Load Balancers do not support Stickiness")
}
// Network Load Balancers have many special qwirks to them.
// See http://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_CreateTargetGroup.html
if healthChecks := diff.Get("health_check").([]interface{}); len(healthChecks) == 1 {
healthCheck := healthChecks[0].(map[string]interface{})
}

// Network Load Balancers have many special qwirks to them.
// See http://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_CreateTargetGroup.html
if healthChecks := diff.Get("health_check").([]interface{}); len(healthChecks) == 1 {
healthCheck := healthChecks[0].(map[string]interface{})
protocol := healthCheck["protocol"].(string)

if protocol == "TCP" {
// Cannot set custom matcher on TCP health checks
if m := healthCheck["matcher"].(string); m != "" {
return fmt.Errorf("%s: custom matcher is not supported for target_groups with TCP protocol", diff.Id())
Expand Down Expand Up @@ -642,6 +647,12 @@ func resourceAwsLbTargetGroupCustomizeDiff(diff *schema.ResourceDiff, v interfac
" use 'terraform taint' to recreate the resource if you wish",
old, new, diff.Id())
}
if diff.HasChange("health_check.0.timeout") {
old, new := diff.GetChange("health_check.0.timeout")
return fmt.Errorf("Health check timeout cannot be updated from %d to %d for TCP based Target Group %s,"+
" use 'terraform taint' to recreate the resource if you wish",
old, new, diff.Id())
}
}
return nil
}
112 changes: 105 additions & 7 deletions aws/resource_aws_lb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestAccAWSLBTargetGroup_networkLB_TargetGroup(t *testing.T) {
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "traffic-port"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "TCP"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "10"),
testAccCheckAWSLBTargetGroupHealthCheckTimeout(&confBefore, 10),
testAccCheckAWSLBTargetGroupHealthCheckTimeout(&confAfter, 10),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "5"),
testAccCheckAWSLBTargetGroupHealthyThreshold(&confAfter, 5),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "5"),
Expand All @@ -156,6 +156,73 @@ func TestAccAWSLBTargetGroup_networkLB_TargetGroup(t *testing.T) {
})
}

func TestAccAWSLBTargetGroup_TCP_HTTPHealthCheck(t *testing.T) {
var confBefore, confAfter elbv2.TargetGroup
rString := acctest.RandString(8)
targetGroupName := fmt.Sprintf("test-tg-tcp-http-hc-%s", rString)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_lb_target_group.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLBTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLBTargetGroupConfig_typeTCP_HTTPHealthCheck(targetGroupName, "/healthz", 2),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &confBefore),
resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "8082"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "TCP"),
resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "300"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "30"),
testAccCheckAWSLBTargetGroupHealthCheckInterval(&confBefore, 30),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/healthz"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "443"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTPS"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "10"),
testAccCheckAWSLBTargetGroupHealthCheckTimeout(&confBefore, 10),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "2"),
testAccCheckAWSLBTargetGroupHealthyThreshold(&confBefore, 2),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "2"),
testAccCheckAWSLBTargetGroupUnhealthyThreshold(&confBefore, 2),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.Name", "TestAcc_networkLB_HTTPHealthCheck"),
),
},
{
Config: testAccAWSLBTargetGroupConfig_typeTCP_HTTPHealthCheck(targetGroupName, "/healthz2", 4),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &confAfter),
resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "8082"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "TCP"),
resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "300"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "30"),
testAccCheckAWSLBTargetGroupHealthCheckInterval(&confAfter, 30),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/healthz2"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "443"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTPS"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "10"),
testAccCheckAWSLBTargetGroupHealthCheckTimeout(&confAfter, 10),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "4"),
testAccCheckAWSLBTargetGroupHealthyThreshold(&confAfter, 4),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "4"),
testAccCheckAWSLBTargetGroupUnhealthyThreshold(&confAfter, 4),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.Name", "TestAcc_networkLB_HTTPHealthCheck"),
),
},
},
})
}

func TestAccAWSLBTargetGroupBackwardsCompatibility(t *testing.T) {
var conf elbv2.TargetGroup
targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -574,24 +641,24 @@ func TestAccAWSLBTargetGroup_defaults_network(t *testing.T) {
interval = 10
port = 8081
protocol = "TCP"
`
`
healthCheckInvalid2 := `
interval = 10
port = 8081
protocol = "TCP"
matcher = "200"
`
matcher = "200"
`
healthCheckInvalid3 := `
interval = 10
port = 8081
protocol = "TCP"
timeout = 4
`
timeout = 4
`
healthCheckValid := `
interval = 10
port = 8081
protocol = "TCP"
`
`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -1189,6 +1256,37 @@ resource "aws_vpc" "test" {
}`, targetGroupName)
}

func testAccAWSLBTargetGroupConfig_typeTCP_HTTPHealthCheck(targetGroupName, path string, threshold int) string {
return fmt.Sprintf(`resource "aws_lb_target_group" "test" {
name = "%[1]s"
port = 8082
protocol = "TCP"
vpc_id = "${aws_vpc.test.id}"

health_check {
healthy_threshold = %[2]d
unhealthy_threshold = %[2]d
timeout = "10"
port = "443"
path = "%[3]s"
protocol = "HTTPS"
interval = 30
matcher = "200-399"
}

tags {
Name = "TestAcc_networkLB_HTTPHealthCheck"
}
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags {
Name = "TestAcc_networkLB_HTTPHealthCheck"
}
}`, targetGroupName, threshold, path)
}

func testAccAWSLBTargetGroupConfig_stickiness(targetGroupName string, addStickinessBlock bool, enabled bool) string {
var stickinessBlock string

Expand Down