providers/aws: helper for expanding listeners

This commit is contained in:
Jack Pearkes 2014-07-02 15:55:28 -07:00
parent 339355b2f1
commit 1b6faa0eb9
3 changed files with 75 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/hashicorp/terraform/flatmap"
"github.com/hashicorp/terraform/helper/diff" "github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/elb" "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 // we save to state if the creation is succesful (amazon verifies
// it is unique) // it is unique)
elbName := rs.Attributes["name"] 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 // Provision the elb
elbOpts := &elb.CreateLoadBalancer{ elbOpts := &elb.CreateLoadBalancer{
LoadBalancerName: elbName, LoadBalancerName: elbName,
Listeners: []elb.Listener{ Listeners: listeners,
elb.Listener{
InstancePort: 8000,
InstanceProtocol: "http",
LoadBalancerPort: 80,
Protocol: "http",
},
},
AvailZone: []string{ AvailZone: []string{
"us-east-1a", "us-east-1a",
"us-east-1b", "us-east-1b",
@ -102,10 +98,12 @@ func resource_aws_elb_diff(
Attrs: map[string]diff.AttrType{ Attrs: map[string]diff.AttrType{
"name": diff.AttrTypeCreate, "name": diff.AttrTypeCreate,
"availability_zone": diff.AttrTypeCreate, "availability_zone": diff.AttrTypeCreate,
"listener": diff.AttrTypeCreate,
}, },
ComputedAttrs: []string{ ComputedAttrs: []string{
"dns_name", "dns_name",
"instances",
}, },
} }

View File

@ -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
}

View File

@ -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)
}
}