handle addition/update/removal of listeners after creation

This commit is contained in:
Gavin James 2015-04-21 23:17:17 +01:00
parent 1c0f2f136c
commit 81079bb6ac
2 changed files with 84 additions and 0 deletions

View File

@ -295,6 +295,46 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(true)
if d.HasChange("listener") {
o, n := d.GetChange("listener")
os := o.(*schema.Set)
ns := n.(*schema.Set)
remove, _ := expandListeners(os.Difference(ns).List())
add, _ := expandListeners(ns.Difference(os).List())
if len(remove) > 0 {
ports := make([]*int64, 0, len(remove))
for _, listener := range remove {
ports = append(ports, listener.LoadBalancerPort)
}
deleteListenersOpts := &elb.DeleteLoadBalancerListenersInput{
LoadBalancerName: aws.String(d.Id()),
LoadBalancerPorts: ports,
}
_, err := elbconn.DeleteLoadBalancerListeners(deleteListenersOpts)
if err != nil {
return fmt.Errorf("Failure removing outdated listeners: %s", err)
}
}
if len(add) > 0 {
createListenersOpts := &elb.CreateLoadBalancerListenersInput{
LoadBalancerName: aws.String(d.Id()),
Listeners: add,
}
_, err := elbconn.CreateLoadBalancerListeners(createListenersOpts)
if err != nil {
return fmt.Errorf("Failure adding new or updated listeners: %s", err)
}
}
d.SetPartial("listener")
}
// If we currently have instances, or did have instances,
// we want to figure out what to add and remove from the load
// balancer

View File

@ -144,6 +144,36 @@ func TestAccAWSELB_InstanceAttaching(t *testing.T) {
})
}
func TestAccAWSELBUpdate_Listener(t *testing.T) {
var conf elb.LoadBalancerDescription
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSELBDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSELBConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSELBExists("aws_elb.bar", &conf),
testAccCheckAWSELBAttributes(&conf),
resource.TestCheckResourceAttr(
"aws_elb.bar", "listener.206423021.instance_port", "8000"),
),
},
resource.TestStep{
Config: testAccAWSELBConfigListener_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSELBExists("aws_elb.bar", &conf),
resource.TestCheckResourceAttr(
"aws_elb.bar", "listener.3931999347.instance_port", "8080"),
),
},
},
})
}
func TestAccAWSELB_HealthCheck(t *testing.T) {
var conf elb.LoadBalancerDescription
@ -460,3 +490,17 @@ resource "aws_elb" "bar" {
}
}
`
const testAccAWSELBConfigListener_update = `
resource "aws_elb" "bar" {
name = "foobar-terraform-test"
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
listener {
instance_port = 8080
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
`