Skip to content

Commit

Permalink
Add import support for aws_waf_regex_pattern_set resource (#10482)
Browse files Browse the repository at this point in the history
Output from acceptance testing:

```
--- PASS: TestAccAWSWafRegexPatternSet (56.62s)
```
  • Loading branch information
DrFaust92 authored and bflad committed Nov 15, 2019
1 parent c10f705 commit e4864de
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
20 changes: 17 additions & 3 deletions aws/resource_aws_waf_regex_pattern_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand All @@ -15,13 +16,20 @@ func resourceAwsWafRegexPatternSet() *schema.Resource {
Read: resourceAwsWafRegexPatternSetRead,
Update: resourceAwsWafRegexPatternSetUpdate,
Delete: resourceAwsWafRegexPatternSetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"regex_pattern_strings": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -63,9 +71,7 @@ func resourceAwsWafRegexPatternSetRead(d *schema.ResourceData, meta interface{})

resp, err := conn.GetRegexPatternSet(params)
if err != nil {
// TODO: Replace with a constant once available
// See https://github.com/aws/aws-sdk-go/issues/1856
if isAWSErr(err, "WAFNonexistentItemException", "") {
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
log.Printf("[WARN] WAF Regex Pattern Set (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Expand All @@ -77,6 +83,14 @@ func resourceAwsWafRegexPatternSetRead(d *schema.ResourceData, meta interface{})
d.Set("name", resp.RegexPatternSet.Name)
d.Set("regex_pattern_strings", aws.StringValueSlice(resp.RegexPatternSet.RegexPatternStrings))

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "waf",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("regexpatternset/%s", d.Id()),
}
d.Set("arn", arn.String())

return nil
}

Expand Down
63 changes: 42 additions & 21 deletions aws/resource_aws_waf_regex_pattern_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -32,6 +33,7 @@ func TestAccAWSWafRegexPatternSet(t *testing.T) {
func testAccAWSWafRegexPatternSet_basic(t *testing.T) {
var v waf.RegexPatternSet
patternSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_pattern_set.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -41,20 +43,27 @@ func testAccAWSWafRegexPatternSet_basic(t *testing.T) {
{
Config: testAccAWSWafRegexPatternSetConfig(patternSetName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegexPatternSetExists("aws_waf_regex_pattern_set.test", &v),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "name", patternSetName),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.#", "2"),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.2848565413", "one"),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.3351840846", "two"),
testAccCheckAWSWafRegexPatternSetExists(resourceName, &v),
testAccMatchResourceAttrGlobalARN(resourceName, "arn", "waf", regexp.MustCompile(`regexpatternset/.+`)),
resource.TestCheckResourceAttr(resourceName, "name", patternSetName),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.#", "2"),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.2848565413", "one"),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.3351840846", "two"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSWafRegexPatternSet_changePatterns(t *testing.T) {
var before, after waf.RegexPatternSet
patternSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_pattern_set.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -64,31 +73,37 @@ func testAccAWSWafRegexPatternSet_changePatterns(t *testing.T) {
{
Config: testAccAWSWafRegexPatternSetConfig(patternSetName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegexPatternSetExists("aws_waf_regex_pattern_set.test", &before),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "name", patternSetName),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.#", "2"),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.2848565413", "one"),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.3351840846", "two"),
testAccCheckAWSWafRegexPatternSetExists(resourceName, &before),
resource.TestCheckResourceAttr(resourceName, "name", patternSetName),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.#", "2"),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.2848565413", "one"),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.3351840846", "two"),
),
},
{
Config: testAccAWSWafRegexPatternSetConfig_changePatterns(patternSetName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegexPatternSetExists("aws_waf_regex_pattern_set.test", &after),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "name", patternSetName),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.#", "3"),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.3351840846", "two"),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.2929247714", "three"),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.1294846542", "four"),
testAccCheckAWSWafRegexPatternSetExists(resourceName, &after),
resource.TestCheckResourceAttr(resourceName, "name", patternSetName),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.#", "3"),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.3351840846", "two"),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.2929247714", "three"),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.1294846542", "four"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSWafRegexPatternSet_noPatterns(t *testing.T) {
var patternSet waf.RegexPatternSet
patternSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_pattern_set.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -98,18 +113,24 @@ func testAccAWSWafRegexPatternSet_noPatterns(t *testing.T) {
{
Config: testAccAWSWafRegexPatternSetConfig_noPatterns(patternSetName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegexPatternSetExists("aws_waf_regex_pattern_set.test", &patternSet),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "name", patternSetName),
resource.TestCheckResourceAttr("aws_waf_regex_pattern_set.test", "regex_pattern_strings.#", "0"),
testAccCheckAWSWafRegexPatternSetExists(resourceName, &patternSet),
resource.TestCheckResourceAttr(resourceName, "name", patternSetName),
resource.TestCheckResourceAttr(resourceName, "regex_pattern_strings.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSWafRegexPatternSet_disappears(t *testing.T) {
var v waf.RegexPatternSet
patternSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_pattern_set.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -119,7 +140,7 @@ func testAccAWSWafRegexPatternSet_disappears(t *testing.T) {
{
Config: testAccAWSWafRegexPatternSetConfig(patternSetName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegexPatternSetExists("aws_waf_regex_pattern_set.test", &v),
testAccCheckAWSWafRegexPatternSetExists(resourceName, &v),
testAccCheckAWSWafRegexPatternSetDisappears(&v),
),
ExpectNonEmptyPlan: true,
Expand Down Expand Up @@ -215,7 +236,7 @@ func testAccCheckAWSWafRegexPatternSetDestroy(s *terraform.State) error {
}

// Return nil if the Regex Pattern Set is already destroyed
if isAWSErr(err, "WAFNonexistentItemException", "") {
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/waf_regex_pattern_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the WAF Regex Pattern Set.
* `arn` - Amazon Resource Name (ARN)

## Import

AWS WAF Regex Pattern Set can be imported using their ID, e.g.

```
$ terraform import aws_waf_regex_pattern_set.example a1b2c3d4-d5f6-7777-8888-9999aaaabbbbcccc
```

0 comments on commit e4864de

Please sign in to comment.