provider/aws: Convert AWS ELB to aws-sdk-go

This commit is contained in:
Clint Shryock 2015-03-02 09:44:06 -06:00
parent 1f7e71b7f6
commit da55f9bf85
5 changed files with 133 additions and 111 deletions

View File

@ -9,10 +9,10 @@ import (
"github.com/hashicorp/terraform/helper/multierror" "github.com/hashicorp/terraform/helper/multierror"
"github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/goamz/elb"
awsGo "github.com/hashicorp/aws-sdk-go/aws" awsGo "github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/autoscaling" "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/rds"
"github.com/hashicorp/aws-sdk-go/gen/route53" "github.com/hashicorp/aws-sdk-go/gen/route53"
"github.com/hashicorp/aws-sdk-go/gen/s3" "github.com/hashicorp/aws-sdk-go/gen/s3"
@ -63,7 +63,7 @@ func (c *Config) Client() (interface{}, error) {
log.Println("[INFO] Initializing EC2 connection") log.Println("[INFO] Initializing EC2 connection")
client.ec2conn = ec2.New(auth, region) client.ec2conn = ec2.New(auth, region)
log.Println("[INFO] Initializing ELB connection") 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") log.Println("[INFO] Initializing AutoScaling connection")
client.autoscalingconn = autoscaling.New(creds, c.Region, nil) client.autoscalingconn = autoscaling.New(creds, c.Region, nil)
log.Println("[INFO] Initializing S3 connection") log.Println("[INFO] Initializing S3 connection")

View File

@ -5,9 +5,10 @@ import (
"fmt" "fmt"
"log" "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/hashcode"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/elb"
) )
func resourceAwsElb() *schema.Resource { func resourceAwsElb() *schema.Resource {
@ -167,14 +168,18 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
} }
// Provision the elb // Provision the elb
elbOpts := &elb.CreateLoadBalancer{
LoadBalancerName: d.Get("name").(string), elbOpts := &elb.CreateAccessPointInput{
LoadBalancerName: aws.String(d.Get("name").(string)),
Listeners: listeners, 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 { 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 { if v, ok := d.GetOk("security_groups"); ok {
@ -208,14 +213,14 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
if len(vs) > 0 { if len(vs) > 0 {
check := vs[0].(map[string]interface{}) check := vs[0].(map[string]interface{})
configureHealthCheckOpts := elb.ConfigureHealthCheck{ configureHealthCheckOpts := elb.ConfigureHealthCheckInput{
LoadBalancerName: d.Id(), LoadBalancerName: aws.String(d.Id()),
Check: elb.HealthCheck{ HealthCheck: &elb.HealthCheck{
HealthyThreshold: int64(check["healthy_threshold"].(int)), HealthyThreshold: aws.Integer(check["healthy_threshold"].(int)),
UnhealthyThreshold: int64(check["unhealthy_threshold"].(int)), UnhealthyThreshold: aws.Integer(check["unhealthy_threshold"].(int)),
Interval: int64(check["interval"].(int)), Interval: aws.Integer(check["interval"].(int)),
Target: check["target"].(string), Target: aws.String(check["target"].(string)),
Timeout: int64(check["timeout"].(int)), Timeout: aws.Integer(check["timeout"].(int)),
}, },
} }
@ -233,13 +238,13 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn elbconn := meta.(*AWSClient).elbconn
// Retrieve the ELB properties for updating the state // Retrieve the ELB properties for updating the state
describeElbOpts := &elb.DescribeLoadBalancer{ describeElbOpts := &elb.DescribeAccessPointsInput{
Names: []string{d.Id()}, LoadBalancerNames: []string{d.Id()},
} }
describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts) describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
if err != nil { 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 // The ELB is gone now, so just remove it from the state
d.SetId("") d.SetId("")
return nil return nil
@ -247,24 +252,24 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error retrieving ELB: %s", err) return fmt.Errorf("Error retrieving ELB: %s", err)
} }
if len(describeResp.LoadBalancers) != 1 { if len(describeResp.LoadBalancerDescriptions) != 1 {
return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancers) 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("name", *lb.LoadBalancerName)
d.Set("dns_name", lb.DNSName) d.Set("dns_name", *lb.DNSName)
d.Set("internal", lb.Scheme == "internal") d.Set("internal", *lb.Scheme == "internal")
d.Set("availability_zones", lb.AvailabilityZones) d.Set("availability_zones", lb.AvailabilityZones)
d.Set("instances", flattenInstances(lb.Instances)) 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("security_groups", lb.SecurityGroups)
d.Set("subnets", lb.Subnets) d.Set("subnets", lb.Subnets)
// There's only one health check, so save that to state as we // There's only one health check, so save that to state as we
// currently can // currently can
if lb.HealthCheck.Target != "" { if *lb.HealthCheck.Target != "" {
d.Set("health_check", flattenHealthCheck(lb.HealthCheck)) d.Set("health_check", flattenHealthCheck(lb.HealthCheck))
} }
@ -283,12 +288,12 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
o, n := d.GetChange("instances") o, n := d.GetChange("instances")
os := o.(*schema.Set) os := o.(*schema.Set)
ns := n.(*schema.Set) ns := n.(*schema.Set)
remove := expandStringList(os.Difference(ns).List()) remove := expandInstanceString(os.Difference(ns).List())
add := expandStringList(ns.Difference(os).List()) add := expandInstanceString(ns.Difference(os).List())
if len(add) > 0 { if len(add) > 0 {
registerInstancesOpts := elb.RegisterInstancesWithLoadBalancer{ registerInstancesOpts := elb.RegisterEndPointsInput{
LoadBalancerName: d.Id(), LoadBalancerName: aws.String(d.Id()),
Instances: add, Instances: add,
} }
@ -298,8 +303,8 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
} }
} }
if len(remove) > 0 { if len(remove) > 0 {
deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancer{ deRegisterInstancesOpts := elb.DeregisterEndPointsInput{
LoadBalancerName: d.Id(), LoadBalancerName: aws.String(d.Id()),
Instances: remove, Instances: remove,
} }
@ -315,10 +320,12 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] outside modify attributes") log.Println("[INFO] outside modify attributes")
if d.HasChange("cross_zone_load_balancing") { if d.HasChange("cross_zone_load_balancing") {
log.Println("[INFO] inside modify attributes") log.Println("[INFO] inside modify attributes")
attrs := elb.ModifyLoadBalancerAttributes{ attrs := elb.ModifyLoadBalancerAttributesInput{
LoadBalancerName: d.Get("name").(string), LoadBalancerName: aws.String(d.Get("name").(string)),
LoadBalancerAttributes: elb.LoadBalancerAttributes{ LoadBalancerAttributes: &elb.LoadBalancerAttributes{
CrossZoneLoadBalancingEnabled: d.Get("cross_zone_load_balancing").(bool), CrossZoneLoadBalancing: &elb.CrossZoneLoadBalancing{
aws.Boolean(d.Get("cross_zone_load_balancing").(bool)),
},
}, },
} }
_, err := elbconn.ModifyLoadBalancerAttributes(&attrs) _, err := elbconn.ModifyLoadBalancerAttributes(&attrs)
@ -332,14 +339,14 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
vs := d.Get("health_check").(*schema.Set).List() vs := d.Get("health_check").(*schema.Set).List()
if len(vs) > 0 { if len(vs) > 0 {
check := vs[0].(map[string]interface{}) check := vs[0].(map[string]interface{})
configureHealthCheckOpts := elb.ConfigureHealthCheck{ configureHealthCheckOpts := elb.ConfigureHealthCheckInput{
LoadBalancerName: d.Id(), LoadBalancerName: aws.String(d.Id()),
Check: elb.HealthCheck{ HealthCheck: &elb.HealthCheck{
HealthyThreshold: int64(check["healthy_threshold"].(int)), HealthyThreshold: aws.Integer(check["healthy_threshold"].(int)),
UnhealthyThreshold: int64(check["unhealthy_threshold"].(int)), UnhealthyThreshold: aws.Integer(check["unhealthy_threshold"].(int)),
Interval: int64(check["interval"].(int)), Interval: aws.Integer(check["interval"].(int)),
Target: check["target"].(string), Target: aws.String(check["target"].(string)),
Timeout: int64(check["timeout"].(int)), Timeout: aws.Integer(check["timeout"].(int)),
}, },
} }
_, err := elbconn.ConfigureHealthCheck(&configureHealthCheckOpts) _, err := elbconn.ConfigureHealthCheck(&configureHealthCheckOpts)
@ -351,6 +358,7 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
} }
d.Partial(false) d.Partial(false)
return resourceAwsElbRead(d, meta) return resourceAwsElbRead(d, meta)
} }
@ -360,8 +368,8 @@ func resourceAwsElbDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Deleting ELB: %s", d.Id()) log.Printf("[INFO] Deleting ELB: %s", d.Id())
// Destroy the load balancer // Destroy the load balancer
deleteElbOpts := elb.DeleteLoadBalancer{ deleteElbOpts := elb.DeleteAccessPointInput{
LoadBalancerName: d.Id(), LoadBalancerName: aws.String(d.Id()),
} }
if _, err := elbconn.DeleteLoadBalancer(&deleteElbOpts); err != nil { if _, err := elbconn.DeleteLoadBalancer(&deleteElbOpts); err != nil {
return fmt.Errorf("Error deleting ELB: %s", err) return fmt.Errorf("Error deleting ELB: %s", err)

View File

@ -7,13 +7,14 @@ import (
"sort" "sort"
"testing" "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/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/elb"
) )
func TestAccAWSELB_basic(t *testing.T) { func TestAccAWSELB_basic(t *testing.T) {
var conf elb.LoadBalancer var conf elb.LoadBalancerDescription
ssl_certificate_id := os.Getenv("AWS_SSL_CERTIFICATE_ID") ssl_certificate_id := os.Getenv("AWS_SSL_CERTIFICATE_ID")
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -53,7 +54,7 @@ func TestAccAWSELB_basic(t *testing.T) {
} }
func TestAccAWSELB_InstanceAttaching(t *testing.T) { func TestAccAWSELB_InstanceAttaching(t *testing.T) {
var conf elb.LoadBalancer var conf elb.LoadBalancerDescription
testCheckInstanceAttached := func(count int) resource.TestCheckFunc { testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
return func(*terraform.State) error { return func(*terraform.State) error {
@ -89,7 +90,7 @@ func TestAccAWSELB_InstanceAttaching(t *testing.T) {
} }
func TestAccAWSELB_HealthCheck(t *testing.T) { func TestAccAWSELB_HealthCheck(t *testing.T) {
var conf elb.LoadBalancer var conf elb.LoadBalancerDescription
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -149,19 +150,19 @@ func testAccCheckAWSELBDestroy(s *terraform.State) error {
continue continue
} }
describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ describe, err := conn.DescribeLoadBalancers(&elb.DescribeAccessPointsInput{
Names: []string{rs.Primary.ID}, LoadBalancerNames: []string{rs.Primary.ID},
}) })
if err == nil { if err == nil {
if len(describe.LoadBalancers) != 0 && if len(describe.LoadBalancerDescriptions) != 0 &&
describe.LoadBalancers[0].LoadBalancerName == rs.Primary.ID { *describe.LoadBalancerDescriptions[0].LoadBalancerName == rs.Primary.ID {
return fmt.Errorf("ELB still exists") return fmt.Errorf("ELB still exists")
} }
} }
// Verify the error // Verify the error
providerErr, ok := err.(*elb.Error) providerErr, ok := err.(aws.APIError)
if !ok { if !ok {
return err return err
} }
@ -174,7 +175,7 @@ func testAccCheckAWSELBDestroy(s *terraform.State) error {
return nil return nil
} }
func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc { func testAccCheckAWSELBAttributes(conf *elb.LoadBalancerDescription) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
sort.StringSlice(conf.AvailabilityZones).Sort() sort.StringSlice(conf.AvailabilityZones).Sort()
@ -182,25 +183,25 @@ func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc
return fmt.Errorf("bad availability_zones") return fmt.Errorf("bad availability_zones")
} }
if conf.LoadBalancerName != "foobar-terraform-test" { if *conf.LoadBalancerName != "foobar-terraform-test" {
return fmt.Errorf("bad name") return fmt.Errorf("bad name")
} }
l := elb.Listener{ l := elb.Listener{
InstancePort: 8000, InstancePort: aws.Integer(8000),
InstanceProtocol: "HTTP", InstanceProtocol: aws.String("HTTP"),
LoadBalancerPort: 80, LoadBalancerPort: aws.Integer(80),
Protocol: "HTTP", Protocol: aws.String("HTTP"),
} }
if !reflect.DeepEqual(conf.Listeners[0], l) { if !reflect.DeepEqual(conf.ListenerDescriptions[0].Listener, &l) {
return fmt.Errorf( return fmt.Errorf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n", "Got:\n\n%#v\n\nExpected:\n\n%#v\n",
conf.Listeners[0], conf.ListenerDescriptions[0].Listener,
l) l)
} }
if conf.DNSName == "" { if *conf.DNSName == "" {
return fmt.Errorf("empty dns_name") 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 { return func(s *terraform.State) error {
zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"}
sort.StringSlice(conf.AvailabilityZones).Sort() sort.StringSlice(conf.AvailabilityZones).Sort()
@ -216,26 +217,26 @@ func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.Te
return fmt.Errorf("bad availability_zones") return fmt.Errorf("bad availability_zones")
} }
if conf.LoadBalancerName != "foobar-terraform-test" { if *conf.LoadBalancerName != "foobar-terraform-test" {
return fmt.Errorf("bad name") return fmt.Errorf("bad name")
} }
check := elb.HealthCheck{ check := elb.HealthCheck{
Timeout: 30, Timeout: aws.Integer(30),
UnhealthyThreshold: 5, UnhealthyThreshold: aws.Integer(5),
HealthyThreshold: 5, HealthyThreshold: aws.Integer(5),
Interval: 60, Interval: aws.Integer(60),
Target: "HTTP:8000/", Target: aws.String("HTTP:8000/"),
} }
if !reflect.DeepEqual(conf.HealthCheck, check) { if !reflect.DeepEqual(conf.HealthCheck, &check) {
return fmt.Errorf( return fmt.Errorf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n", "Got:\n\n%#v\n\nExpected:\n\n%#v\n",
conf.HealthCheck, conf.HealthCheck,
check) check)
} }
if conf.DNSName == "" { if *conf.DNSName == "" {
return fmt.Errorf("empty dns_name") 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 { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
@ -256,20 +257,20 @@ func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestChec
conn := testAccProvider.Meta().(*AWSClient).elbconn conn := testAccProvider.Meta().(*AWSClient).elbconn
describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ describe, err := conn.DescribeLoadBalancers(&elb.DescribeAccessPointsInput{
Names: []string{rs.Primary.ID}, LoadBalancerNames: []string{rs.Primary.ID},
}) })
if err != nil { if err != nil {
return err return err
} }
if len(describe.LoadBalancers) != 1 || if len(describe.LoadBalancerDescriptions) != 1 ||
describe.LoadBalancers[0].LoadBalancerName != rs.Primary.ID { *describe.LoadBalancerDescriptions[0].LoadBalancerName != rs.Primary.ID {
return fmt.Errorf("ELB not found") return fmt.Errorf("ELB not found")
} }
*res = describe.LoadBalancers[0] *res = describe.LoadBalancerDescriptions[0]
return nil return nil
} }

View File

@ -4,10 +4,10 @@ import (
"strings" "strings"
"github.com/hashicorp/aws-sdk-go/aws" "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/aws-sdk-go/gen/rds"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/goamz/elb"
) )
// Takes the result of flatmap.Expand for an array of listeners and // 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{}) data := lRaw.(map[string]interface{})
l := elb.Listener{ l := elb.Listener{
InstancePort: int64(data["instance_port"].(int)), InstancePort: aws.Integer(data["instance_port"].(int)),
InstanceProtocol: data["instance_protocol"].(string), InstanceProtocol: aws.String(data["instance_protocol"].(string)),
LoadBalancerPort: int64(data["lb_port"].(int)), LoadBalancerPort: aws.Integer(data["lb_port"].(int)),
Protocol: data["lb_protocol"].(string), Protocol: aws.String(data["lb_protocol"].(string)),
} }
if v, ok := data["ssl_certificate_id"]; ok { if v, ok := data["ssl_certificate_id"]; ok {
l.SSLCertificateId = v.(string) l.SSLCertificateID = aws.String(v.(string))
} }
listeners = append(listeners, l) 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() // Flattens a health check into something that flatmap.Flatten()
// can handle // 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) result := make([]map[string]interface{}, 0, 1)
chk := make(map[string]interface{}) chk := make(map[string]interface{})
chk["unhealthy_threshold"] = int(check.UnhealthyThreshold) chk["unhealthy_threshold"] = *check.UnhealthyThreshold
chk["healthy_threshold"] = int(check.HealthyThreshold) chk["healthy_threshold"] = *check.HealthyThreshold
chk["target"] = check.Target chk["target"] = *check.Target
chk["timeout"] = int(check.Timeout) chk["timeout"] = *check.Timeout
chk["interval"] = int(check.Interval) chk["interval"] = *check.Interval
result = append(result, chk) result = append(result, chk)
@ -166,22 +166,35 @@ func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string {
func flattenInstances(list []elb.Instance) []string { func flattenInstances(list []elb.Instance) []string {
result := make([]string, 0, len(list)) result := make([]string, 0, len(list))
for _, i := range 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 return result
} }
// Flattens an array of Listeners into a []map[string]interface{} // 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)) result := make([]map[string]interface{}, 0, len(list))
for _, i := range list { for _, i := range list {
result = append(result, map[string]interface{}{ l := map[string]interface{}{
"instance_port": i.InstancePort, "instance_port": *i.Listener.InstancePort,
"instance_protocol": strings.ToLower(i.InstanceProtocol), "instance_protocol": strings.ToLower(*i.Listener.InstanceProtocol),
"ssl_certificate_id": i.SSLCertificateId, "lb_port": *i.Listener.LoadBalancerPort,
"lb_port": i.LoadBalancerPort, "lb_protocol": strings.ToLower(*i.Listener.Protocol),
"lb_protocol": strings.ToLower(i.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 return result
} }

View File

@ -5,12 +5,12 @@ import (
"testing" "testing"
"github.com/hashicorp/aws-sdk-go/aws" "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/aws-sdk-go/gen/rds"
"github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/goamz/elb"
) )
// Returns test configuration // Returns test configuration
@ -192,10 +192,10 @@ func Test_expandListeners(t *testing.T) {
} }
expected := elb.Listener{ expected := elb.Listener{
InstancePort: 8000, InstancePort: aws.Integer(8000),
LoadBalancerPort: 80, LoadBalancerPort: aws.Integer(80),
InstanceProtocol: "http", InstanceProtocol: aws.String("http"),
Protocol: "http", Protocol: aws.String("http"),
} }
if !reflect.DeepEqual(listeners[0], expected) { if !reflect.DeepEqual(listeners[0], expected) {
@ -214,11 +214,11 @@ func Test_flattenHealthCheck(t *testing.T) {
}{ }{
{ {
Input: elb.HealthCheck{ Input: elb.HealthCheck{
UnhealthyThreshold: 10, UnhealthyThreshold: aws.Integer(10),
HealthyThreshold: 10, HealthyThreshold: aws.Integer(10),
Target: "HTTP:80/", Target: aws.String("HTTP:80/"),
Timeout: 30, Timeout: aws.Integer(30),
Interval: 30, Interval: aws.Integer(30),
}, },
Output: []map[string]interface{}{ Output: []map[string]interface{}{
map[string]interface{}{ map[string]interface{}{
@ -233,7 +233,7 @@ func Test_flattenHealthCheck(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
output := flattenHealthCheck(tc.Input) output := flattenHealthCheck(&tc.Input)
if !reflect.DeepEqual(output, tc.Output) { if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output) t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
} }