Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import support for aws_waf_regex_pattern_set resource #10482

Merged
merged 6 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -30,3 +30,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
```