From 1b6faa0eb9379cb3474a57c92e866644accbce69 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Wed, 2 Jul 2014 15:55:28 -0700 Subject: [PATCH] providers/aws: helper for expanding listeners --- builtin/providers/aws/resource_aws_elb.go | 18 +++++------ builtin/providers/aws/structure.go | 28 ++++++++++++++++ builtin/providers/aws/structure_test.go | 39 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 builtin/providers/aws/structure.go create mode 100644 builtin/providers/aws/structure_test.go diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index d82865fd7..3128f8a76 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -4,6 +4,7 @@ import ( "fmt" "log" + "github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/helper/diff" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/goamz/elb" @@ -24,20 +25,15 @@ func resource_aws_elb_create( // we save to state if the creation is succesful (amazon verifies // it is unique) elbName := rs.Attributes["name"] - // v := flatmap.Expand(rs.Attributes, "listener") - // log.Println(v) + + // Expand the "listener" array to goamz compat []elb.Listener + v := flatmap.Expand(rs.Attributes, "listener").([]interface{}) + listeners := expandListeners(v) // Provision the elb elbOpts := &elb.CreateLoadBalancer{ LoadBalancerName: elbName, - Listeners: []elb.Listener{ - elb.Listener{ - InstancePort: 8000, - InstanceProtocol: "http", - LoadBalancerPort: 80, - Protocol: "http", - }, - }, + Listeners: listeners, AvailZone: []string{ "us-east-1a", "us-east-1b", @@ -102,10 +98,12 @@ func resource_aws_elb_diff( Attrs: map[string]diff.AttrType{ "name": diff.AttrTypeCreate, "availability_zone": diff.AttrTypeCreate, + "listener": diff.AttrTypeCreate, }, ComputedAttrs: []string{ "dns_name", + "instances", }, } diff --git a/builtin/providers/aws/structure.go b/builtin/providers/aws/structure.go new file mode 100644 index 000000000..44ab80519 --- /dev/null +++ b/builtin/providers/aws/structure.go @@ -0,0 +1,28 @@ +package aws + +import ( + "github.com/mitchellh/goamz/elb" +) + +// Takes the result of flatmap.Expand for an array of listeners and +// returns ELB API compatible objects +func expandListeners(configured []interface{}) []elb.Listener { + listeners := make([]elb.Listener, 0, len(configured)) + + // Loop over our configured listeners and create + // an array of goamz compatabile objects + for _, listener := range configured { + newL := listener.(map[string]interface{}) + + l := elb.Listener{ + InstancePort: int64(newL["instance_port"].(int)), + InstanceProtocol: newL["instance_protocol"].(string), + LoadBalancerPort: int64(newL["lb_port"].(int)), + Protocol: newL["lb_protocol"].(string), + } + + listeners = append(listeners, l) + } + + return listeners +} diff --git a/builtin/providers/aws/structure_test.go b/builtin/providers/aws/structure_test.go new file mode 100644 index 000000000..28283a676 --- /dev/null +++ b/builtin/providers/aws/structure_test.go @@ -0,0 +1,39 @@ +package aws + +import ( + "reflect" + "testing" + + "github.com/hashicorp/terraform/flatmap" + "github.com/mitchellh/goamz/elb" +) + +// Returns test configuration +func testConf() map[string]string { + return map[string]string{ + "listener.#": "1", + "listener.0.lb_port": "80", + "listener.0.lb_protocol": "http", + "listener.0.instance_port": "8000", + "listener.0.instance_protocol": "http", + } +} + +func Test_expandListeners(t *testing.T) { + expanded := flatmap.Expand(testConf(), "listener").([]interface{}) + listeners := expandListeners(expanded) + expected := elb.Listener{ + InstancePort: 8000, + LoadBalancerPort: 80, + InstanceProtocol: "http", + Protocol: "http", + } + + if !reflect.DeepEqual(listeners[0], expected) { + t.Fatalf( + "Got:\n\n%#v\n\nExpected:\n\n%#v\n", + listeners[0], + expected) + } + +}