Skip to content

Commit

Permalink
Merge pull request #3854 from appilon/fix-alb-http2
Browse files Browse the repository at this point in the history
resource/aws_lb: Fix ELBv2 attributes on create
  • Loading branch information
appilon committed Mar 27, 2018
2 parents 7d7e837 + 81bc63f commit 96288bc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
12 changes: 6 additions & 6 deletions aws/resource_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func resourceAwsLbUpdate(d *schema.ResourceData, meta interface{}) error {

switch d.Get("load_balancer_type").(string) {
case "application":
if d.HasChange("access_logs") {
if d.HasChange("access_logs") || d.IsNewResource() {
logs := d.Get("access_logs").([]interface{})
if len(logs) == 1 {
log := logs[0].(map[string]interface{})
Expand Down Expand Up @@ -360,28 +360,28 @@ func resourceAwsLbUpdate(d *schema.ResourceData, meta interface{}) error {
})
}
}
if d.HasChange("idle_timeout") {
if d.HasChange("idle_timeout") || d.IsNewResource() {
attributes = append(attributes, &elbv2.LoadBalancerAttribute{
Key: aws.String("idle_timeout.timeout_seconds"),
Value: aws.String(fmt.Sprintf("%d", d.Get("idle_timeout").(int))),
})
}
if d.HasChange("enable_http2") {
if d.HasChange("enable_http2") || d.IsNewResource() {
attributes = append(attributes, &elbv2.LoadBalancerAttribute{
Key: aws.String("routing.http2.enabled"),
Value: aws.String(strconv.FormatBool(d.Get("enable_http2").(bool))),
})
}
case "network":
if d.HasChange("enable_cross_zone_load_balancing") {
if d.HasChange("enable_cross_zone_load_balancing") || d.IsNewResource() {
attributes = append(attributes, &elbv2.LoadBalancerAttribute{
Key: aws.String("load_balancing.cross_zone.enabled"),
Value: aws.String(fmt.Sprintf("%t", d.Get("enable_cross_zone_load_balancing").(bool))),
})
}
}

if d.HasChange("enable_deletion_protection") {
if d.HasChange("enable_deletion_protection") || d.IsNewResource() {
attributes = append(attributes, &elbv2.LoadBalancerAttribute{
Key: aws.String("deletion_protection.enabled"),
Value: aws.String(fmt.Sprintf("%t", d.Get("enable_deletion_protection").(bool))),
Expand Down Expand Up @@ -707,7 +707,7 @@ func flattenAwsLbResource(d *schema.ResourceData, meta interface{}, lb *elbv2.Lo
protectionEnabled := (*attr.Value) == "true"
log.Printf("[DEBUG] Setting LB Deletion Protection Enabled: %t", protectionEnabled)
d.Set("enable_deletion_protection", protectionEnabled)
case "enable_http2":
case "routing.http2.enabled":
http2Enabled := (*attr.Value) == "true"
log.Printf("[DEBUG] Setting ALB HTTP/2 Enabled: %t", http2Enabled)
d.Set("enable_http2", http2Enabled)
Expand Down
62 changes: 56 additions & 6 deletions aws/resource_aws_lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,15 @@ func TestAccAWSLB_networkLoadbalancer_updateCrossZone(t *testing.T) {
Config: testAccAWSLBConfig_networkLoadbalancer(lbName, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &pre),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "load_balancing.cross_zone.enabled", "true"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_cross_zone_load_balancing", "true"),
),
},
{
Config: testAccAWSLBConfig_networkLoadbalancer(lbName, false),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &mid),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "load_balancing.cross_zone.enabled", "false"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_cross_zone_load_balancing", "false"),
testAccCheckAWSlbARNs(&pre, &mid),
),
Expand All @@ -294,6 +296,7 @@ func TestAccAWSLB_networkLoadbalancer_updateCrossZone(t *testing.T) {
Config: testAccAWSLBConfig_networkLoadbalancer(lbName, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &post),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "load_balancing.cross_zone.enabled", "true"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_cross_zone_load_balancing", "true"),
testAccCheckAWSlbARNs(&mid, &post),
),
Expand All @@ -313,25 +316,28 @@ func TestAccAWSLB_applicationLoadBalancer_updateHttp2(t *testing.T) {
CheckDestroy: testAccCheckAWSLBDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLBConfig_enableHttp2(lbName, true),
Config: testAccAWSLBConfig_enableHttp2(lbName, false),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &pre),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_http2", "true"),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "routing.http2.enabled", "false"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_http2", "false"),
),
},
{
Config: testAccAWSLBConfig_enableHttp2(lbName, false),
Config: testAccAWSLBConfig_enableHttp2(lbName, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &mid),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_http2", "false"),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "routing.http2.enabled", "true"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_http2", "true"),
testAccCheckAWSlbARNs(&pre, &mid),
),
},
{
Config: testAccAWSLBConfig_enableHttp2(lbName, true),
Config: testAccAWSLBConfig_enableHttp2(lbName, false),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &post),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_http2", "true"),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "routing.http2.enabled", "false"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_http2", "false"),
testAccCheckAWSlbARNs(&mid, &post),
),
},
Expand All @@ -353,13 +359,15 @@ func TestAccAWSLB_applicationLoadBalancer_updateDeletionProtection(t *testing.T)
Config: testAccAWSLBConfig_enableDeletionProtection(lbName, false),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &pre),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "deletion_protection.enabled", "false"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"),
),
},
{
Config: testAccAWSLBConfig_enableDeletionProtection(lbName, true),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &mid),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "deletion_protection.enabled", "true"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "true"),
testAccCheckAWSlbARNs(&pre, &mid),
),
Expand All @@ -368,6 +376,7 @@ func TestAccAWSLB_applicationLoadBalancer_updateDeletionProtection(t *testing.T)
Config: testAccAWSLBConfig_enableDeletionProtection(lbName, false),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &post),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "deletion_protection.enabled", "false"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"),
testAccCheckAWSlbARNs(&mid, &post),
),
Expand Down Expand Up @@ -511,6 +520,9 @@ func TestAccAWSLB_accesslogs(t *testing.T) {
Config: testAccAWSLBConfig_basic(lbName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &conf),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.enabled", "false"),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.bucket", ""),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.prefix", ""),
resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName),
resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"),
Expand All @@ -529,6 +541,9 @@ func TestAccAWSLB_accesslogs(t *testing.T) {
Config: testAccAWSLBConfig_accessLogs(true, lbName, bucketName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &conf),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.enabled", "true"),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.bucket", bucketName),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.prefix", "testAccAWSALBConfig_accessLogs"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName),
resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"),
Expand All @@ -551,6 +566,9 @@ func TestAccAWSLB_accesslogs(t *testing.T) {
Config: testAccAWSLBConfig_accessLogs(false, lbName, bucketName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists("aws_lb.lb_test", &conf),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.enabled", "false"),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.bucket", bucketName),
testAccCheckAWSLBAttribute("aws_lb.lb_test", "access_logs.s3.prefix", "testAccAWSALBConfig_accessLogs"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName),
resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"),
resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"),
Expand Down Expand Up @@ -637,6 +655,38 @@ func testAccCheckAWSLBExists(n string, res *elbv2.LoadBalancer) resource.TestChe
}
}

func testAccCheckAWSLBAttribute(n, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return errors.New("No LB ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).elbv2conn
attributesResp, err := conn.DescribeLoadBalancerAttributes(&elbv2.DescribeLoadBalancerAttributesInput{
LoadBalancerArn: aws.String(rs.Primary.ID),
})
if err != nil {
return errwrap.Wrapf("Error retrieving LB Attributes: {{err}}", err)
}

for _, attr := range attributesResp.Attributes {
if *attr.Key == key {
if *attr.Value == value {
return nil
} else {
return fmt.Errorf("LB attribute %s expected: %q actual: %q", key, value, *attr.Value)
}
}
}
return fmt.Errorf("LB attribute %s does not exist on LB: %s", key, rs.Primary.ID)
}
}

func testAccCheckAWSLBDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).elbv2conn

Expand Down

0 comments on commit 96288bc

Please sign in to comment.