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.
This commit is contained in:
James Bardin 2016-06-14 17:52:55 -04:00
parent 4796e27d1a
commit d4d8a99ba4
2 changed files with 56 additions and 3 deletions

View File

@ -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)

View File

@ -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)
}