Skip to content

Commit

Permalink
Merge pull request #10323 from terraform-providers/t-aws_waf_web_acl-…
Browse files Browse the repository at this point in the history
…sweeper

tests/resource/aws_waf_web_acl: Add sweeper
  • Loading branch information
bflad authored Oct 2, 2019
2 parents 4c80e66 + 5bc8d80 commit ce20280
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws/resource_aws_waf_rule_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func init() {
resource.AddTestSweepers("aws_waf_rule_group", &resource.Sweeper{
Name: "aws_waf_rule_group",
F: testSweepWafRuleGroups,
Dependencies: []string{
"aws_waf_web_acl",
},
})
}

Expand Down
101 changes: 101 additions & 0 deletions aws/resource_aws_waf_web_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"os"
"testing"

Expand All @@ -12,6 +13,106 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func init() {
resource.AddTestSweepers("aws_waf_web_acl", &resource.Sweeper{
Name: "aws_waf_web_acl",
F: testSweepWafWebAcls,
})
}

func testSweepWafWebAcls(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*AWSClient).wafconn

input := &waf.ListWebACLsInput{}

for {
output, err := conn.ListWebACLs(input)

if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping WAF Regional Web ACL sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing WAF Regional Web ACLs: %s", err)
}

for _, webACL := range output.WebACLs {
deleteInput := &waf.DeleteWebACLInput{
WebACLId: webACL.WebACLId,
}
id := aws.StringValue(webACL.WebACLId)
wr := newWafRetryer(conn)

_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
deleteInput.ChangeToken = token
log.Printf("[INFO] Deleting WAF Regional Web ACL: %s", id)
return conn.DeleteWebACL(deleteInput)
})

if isAWSErr(err, waf.ErrCodeNonEmptyEntityException, "") {
getWebACLInput := &waf.GetWebACLInput{
WebACLId: webACL.WebACLId,
}

getWebACLOutput, getWebACLErr := conn.GetWebACL(getWebACLInput)

if getWebACLErr != nil {
return fmt.Errorf("error getting WAF Regional Web ACL (%s): %s", id, getWebACLErr)
}

var updates []*waf.WebACLUpdate
updateWebACLInput := &waf.UpdateWebACLInput{
DefaultAction: getWebACLOutput.WebACL.DefaultAction,
Updates: updates,
WebACLId: webACL.WebACLId,
}

for _, rule := range getWebACLOutput.WebACL.Rules {
update := &waf.WebACLUpdate{
Action: aws.String(waf.ChangeActionDelete),
ActivatedRule: rule,
}

updateWebACLInput.Updates = append(updateWebACLInput.Updates, update)
}

_, updateWebACLErr := wr.RetryWithToken(func(token *string) (interface{}, error) {
updateWebACLInput.ChangeToken = token
log.Printf("[INFO] Removing Rules from WAF Regional Web ACL: %s", id)
return conn.UpdateWebACL(updateWebACLInput)
})

if updateWebACLErr != nil {
return fmt.Errorf("error removing rules from WAF Regional Web ACL (%s): %s", id, updateWebACLErr)
}

_, err = wr.RetryWithToken(func(token *string) (interface{}, error) {
deleteInput.ChangeToken = token
log.Printf("[INFO] Deleting WAF Regional Web ACL: %s", id)
return conn.DeleteWebACL(deleteInput)
})
}

if err != nil {
return fmt.Errorf("error deleting WAF Regional Web ACL (%s): %s", id, err)
}
}

if aws.StringValue(output.NextMarker) == "" {
break
}

input.NextMarker = output.NextMarker
}

return nil
}

func TestAccAWSWafWebAcl_basic(t *testing.T) {
var webACL waf.WebACL
rName := fmt.Sprintf("wafacl%s", acctest.RandString(5))
Expand Down

0 comments on commit ce20280

Please sign in to comment.