Merge pull request #1619 from jgjay/elb-listener-update

provider/aws: handle changes to elb listeners after creation
This commit is contained in:
Mitchell Hashimoto 2015-04-22 08:24:36 +02:00
commit 2cecd586c2
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"
}
}
`