Skip to content

Commit

Permalink
Merge pull request #10216 from terraform-providers/b-aws_wafregional_…
Browse files Browse the repository at this point in the history
…web_acl_association-disappears

resource/aws_wafregional_web_acl_association: Ensure missing resource triggers state removal
  • Loading branch information
bflad authored Sep 30, 2019
2 parents 3e61dea + 9e938e5 commit bcc476b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aws/resource_aws_wafregional_web_acl_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func resourceAwsWafRegionalWebAclAssociationRead(d *schema.ResourceData, meta in
}

if output == nil || output.WebACLSummary == nil {
return fmt.Errorf("error getting WAF Regional Web ACL for resource (%s): empty response", resourceArn)
log.Printf("[WARN] WAF Regional Web ACL for resource (%s) not found, removing from state", resourceArn)
d.SetId("")
return nil
}

d.Set("resource_arn", resourceArn)
Expand Down
45 changes: 45 additions & 0 deletions aws/resource_aws_wafregional_web_acl_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ func TestAccAWSWafRegionalWebAclAssociation_basic(t *testing.T) {
})
}

func TestAccAWSWafRegionalWebAclAssociation_disappears(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckWafRegionalWebAclAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckWafRegionalWebAclAssociationConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckWafRegionalWebAclAssociationExists("aws_wafregional_web_acl_association.foo"),
testAccCheckWafRegionalWebAclAssociationDisappears("aws_wafregional_web_acl_association.foo"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSWafRegionalWebAclAssociation_multipleAssociations(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -119,6 +137,33 @@ func testAccCheckWafRegionalWebAclAssociationExists(n string) resource.TestCheck
}
}

func testAccCheckWafRegionalWebAclAssociationDisappears(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// waf.WebACLSummary does not contain the information so we instead just use the state information

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No WebACL association ID is set")
}

_, resourceArn := resourceAwsWafRegionalWebAclAssociationParseId(rs.Primary.ID)

conn := testAccProvider.Meta().(*AWSClient).wafregionalconn

input := &wafregional.DisassociateWebACLInput{
ResourceArn: aws.String(resourceArn),
}

_, err := conn.DisassociateWebACL(input)

return err
}
}

const testAccCheckWafRegionalWebAclAssociationConfig_basic = `
resource "aws_wafregional_rule" "foo" {
name = "foo"
Expand Down

0 comments on commit bcc476b

Please sign in to comment.