From dfe1e5dc960871ef129b973422aacc993df3ff60 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Tue, 9 Jan 2018 11:28:10 +0000 Subject: [PATCH] r/lb_target_group: Fix validation rules for LB's healthcheck --- aws/resource_aws_lb_target_group.go | 19 +++- aws/resource_aws_lb_target_group_test.go | 112 +++++++++++++++++++++-- 2 files changed, 120 insertions(+), 11 deletions(-) diff --git a/aws/resource_aws_lb_target_group.go b/aws/resource_aws_lb_target_group.go index 0e4a6622bc96..867652dd29a0 100644 --- a/aws/resource_aws_lb_target_group.go +++ b/aws/resource_aws_lb_target_group.go @@ -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()) @@ -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 } diff --git a/aws/resource_aws_lb_target_group_test.go b/aws/resource_aws_lb_target_group_test.go index 5a563cc56c94..bf4a059b38d6 100644 --- a/aws/resource_aws_lb_target_group_test.go +++ b/aws/resource_aws_lb_target_group_test.go @@ -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"), @@ -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)) @@ -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) }, @@ -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