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_match_set resource #10481

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
18 changes: 17 additions & 1 deletion aws/resource_aws_waf_regex_match_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"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 @@ -16,13 +17,20 @@ func resourceAwsWafRegexMatchSet() *schema.Resource {
Read: resourceAwsWafRegexMatchSetRead,
Update: resourceAwsWafRegexMatchSetUpdate,
Delete: resourceAwsWafRegexMatchSetDelete,
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_match_tuple": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -96,7 +104,7 @@ func resourceAwsWafRegexMatchSetRead(d *schema.ResourceData, meta interface{}) e

resp, err := conn.GetRegexMatchSet(params)
if err != nil {
if isAWSErr(err, "WAFNonexistentItemException", "") {
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
log.Printf("[WARN] WAF Regex Match Set (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Expand All @@ -108,6 +116,14 @@ func resourceAwsWafRegexMatchSetRead(d *schema.ResourceData, meta interface{}) e
d.Set("name", resp.RegexMatchSet.Name)
d.Set("regex_match_tuple", flattenWafRegexMatchTuples(resp.RegexMatchSet.RegexMatchTuples))

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

return nil
}

Expand Down
73 changes: 47 additions & 26 deletions aws/resource_aws_waf_regex_match_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -99,6 +100,7 @@ func testAccAWSWafRegexMatchSet_basic(t *testing.T) {

matchSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
patternSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_match_set.test"

fieldToMatch := waf.FieldToMatch{
Data: aws.String("User-Agent"),
Expand All @@ -113,17 +115,23 @@ func testAccAWSWafRegexMatchSet_basic(t *testing.T) {
{
Config: testAccAWSWafRegexMatchSetConfig(matchSetName, patternSetName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegexMatchSetExists("aws_waf_regex_match_set.test", &matchSet),
testAccCheckAWSWafRegexMatchSetExists(resourceName, &matchSet),
testAccCheckAWSWafRegexPatternSetExists("aws_waf_regex_pattern_set.test", &patternSet),
testAccMatchResourceAttrGlobalARN(resourceName, "arn", "waf", regexp.MustCompile(`regexmatchset/.+`)),
computeWafRegexMatchSetTuple(&patternSet, &fieldToMatch, "NONE", &idx),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "name", matchSetName),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "regex_match_tuple.#", "1"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.#", &idx, "1"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.0.data", &idx, "user-agent"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.0.type", &idx, "HEADER"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.text_transformation", &idx, "NONE"),
resource.TestCheckResourceAttr(resourceName, "name", matchSetName),
resource.TestCheckResourceAttr(resourceName, "regex_match_tuple.#", "1"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.#", &idx, "1"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.0.data", &idx, "user-agent"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.0.type", &idx, "HEADER"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.text_transformation", &idx, "NONE"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -135,6 +143,7 @@ func testAccAWSWafRegexMatchSet_changePatterns(t *testing.T) {

matchSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
patternSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_match_set.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -144,38 +153,44 @@ func testAccAWSWafRegexMatchSet_changePatterns(t *testing.T) {
{
Config: testAccAWSWafRegexMatchSetConfig(matchSetName, patternSetName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegexMatchSetExists("aws_waf_regex_match_set.test", &before),
testAccCheckAWSWafRegexMatchSetExists(resourceName, &before),
testAccCheckAWSWafRegexPatternSetExists("aws_waf_regex_pattern_set.test", &patternSet),
computeWafRegexMatchSetTuple(&patternSet, &waf.FieldToMatch{Data: aws.String("User-Agent"), Type: aws.String("HEADER")}, "NONE", &idx1),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "name", matchSetName),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "regex_match_tuple.#", "1"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.#", &idx1, "1"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.0.data", &idx1, "user-agent"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.0.type", &idx1, "HEADER"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.text_transformation", &idx1, "NONE"),
resource.TestCheckResourceAttr(resourceName, "name", matchSetName),
resource.TestCheckResourceAttr(resourceName, "regex_match_tuple.#", "1"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.#", &idx1, "1"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.0.data", &idx1, "user-agent"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.0.type", &idx1, "HEADER"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.text_transformation", &idx1, "NONE"),
),
},
{
Config: testAccAWSWafRegexMatchSetConfig_changePatterns(matchSetName, patternSetName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegexMatchSetExists("aws_waf_regex_match_set.test", &after),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "name", matchSetName),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "regex_match_tuple.#", "1"),
testAccCheckAWSWafRegexMatchSetExists(resourceName, &after),
resource.TestCheckResourceAttr(resourceName, "name", matchSetName),
resource.TestCheckResourceAttr(resourceName, "regex_match_tuple.#", "1"),

computeWafRegexMatchSetTuple(&patternSet, &waf.FieldToMatch{Data: aws.String("Referer"), Type: aws.String("HEADER")}, "COMPRESS_WHITE_SPACE", &idx2),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.#", &idx2, "1"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.0.data", &idx2, "referer"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.field_to_match.0.type", &idx2, "HEADER"),
testCheckResourceAttrWithIndexesAddr("aws_waf_regex_match_set.test", "regex_match_tuple.%d.text_transformation", &idx2, "COMPRESS_WHITE_SPACE"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.#", &idx2, "1"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.0.data", &idx2, "referer"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.field_to_match.0.type", &idx2, "HEADER"),
testCheckResourceAttrWithIndexesAddr(resourceName, "regex_match_tuple.%d.text_transformation", &idx2, "COMPRESS_WHITE_SPACE"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccAWSWafRegexMatchSet_noPatterns(t *testing.T) {
var matchSet waf.RegexMatchSet
matchSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_match_set.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -185,11 +200,16 @@ func testAccAWSWafRegexMatchSet_noPatterns(t *testing.T) {
{
Config: testAccAWSWafRegexMatchSetConfig_noPatterns(matchSetName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRegexMatchSetExists("aws_waf_regex_match_set.test", &matchSet),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "name", matchSetName),
resource.TestCheckResourceAttr("aws_waf_regex_match_set.test", "regex_match_tuple.#", "0"),
testAccCheckAWSWafRegexMatchSetExists(resourceName, &matchSet),
resource.TestCheckResourceAttr(resourceName, "name", matchSetName),
resource.TestCheckResourceAttr(resourceName, "regex_match_tuple.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -198,6 +218,7 @@ func testAccAWSWafRegexMatchSet_disappears(t *testing.T) {
var matchSet waf.RegexMatchSet
matchSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
patternSetName := fmt.Sprintf("tfacc-%s", acctest.RandString(5))
resourceName := "aws_waf_regex_match_set.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -207,7 +228,7 @@ func testAccAWSWafRegexMatchSet_disappears(t *testing.T) {
{
Config: testAccAWSWafRegexMatchSetConfig(matchSetName, patternSetName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRegexMatchSetExists("aws_waf_regex_match_set.test", &matchSet),
testAccCheckAWSWafRegexMatchSetExists(resourceName, &matchSet),
testAccCheckAWSWafRegexMatchSetDisappears(&matchSet),
),
ExpectNonEmptyPlan: true,
Expand Down Expand Up @@ -316,7 +337,7 @@ func testAccCheckAWSWafRegexMatchSetDestroy(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_match_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

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

## Import

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

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