From d4d8a99ba4457f8c28b53256eeb0443f137a2ef6 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 14 Jun 2016 17:52:55 -0400 Subject: [PATCH] Check for LoadBalancerNotFound When reading an AppCookieStickinessPolicy check for LoadBalancerNotFound as well as PolicyNotFound. This prevents errors when when destroying a policy on an ELB that no longer exists. --- ...source_aws_app_cookie_stickiness_policy.go | 7 +-- ...e_aws_app_cookie_stickiness_policy_test.go | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go index 0fe85f9e9..2b25064c5 100644 --- a/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go +++ b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go @@ -98,9 +98,10 @@ func resourceAwsAppCookieStickinessPolicyRead(d *schema.ResourceData, meta inter 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_app_cookie_stickiness_policy_test.go b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go index f05952ec8..5b2bd3612 100644 --- a/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go +++ b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go @@ -15,6 +15,7 @@ import ( func TestAccAWSAppCookieStickinessPolicy_basic(t *testing.T) { lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5)) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -42,6 +43,42 @@ func TestAccAWSAppCookieStickinessPolicy_basic(t *testing.T) { }) } +func TestAccAWSAppCookieStickinessPolicy_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: testAccCheckAppCookieStickinessPolicyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAppCookieStickinessPolicyConfig(lbName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAppCookieStickinessPolicy( + "aws_elb.lb", + "aws_app_cookie_stickiness_policy.foo", + ), + ), + }, + resource.TestStep{ + PreConfig: removeLB, + Config: testAccAppCookieStickinessPolicyConfigDestroy(lbName), + }, + }, + }) +} + func testAccCheckAppCookieStickinessPolicyDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elbconn @@ -144,3 +181,18 @@ resource "aws_app_cookie_stickiness_policy" "foo" { cookie_name = "MyOtherAppCookie" }`, rName) } + +// attempt to destroy the policy, but we'll delete the LB in the PreConfig +func testAccAppCookieStickinessPolicyConfigDestroy(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) +}