From da55f9bf85c9eba237affafcad69f1982ded89fd Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Mon, 2 Mar 2015 09:44:06 -0600 Subject: [PATCH] provider/aws: Convert AWS ELB to aws-sdk-go --- builtin/providers/aws/config.go | 4 +- builtin/providers/aws/resource_aws_elb.go | 96 ++++++++++--------- .../providers/aws/resource_aws_elb_test.go | 67 ++++++------- builtin/providers/aws/structure.go | 55 +++++++---- builtin/providers/aws/structure_test.go | 22 ++--- 5 files changed, 133 insertions(+), 111 deletions(-) diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index b6ed66a3b..f3071148d 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -9,10 +9,10 @@ import ( "github.com/hashicorp/terraform/helper/multierror" "github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/ec2" - "github.com/mitchellh/goamz/elb" awsGo "github.com/hashicorp/aws-sdk-go/aws" "github.com/hashicorp/aws-sdk-go/gen/autoscaling" + "github.com/hashicorp/aws-sdk-go/gen/elb" "github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/hashicorp/aws-sdk-go/gen/route53" "github.com/hashicorp/aws-sdk-go/gen/s3" @@ -63,7 +63,7 @@ func (c *Config) Client() (interface{}, error) { log.Println("[INFO] Initializing EC2 connection") client.ec2conn = ec2.New(auth, region) log.Println("[INFO] Initializing ELB connection") - client.elbconn = elb.New(auth, region) + client.elbconn = elb.New(creds, c.Region, nil) log.Println("[INFO] Initializing AutoScaling connection") client.autoscalingconn = autoscaling.New(creds, c.Region, nil) log.Println("[INFO] Initializing S3 connection") diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 66a5b7c21..e5ed9f3cf 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -5,9 +5,10 @@ import ( "fmt" "log" + "github.com/hashicorp/aws-sdk-go/aws" + "github.com/hashicorp/aws-sdk-go/gen/elb" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" - "github.com/mitchellh/goamz/elb" ) func resourceAwsElb() *schema.Resource { @@ -167,14 +168,18 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error { } // Provision the elb - elbOpts := &elb.CreateLoadBalancer{ - LoadBalancerName: d.Get("name").(string), + + elbOpts := &elb.CreateAccessPointInput{ + LoadBalancerName: aws.String(d.Get("name").(string)), Listeners: listeners, - Internal: d.Get("internal").(bool), + } + + if scheme, ok := d.GetOk("internal"); ok && scheme.(bool) { + elbOpts.Scheme = aws.String("internal") } if v, ok := d.GetOk("availability_zones"); ok { - elbOpts.AvailZone = expandStringList(v.(*schema.Set).List()) + elbOpts.AvailabilityZones = expandStringList(v.(*schema.Set).List()) } if v, ok := d.GetOk("security_groups"); ok { @@ -208,14 +213,14 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error { if len(vs) > 0 { check := vs[0].(map[string]interface{}) - configureHealthCheckOpts := elb.ConfigureHealthCheck{ - LoadBalancerName: d.Id(), - Check: elb.HealthCheck{ - HealthyThreshold: int64(check["healthy_threshold"].(int)), - UnhealthyThreshold: int64(check["unhealthy_threshold"].(int)), - Interval: int64(check["interval"].(int)), - Target: check["target"].(string), - Timeout: int64(check["timeout"].(int)), + configureHealthCheckOpts := elb.ConfigureHealthCheckInput{ + LoadBalancerName: aws.String(d.Id()), + HealthCheck: &elb.HealthCheck{ + HealthyThreshold: aws.Integer(check["healthy_threshold"].(int)), + UnhealthyThreshold: aws.Integer(check["unhealthy_threshold"].(int)), + Interval: aws.Integer(check["interval"].(int)), + Target: aws.String(check["target"].(string)), + Timeout: aws.Integer(check["timeout"].(int)), }, } @@ -233,13 +238,13 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbconn // Retrieve the ELB properties for updating the state - describeElbOpts := &elb.DescribeLoadBalancer{ - Names: []string{d.Id()}, + describeElbOpts := &elb.DescribeAccessPointsInput{ + LoadBalancerNames: []string{d.Id()}, } describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts) if err != nil { - if ec2err, ok := err.(*elb.Error); ok && ec2err.Code == "LoadBalancerNotFound" { + if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "LoadBalancerNotFound" { // The ELB is gone now, so just remove it from the state d.SetId("") return nil @@ -247,24 +252,24 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error retrieving ELB: %s", err) } - if len(describeResp.LoadBalancers) != 1 { - return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancers) + if len(describeResp.LoadBalancerDescriptions) != 1 { + return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions) } - lb := describeResp.LoadBalancers[0] + lb := describeResp.LoadBalancerDescriptions[0] - d.Set("name", lb.LoadBalancerName) - d.Set("dns_name", lb.DNSName) - d.Set("internal", lb.Scheme == "internal") + d.Set("name", *lb.LoadBalancerName) + d.Set("dns_name", *lb.DNSName) + d.Set("internal", *lb.Scheme == "internal") d.Set("availability_zones", lb.AvailabilityZones) d.Set("instances", flattenInstances(lb.Instances)) - d.Set("listener", flattenListeners(lb.Listeners)) + d.Set("listener", flattenListeners(lb.ListenerDescriptions)) d.Set("security_groups", lb.SecurityGroups) d.Set("subnets", lb.Subnets) // There's only one health check, so save that to state as we // currently can - if lb.HealthCheck.Target != "" { + if *lb.HealthCheck.Target != "" { d.Set("health_check", flattenHealthCheck(lb.HealthCheck)) } @@ -283,12 +288,12 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { o, n := d.GetChange("instances") os := o.(*schema.Set) ns := n.(*schema.Set) - remove := expandStringList(os.Difference(ns).List()) - add := expandStringList(ns.Difference(os).List()) + remove := expandInstanceString(os.Difference(ns).List()) + add := expandInstanceString(ns.Difference(os).List()) if len(add) > 0 { - registerInstancesOpts := elb.RegisterInstancesWithLoadBalancer{ - LoadBalancerName: d.Id(), + registerInstancesOpts := elb.RegisterEndPointsInput{ + LoadBalancerName: aws.String(d.Id()), Instances: add, } @@ -298,8 +303,8 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { } } if len(remove) > 0 { - deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancer{ - LoadBalancerName: d.Id(), + deRegisterInstancesOpts := elb.DeregisterEndPointsInput{ + LoadBalancerName: aws.String(d.Id()), Instances: remove, } @@ -315,10 +320,12 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { log.Println("[INFO] outside modify attributes") if d.HasChange("cross_zone_load_balancing") { log.Println("[INFO] inside modify attributes") - attrs := elb.ModifyLoadBalancerAttributes{ - LoadBalancerName: d.Get("name").(string), - LoadBalancerAttributes: elb.LoadBalancerAttributes{ - CrossZoneLoadBalancingEnabled: d.Get("cross_zone_load_balancing").(bool), + attrs := elb.ModifyLoadBalancerAttributesInput{ + LoadBalancerName: aws.String(d.Get("name").(string)), + LoadBalancerAttributes: &elb.LoadBalancerAttributes{ + CrossZoneLoadBalancing: &elb.CrossZoneLoadBalancing{ + aws.Boolean(d.Get("cross_zone_load_balancing").(bool)), + }, }, } _, err := elbconn.ModifyLoadBalancerAttributes(&attrs) @@ -332,14 +339,14 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { vs := d.Get("health_check").(*schema.Set).List() if len(vs) > 0 { check := vs[0].(map[string]interface{}) - configureHealthCheckOpts := elb.ConfigureHealthCheck{ - LoadBalancerName: d.Id(), - Check: elb.HealthCheck{ - HealthyThreshold: int64(check["healthy_threshold"].(int)), - UnhealthyThreshold: int64(check["unhealthy_threshold"].(int)), - Interval: int64(check["interval"].(int)), - Target: check["target"].(string), - Timeout: int64(check["timeout"].(int)), + configureHealthCheckOpts := elb.ConfigureHealthCheckInput{ + LoadBalancerName: aws.String(d.Id()), + HealthCheck: &elb.HealthCheck{ + HealthyThreshold: aws.Integer(check["healthy_threshold"].(int)), + UnhealthyThreshold: aws.Integer(check["unhealthy_threshold"].(int)), + Interval: aws.Integer(check["interval"].(int)), + Target: aws.String(check["target"].(string)), + Timeout: aws.Integer(check["timeout"].(int)), }, } _, err := elbconn.ConfigureHealthCheck(&configureHealthCheckOpts) @@ -351,6 +358,7 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error { } d.Partial(false) + return resourceAwsElbRead(d, meta) } @@ -360,8 +368,8 @@ func resourceAwsElbDelete(d *schema.ResourceData, meta interface{}) error { log.Printf("[INFO] Deleting ELB: %s", d.Id()) // Destroy the load balancer - deleteElbOpts := elb.DeleteLoadBalancer{ - LoadBalancerName: d.Id(), + deleteElbOpts := elb.DeleteAccessPointInput{ + LoadBalancerName: aws.String(d.Id()), } if _, err := elbconn.DeleteLoadBalancer(&deleteElbOpts); err != nil { return fmt.Errorf("Error deleting ELB: %s", err) diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index 323130c71..037a9557d 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -7,13 +7,14 @@ import ( "sort" "testing" + "github.com/hashicorp/aws-sdk-go/aws" + "github.com/hashicorp/aws-sdk-go/gen/elb" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "github.com/mitchellh/goamz/elb" ) func TestAccAWSELB_basic(t *testing.T) { - var conf elb.LoadBalancer + var conf elb.LoadBalancerDescription ssl_certificate_id := os.Getenv("AWS_SSL_CERTIFICATE_ID") resource.Test(t, resource.TestCase{ @@ -53,7 +54,7 @@ func TestAccAWSELB_basic(t *testing.T) { } func TestAccAWSELB_InstanceAttaching(t *testing.T) { - var conf elb.LoadBalancer + var conf elb.LoadBalancerDescription testCheckInstanceAttached := func(count int) resource.TestCheckFunc { return func(*terraform.State) error { @@ -89,7 +90,7 @@ func TestAccAWSELB_InstanceAttaching(t *testing.T) { } func TestAccAWSELB_HealthCheck(t *testing.T) { - var conf elb.LoadBalancer + var conf elb.LoadBalancerDescription resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -149,19 +150,19 @@ func testAccCheckAWSELBDestroy(s *terraform.State) error { continue } - describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ - Names: []string{rs.Primary.ID}, + describe, err := conn.DescribeLoadBalancers(&elb.DescribeAccessPointsInput{ + LoadBalancerNames: []string{rs.Primary.ID}, }) if err == nil { - if len(describe.LoadBalancers) != 0 && - describe.LoadBalancers[0].LoadBalancerName == rs.Primary.ID { + if len(describe.LoadBalancerDescriptions) != 0 && + *describe.LoadBalancerDescriptions[0].LoadBalancerName == rs.Primary.ID { return fmt.Errorf("ELB still exists") } } // Verify the error - providerErr, ok := err.(*elb.Error) + providerErr, ok := err.(aws.APIError) if !ok { return err } @@ -174,7 +175,7 @@ func testAccCheckAWSELBDestroy(s *terraform.State) error { return nil } -func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc { +func testAccCheckAWSELBAttributes(conf *elb.LoadBalancerDescription) resource.TestCheckFunc { return func(s *terraform.State) error { zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} sort.StringSlice(conf.AvailabilityZones).Sort() @@ -182,25 +183,25 @@ func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc return fmt.Errorf("bad availability_zones") } - if conf.LoadBalancerName != "foobar-terraform-test" { + if *conf.LoadBalancerName != "foobar-terraform-test" { return fmt.Errorf("bad name") } l := elb.Listener{ - InstancePort: 8000, - InstanceProtocol: "HTTP", - LoadBalancerPort: 80, - Protocol: "HTTP", + InstancePort: aws.Integer(8000), + InstanceProtocol: aws.String("HTTP"), + LoadBalancerPort: aws.Integer(80), + Protocol: aws.String("HTTP"), } - if !reflect.DeepEqual(conf.Listeners[0], l) { + if !reflect.DeepEqual(conf.ListenerDescriptions[0].Listener, &l) { return fmt.Errorf( "Got:\n\n%#v\n\nExpected:\n\n%#v\n", - conf.Listeners[0], + conf.ListenerDescriptions[0].Listener, l) } - if conf.DNSName == "" { + if *conf.DNSName == "" { return fmt.Errorf("empty dns_name") } @@ -208,7 +209,7 @@ func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc } } -func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.TestCheckFunc { +func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancerDescription) resource.TestCheckFunc { return func(s *terraform.State) error { zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} sort.StringSlice(conf.AvailabilityZones).Sort() @@ -216,26 +217,26 @@ func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.Te return fmt.Errorf("bad availability_zones") } - if conf.LoadBalancerName != "foobar-terraform-test" { + 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/", + Timeout: aws.Integer(30), + UnhealthyThreshold: aws.Integer(5), + HealthyThreshold: aws.Integer(5), + Interval: aws.Integer(60), + Target: aws.String("HTTP:8000/"), } - if !reflect.DeepEqual(conf.HealthCheck, check) { + 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 == "" { + if *conf.DNSName == "" { return fmt.Errorf("empty dns_name") } @@ -243,7 +244,7 @@ func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.Te } } -func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc { +func testAccCheckAWSELBExists(n string, res *elb.LoadBalancerDescription) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -256,20 +257,20 @@ func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestChec conn := testAccProvider.Meta().(*AWSClient).elbconn - describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ - Names: []string{rs.Primary.ID}, + describe, err := conn.DescribeLoadBalancers(&elb.DescribeAccessPointsInput{ + LoadBalancerNames: []string{rs.Primary.ID}, }) if err != nil { return err } - if len(describe.LoadBalancers) != 1 || - describe.LoadBalancers[0].LoadBalancerName != rs.Primary.ID { + if len(describe.LoadBalancerDescriptions) != 1 || + *describe.LoadBalancerDescriptions[0].LoadBalancerName != rs.Primary.ID { return fmt.Errorf("ELB not found") } - *res = describe.LoadBalancers[0] + *res = describe.LoadBalancerDescriptions[0] return nil } diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go index 8943164de..7d4793d3d 100644 --- a/builtin/providers/aws/structure.go +++ b/builtin/providers/aws/structure.go @@ -4,10 +4,10 @@ import ( "strings" "github.com/hashicorp/aws-sdk-go/aws" + "github.com/hashicorp/aws-sdk-go/gen/elb" "github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/hashicorp/terraform/helper/schema" "github.com/mitchellh/goamz/ec2" - "github.com/mitchellh/goamz/elb" ) // Takes the result of flatmap.Expand for an array of listeners and @@ -21,14 +21,14 @@ func expandListeners(configured []interface{}) ([]elb.Listener, error) { data := lRaw.(map[string]interface{}) l := elb.Listener{ - InstancePort: int64(data["instance_port"].(int)), - InstanceProtocol: data["instance_protocol"].(string), - LoadBalancerPort: int64(data["lb_port"].(int)), - Protocol: data["lb_protocol"].(string), + InstancePort: aws.Integer(data["instance_port"].(int)), + InstanceProtocol: aws.String(data["instance_protocol"].(string)), + LoadBalancerPort: aws.Integer(data["lb_port"].(int)), + Protocol: aws.String(data["lb_protocol"].(string)), } if v, ok := data["ssl_certificate_id"]; ok { - l.SSLCertificateId = v.(string) + l.SSLCertificateID = aws.String(v.(string)) } listeners = append(listeners, l) @@ -138,15 +138,15 @@ func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} { // Flattens a health check into something that flatmap.Flatten() // can handle -func flattenHealthCheck(check elb.HealthCheck) []map[string]interface{} { +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) + chk["unhealthy_threshold"] = *check.UnhealthyThreshold + chk["healthy_threshold"] = *check.HealthyThreshold + chk["target"] = *check.Target + chk["timeout"] = *check.Timeout + chk["interval"] = *check.Interval result = append(result, chk) @@ -166,22 +166,35 @@ func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string { func flattenInstances(list []elb.Instance) []string { result := make([]string, 0, len(list)) for _, i := range list { - result = append(result, i.InstanceId) + result = append(result, *i.InstanceID) + } + return result +} + +// Expands an array of String Instance IDs into a []Instances +func expandInstanceString(list []interface{}) []elb.Instance { + result := make([]elb.Instance, 0, len(list)) + for _, i := range list { + result = append(result, elb.Instance{aws.String(i.(string))}) } return result } // Flattens an array of Listeners into a []map[string]interface{} -func flattenListeners(list []elb.Listener) []map[string]interface{} { +func flattenListeners(list []elb.ListenerDescription) []map[string]interface{} { result := make([]map[string]interface{}, 0, len(list)) for _, i := range list { - result = append(result, map[string]interface{}{ - "instance_port": i.InstancePort, - "instance_protocol": strings.ToLower(i.InstanceProtocol), - "ssl_certificate_id": i.SSLCertificateId, - "lb_port": i.LoadBalancerPort, - "lb_protocol": strings.ToLower(i.Protocol), - }) + l := map[string]interface{}{ + "instance_port": *i.Listener.InstancePort, + "instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol), + "lb_port": *i.Listener.LoadBalancerPort, + "lb_protocol": strings.ToLower(*i.Listener.Protocol), + } + // SSLCertificateID is optional, and may be nil + if i.Listener.SSLCertificateID != nil { + l["ssl_certificate_id"] = *i.Listener.SSLCertificateID + } + result = append(result, l) } return result } diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go index ed0f44a0b..580fc9cdf 100644 --- a/builtin/providers/aws/structure_test.go +++ b/builtin/providers/aws/structure_test.go @@ -5,12 +5,12 @@ import ( "testing" "github.com/hashicorp/aws-sdk-go/aws" + "github.com/hashicorp/aws-sdk-go/gen/elb" "github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" "github.com/mitchellh/goamz/ec2" - "github.com/mitchellh/goamz/elb" ) // Returns test configuration @@ -192,10 +192,10 @@ func Test_expandListeners(t *testing.T) { } expected := elb.Listener{ - InstancePort: 8000, - LoadBalancerPort: 80, - InstanceProtocol: "http", - Protocol: "http", + InstancePort: aws.Integer(8000), + LoadBalancerPort: aws.Integer(80), + InstanceProtocol: aws.String("http"), + Protocol: aws.String("http"), } if !reflect.DeepEqual(listeners[0], expected) { @@ -214,11 +214,11 @@ func Test_flattenHealthCheck(t *testing.T) { }{ { Input: elb.HealthCheck{ - UnhealthyThreshold: 10, - HealthyThreshold: 10, - Target: "HTTP:80/", - Timeout: 30, - Interval: 30, + UnhealthyThreshold: aws.Integer(10), + HealthyThreshold: aws.Integer(10), + Target: aws.String("HTTP:80/"), + Timeout: aws.Integer(30), + Interval: aws.Integer(30), }, Output: []map[string]interface{}{ map[string]interface{}{ @@ -233,7 +233,7 @@ func Test_flattenHealthCheck(t *testing.T) { } for _, tc := range cases { - output := flattenHealthCheck(tc.Input) + output := flattenHealthCheck(&tc.Input) if !reflect.DeepEqual(output, tc.Output) { t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) }