From 81079bb6ac4b3fd835377f40316feb64958d1fe8 Mon Sep 17 00:00:00 2001 From: Gavin James Date: Tue, 21 Apr 2015 23:17:17 +0100 Subject: [PATCH] handle addition/update/removal of listeners after creation --- builtin/providers/aws/resource_aws_elb.go | 40 +++++++++++++++++ .../providers/aws/resource_aws_elb_test.go | 44 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index b36e752e9..38cb9bfec 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -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 diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index f0048dd20..65e1d1d57 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -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" + } +} +`