diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 2f5069cde..ef5f459a9 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -76,7 +76,7 @@ func resource_aws_elb_create( } } } - + if _, ok := rs.Attributes["health_check.#"]; ok { v := flatmap.Expand(rs.Attributes, "health_check").([]interface{}) health_check := v[0].(map[string]interface{}) @@ -92,11 +92,11 @@ func resource_aws_elb_create( configureHealthCheckOpts := elb.ConfigureHealthCheck{ LoadBalancerName: elbName, Check: elb.HealthCheck{ - HealthyThreshold: healthyThreshold, + HealthyThreshold: healthyThreshold, UnhealthyThreshold: unhealthyThreshold, - Interval: interval, - Target: health_check["target"].(string), - Timeout: timeout, + Interval: interval, + Target: health_check["target"].(string), + Timeout: timeout, }, } @@ -273,9 +273,16 @@ func resource_aws_elb_update_state( if len(balancer.Instances) > 0 && balancer.Instances[0].InstanceId != "" { toFlatten["instances"] = flattenInstances(balancer.Instances) - for k, v := range flatmap.Flatten(toFlatten) { - s.Attributes[k] = v - } + } + + // There's only one health check, so save that to state as we + // currently can + if balancer.HealthCheck.Target != "" { + toFlatten["health_check"] = flattenHealthCheck(balancer.HealthCheck) + } + + for k, v := range flatmap.Flatten(toFlatten) { + s.Attributes[k] = v } return s, nil diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index 5838781d2..7edeb6d85 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -81,6 +81,34 @@ func TestAccAWSELB_InstanceAttaching(t *testing.T) { }) } +func TestAccAWSELB_HealthCheck(t *testing.T) { + var conf elb.LoadBalancer + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSELBDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSELBConfigHealthCheck, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSELBExists("aws_elb.bar", &conf), + testAccCheckAWSELBAttributesHealthCheck(&conf), + resource.TestCheckResourceAttr( + "aws_elb.bar", "health_check.0.healthy_threshold", "5"), + resource.TestCheckResourceAttr( + "aws_elb.bar", "health_check.0.unhealthy_threshold", "5"), + resource.TestCheckResourceAttr( + "aws_elb.bar", "health_check.0.target", "HTTP:8000/"), + resource.TestCheckResourceAttr( + "aws_elb.bar", "health_check.0.timeout", "30"), + resource.TestCheckResourceAttr( + "aws_elb.bar", "health_check.0.interval", "60"), + ), + }, + }, + }) +} func testAccCheckAWSELBDestroy(s *terraform.State) error { conn := testAccProvider.elbconn @@ -146,6 +174,39 @@ func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc } } +func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.TestCheckFunc { + return func(s *terraform.State) error { + if conf.AvailabilityZones[0].AvailabilityZone != "us-west-2a" { + return fmt.Errorf("bad availability_zones") + } + + if conf.LoadBalancerName != "foobar-terraform-test" { + return fmt.Errorf("bad name") + } + + check := elb.HealthCheck{ + Timeout: 30, + UnhealthyThreshold: 5, + HealthyThreshold: 5, + Interval: 60, + Target: "HTTP:8000/", + } + + if !reflect.DeepEqual(conf.HealthCheck, check) { + return fmt.Errorf( + "Got:\n\n%#v\n\nExpected:\n\n%#v\n", + conf.HealthCheck, + check) + } + + if conf.DNSName == "" { + return fmt.Errorf("empty dns_name") + } + + return nil + } +} + func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.Resources[n] @@ -215,3 +276,25 @@ resource "aws_instance" "foo" { instance_type = "t1.micro" } ` + +const testAccAWSELBConfigHealthCheck = ` +resource "aws_elb" "bar" { + name = "foobar-terraform-test" + availability_zones = ["us-west-2a"] + + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } + + health_check { + healthy_threshold = 5 + unhealthy_threshold = 5 + target = "HTTP:8000/" + interval = 60 + timeout = 30 + } +} +` diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 361c95cee..a8be14675 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -126,6 +126,23 @@ func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} { return result } +// Flattens a health check into something that flatmap.Flatten() +// can handle +func flattenHealthCheck(check elb.HealthCheck) []map[string]interface{} { + result := make([]map[string]interface{}, 0, 1) + + chk := make(map[string]interface{}) + chk["unhealthy_threshold"] = int(check.UnhealthyThreshold) + chk["healthy_threshold"] = int(check.HealthyThreshold) + chk["target"] = check.Target + chk["timeout"] = int(check.Timeout) + chk["interval"] = int(check.Interval) + + result = append(result, chk) + + return result +} + // Flattens an array of UserSecurityGroups into a []string func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string { result := make([]string, 0, len(list)) diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go index 0b5c46225..9d438da2c 100644 --- a/builtin/providers/aws/structure_test.go +++ b/builtin/providers/aws/structure_test.go @@ -206,6 +206,39 @@ func Test_expandListeners(t *testing.T) { } +func Test_flattenHealthCheck(t *testing.T) { + cases := []struct { + Input elb.HealthCheck + Output []map[string]interface{} + }{ + { + Input: elb.HealthCheck{ + UnhealthyThreshold: 10, + HealthyThreshold: 10, + Target: "HTTP:80/", + Timeout: 30, + Interval: 30, + }, + Output: []map[string]interface{}{ + map[string]interface{}{ + "unhealthy_threshold": 10, + "healthy_threshold": 10, + "target": "HTTP:80/", + "timeout": 30, + "interval": 30, + }, + }, + }, + } + + for _, tc := range cases { + output := flattenHealthCheck(tc.Input) + if !reflect.DeepEqual(output, tc.Output) { + t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) + } + } +} + func Test_expandStringList(t *testing.T) { expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{}) stringList := expandStringList(expanded)