From b0096abbe30d2fe75405dbfbfed1edb47e96dd96 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Fri, 27 Jan 2017 11:14:23 -0800 Subject: [PATCH] provider/aws: allow destroy of LB stickiness policy with missing LB (#11462) Previously an attempt to destroy a LB stickiness policy would result in an error like this: ``` * aws_lb_cookie_stickiness_policy.foo: Error removing LBCookieStickinessPolicy: LoadBalancerNotFound: There is no ACTIVE Load Balancer named 'tf-test-lb-tqatd' status code: 400, request id: 28af1167-e4a4-11e6-8ddd-57ba410cbbb6 ``` This checks for a missing load balancer on the policy read and allows the destroy. ``` $ make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSLBCookieStickinessPolicy_missingLB' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/01/27 07:21:11 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSLBCookieStickinessPolicy_missingLB -timeout 120m === RUN TestAccAWSLBCookieStickinessPolicy_missingLB --- PASS: TestAccAWSLBCookieStickinessPolicy_missingLB (28.90s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 28.929s ``` --- ...esource_aws_lb_cookie_stickiness_policy.go | 7 +-- ...ce_aws_lb_cookie_stickiness_policy_test.go | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go index 6ef5042bf4..026f981426 100644 --- a/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go +++ b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go @@ -103,9 +103,10 @@ func resourceAwsLBCookieStickinessPolicyRead(d *schema.ResourceData, meta interf getResp, err := elbconn.DescribeLoadBalancerPolicies(request) if err != nil { - if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "PolicyNotFound" { - // The policy is gone. - d.SetId("") + if ec2err, ok := err.(awserr.Error); ok { + if ec2err.Code() == "PolicyNotFound" || ec2err.Code() == "LoadBalancerNotFound" { + d.SetId("") + } return nil } return fmt.Errorf("Error retrieving policy: %s", err) diff --git a/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go index 877e49b768..a57660ac58 100644 --- a/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go +++ b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go @@ -149,6 +149,42 @@ func TestAccCheckLBCookieStickinessPolicy_drift(t *testing.T) { }) } +func TestAccAWSLBCookieStickinessPolicy_missingLB(t *testing.T) { + lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5)) + + // check that we can destroy the policy if the LB is missing + removeLB := func() { + conn := testAccProvider.Meta().(*AWSClient).elbconn + deleteElbOpts := elb.DeleteLoadBalancerInput{ + LoadBalancerName: aws.String(lbName), + } + if _, err := conn.DeleteLoadBalancer(&deleteElbOpts); err != nil { + t.Fatalf("Error deleting ELB: %s", err) + } + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccLBCookieStickinessPolicyConfig(lbName), + Check: resource.ComposeTestCheckFunc( + testAccCheckLBCookieStickinessPolicy( + "aws_elb.lb", + "aws_lb_cookie_stickiness_policy.foo", + ), + ), + }, + resource.TestStep{ + PreConfig: removeLB, + Config: testAccLBCookieStickinessPolicyConfigDestroy(lbName), + }, + }, + }) +} + func testAccLBCookieStickinessPolicyConfig(rName string) string { return fmt.Sprintf(` resource "aws_elb" "lb" { @@ -190,3 +226,18 @@ resource "aws_lb_cookie_stickiness_policy" "foo" { cookie_expiration_period = 300 }`, rName) } + +// attempt to destroy the policy, but we'll delete the LB in the PreConfig +func testAccLBCookieStickinessPolicyConfigDestroy(rName string) string { + return fmt.Sprintf(` +resource "aws_elb" "lb" { + name = "%s" + availability_zones = ["us-west-2a"] + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +}`, rName) +}