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

Add new attribute slow_start to aws_lb_target_group #4661

Merged
merged 6 commits into from
May 30, 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
5 changes: 5 additions & 0 deletions aws/data_source_aws_lb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func dataSourceAwsLbTargetGroup() *schema.Resource {
Computed: true,
},

"slow_start": {
Type: schema.TypeInt,
Computed: true,
},

"stickiness": {
Type: schema.TypeList,
Computed: true,
Expand Down
4 changes: 4 additions & 0 deletions aws/data_source_aws_lb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestAccDataSourceAWSALBTargetGroup_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"),
resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_arn", "vpc_id"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "deregistration_delay", "300"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "slow_start", "0"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data source testing is failing because d.Set("slow_start", ...) is never called in the resource read function. This would normally be caught in the resource acceptance testing with a TestCase that calls ImportStateVerify: true.

=== RUN   TestAccDataSourceAWSALBTargetGroup_basic
--- FAIL: TestAccDataSourceAWSALBTargetGroup_basic (494.14s)
    testing.go:518: Step 0 error: Check failed: 2 error(s) occurred:
        
        * Check 9/39 error: data.aws_lb_target_group.alb_tg_test_with_arn: Attribute 'slow_start' not found
        * Check 28/39 error: data.aws_lb_target_group.alb_tg_test_with_name: Attribute 'slow_start' not found

resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "tags.%", "1"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "stickiness.#", "1"),
Expand All @@ -46,6 +47,7 @@ func TestAccDataSourceAWSALBTargetGroup_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "protocol", "HTTP"),
resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_name", "vpc_id"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "deregistration_delay", "300"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "slow_start", "0"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "tags.%", "1"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"),
resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "stickiness.#", "1"),
Expand Down Expand Up @@ -82,6 +84,7 @@ func TestAccDataSourceAWSLBTargetGroupBackwardsCompatibility(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"),
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "vpc_id"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "deregistration_delay", "300"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "slow_start", "0"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.%", "1"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "stickiness.#", "1"),
Expand All @@ -101,6 +104,7 @@ func TestAccDataSourceAWSLBTargetGroupBackwardsCompatibility(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "protocol", "HTTP"),
resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "vpc_id"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "deregistration_delay", "300"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "slow_start", "0"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.%", "1"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"),
resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "stickiness.#", "1"),
Expand Down
69 changes: 69 additions & 0 deletions aws/resource_aws_alb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestAccAWSALBTargetGroup_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "slow_start", "0"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need to check slow_start being equal to "0" in the _basic resource test and its configuration as the default applies to all configurations unless overridden. Speaking of which, we should have a completely separate acceptance test and configuration that actually sets this to a non-0 value and preferably tries to update it as well so we know that functionality works.

resource.TestCheckResourceAttr("aws_alb_target_group.test", "target_type", "instance"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"),
Expand Down Expand Up @@ -415,6 +416,34 @@ func TestAccAWSALBTargetGroup_updateSticknessEnabled(t *testing.T) {
})
}

func TestAccAWSALBTargetGroup_setAndUpdateSlowStart(t *testing.T) {
var before, after elbv2.TargetGroup
targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_alb_target_group.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSALBTargetGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSALBTargetGroupConfig_updateSlowStart(targetGroupName, 30),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "slow_start", "30"),
),
},
{
Config: testAccAWSALBTargetGroupConfig_updateSlowStart(targetGroupName, 60),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "slow_start", "60"),
),
},
},
})
}

func testAccCheckAWSALBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -757,6 +786,46 @@ resource "aws_vpc" "test" {
}`, targetGroupName, stickinessBlock)
}

func testAccAWSALBTargetGroupConfig_updateSlowStart(targetGroupName string, slowStartDuration int) string {
return fmt.Sprintf(`resource "aws_alb_target_group" "test" {
name = "%s"
port = 443
protocol = "HTTP"
vpc_id = "${aws_vpc.test.id}"

deregistration_delay = 200
slow_start = %d

stickiness {
type = "lb_cookie"
cookie_duration = 10000
}

health_check {
path = "/health"
interval = 60
port = 8081
protocol = "HTTP"
timeout = 3
healthy_threshold = 3
unhealthy_threshold = 3
matcher = "200-299"
}

tags {
TestName = "TestAccAWSALBTargetGroup_SlowStart"
}
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

tags {
Name = "terraform-testacc-alb-target-group-slowstart"
}
}`, targetGroupName, slowStartDuration)
}

const testAccAWSALBTargetGroupConfig_namePrefix = `
resource "aws_alb_target_group" "test" {
name_prefix = "tf-"
Expand Down
27 changes: 27 additions & 0 deletions aws/resource_aws_lb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ func resourceAwsLbTargetGroup() *schema.Resource {
ValidateFunc: validation.IntBetween(0, 3600),
},

"slow_start": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
ValidateFunc: validateSlowStart,
},

"proxy_protocol_v2": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -339,6 +346,13 @@ func resourceAwsLbTargetGroupUpdate(d *schema.ResourceData, meta interface{}) er
})
}

if d.HasChange("slow_start") {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("slow_start.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", d.Get("slow_start").(int))),
})
}

if d.HasChange("proxy_protocol_v2") {
attrs = append(attrs, &elbv2.TargetGroupAttribute{
Key: aws.String("proxy_protocol_v2.enabled"),
Expand Down Expand Up @@ -422,6 +436,19 @@ func validateAwsLbTargetGroupHealthCheckPath(v interface{}, k string) (ws []stri
return
}

func validateSlowStart(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)

// Check if the value is between 30-900 or 0 (seconds).
if value != 0 && !(value >= 30 && value <= 900) {
errors = append(errors, fmt.Errorf(
"%q contains an invalid Slow Start Duration \"%d\". "+
"Valid intervals are 30-900 or 0 to disable.",
k, value))
}
return
}

func validateAwsLbTargetGroupHealthCheckPort(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

Expand Down
8 changes: 8 additions & 0 deletions aws/resource_aws_lb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestAccAWSLBTargetGroup_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"),
resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "slow_start", "0"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.#", "1"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.enabled", "true"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.type", "lb_cookie"),
Expand Down Expand Up @@ -276,6 +277,7 @@ func TestAccAWSLBTargetGroupBackwardsCompatibility(t *testing.T) {
resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"),
resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "slow_start", "0"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"),
resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"),
Expand Down Expand Up @@ -651,6 +653,7 @@ func TestAccAWSLBTargetGroup_defaults_application(t *testing.T) {
resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTP"),
resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "slow_start", "0"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "10"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8081"),
Expand Down Expand Up @@ -721,6 +724,7 @@ func TestAccAWSLBTargetGroup_defaults_network(t *testing.T) {
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", "200"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "slow_start", "0"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "10"),
resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8081"),
Expand Down Expand Up @@ -899,6 +903,7 @@ resource "aws_lb_target_group" "test" {
vpc_id = "${aws_vpc.test.id}"

deregistration_delay = 200
slow_start = 0

# HTTP Only
stickiness {
Expand Down Expand Up @@ -936,6 +941,7 @@ resource "aws_lb_target_group" "test" {
vpc_id = "${aws_vpc.test.id}"

deregistration_delay = 200
slow_start = 0

health_check {
%s
Expand Down Expand Up @@ -963,6 +969,7 @@ func testAccAWSLBTargetGroupConfig_basic(targetGroupName string) string {
vpc_id = "${aws_vpc.test.id}"

deregistration_delay = 200
slow_start = 0

stickiness {
type = "lb_cookie"
Expand Down Expand Up @@ -1002,6 +1009,7 @@ func testAccAWSLBTargetGroupConfigBackwardsCompatibility(targetGroupName string)
vpc_id = "${aws_vpc.test.id}"

deregistration_delay = 200
slow_start = 0

stickiness {
type = "lb_cookie"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/lb_target_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following arguments are supported:
* `protocol` - (Required) The protocol to use for routing traffic to the targets.
* `vpc_id` - (Required) The identifier of the VPC in which to create the target group.
* `deregistration_delay` - (Optional) The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. The default value is 300 seconds.
* `slow_start` - (Optional) The amount time for targets to warm up before the load balancer sends them a full share of requests. The range is 30-900 seconds or 0 to disable. The default value is 0 seconds.
* `proxy_protocol_v2` - (Optional) Boolean to enable / disable support for proxy protocol v2 on Network Load Balancers. See [doc](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-target-groups.html#proxy-protocol) for more information.
* `stickiness` - (Optional) A Stickiness block. Stickiness blocks are documented below. `stickiness` is only valid if used with Load Balancers of type `Application`
* `health_check` - (Optional) A Health Check block. Health Check blocks are documented below.
Expand Down