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 support import for aws_waf_rule_group resource #9254

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
3 changes: 3 additions & 0 deletions aws/resource_aws_waf_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func resourceAwsWafRuleGroup() *schema.Resource {
Read: resourceAwsWafRuleGroupRead,
Update: resourceAwsWafRuleGroupUpdate,
Delete: resourceAwsWafRuleGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand Down
94 changes: 57 additions & 37 deletions aws/resource_aws_waf_rule_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestAccAWSWafRuleGroup_basic(t *testing.T) {

ruleName := fmt.Sprintf("tfacc%s", acctest.RandString(5))
groupName := fmt.Sprintf("tfacc%s", acctest.RandString(5))
resourceName := "aws_waf_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -76,16 +77,21 @@ func TestAccAWSWafRuleGroup_basic(t *testing.T) {
Config: testAccAWSWafRuleGroupConfig(ruleName, groupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRuleExists("aws_waf_rule.test", &rule),
testAccCheckAWSWafRuleGroupExists("aws_waf_rule_group.test", &group),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "name", groupName),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "activated_rule.#", "1"),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "metric_name", groupName),
testAccCheckAWSWafRuleGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", groupName),
computeWafActivatedRuleWithRuleId(&rule, "COUNT", 50, &idx),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.action.0.type", &idx, "COUNT"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.priority", &idx, "50"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.type", &idx, waf.WafRuleTypeRegular),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.action.0.type", &idx, "COUNT"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.priority", &idx, "50"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.type", &idx, waf.WafRuleTypeRegular),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -96,6 +102,7 @@ func TestAccAWSWafRuleGroup_changeNameForceNew(t *testing.T) {
ruleName := fmt.Sprintf("tfacc%s", acctest.RandString(5))
groupName := fmt.Sprintf("tfacc%s", acctest.RandString(5))
newGroupName := fmt.Sprintf("tfacc%s", acctest.RandString(5))
resourceName := "aws_waf_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -105,21 +112,26 @@ func TestAccAWSWafRuleGroup_changeNameForceNew(t *testing.T) {
{
Config: testAccAWSWafRuleGroupConfig(ruleName, groupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRuleGroupExists("aws_waf_rule_group.test", &before),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "name", groupName),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "activated_rule.#", "1"),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "metric_name", groupName),
testAccCheckAWSWafRuleGroupExists(resourceName, &before),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", groupName),
),
},
{
Config: testAccAWSWafRuleGroupConfig(ruleName, newGroupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRuleGroupExists("aws_waf_rule_group.test", &after),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "name", newGroupName),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "activated_rule.#", "1"),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "metric_name", newGroupName),
testAccCheckAWSWafRuleGroupExists(resourceName, &after),
resource.TestCheckResourceAttr(resourceName, "name", newGroupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metric_name", newGroupName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -128,6 +140,7 @@ func TestAccAWSWafRuleGroup_disappears(t *testing.T) {
var group waf.RuleGroup
ruleName := fmt.Sprintf("tfacc%s", acctest.RandString(5))
groupName := fmt.Sprintf("tfacc%s", acctest.RandString(5))
resourceName := "aws_waf_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -137,7 +150,7 @@ func TestAccAWSWafRuleGroup_disappears(t *testing.T) {
{
Config: testAccAWSWafRuleGroupConfig(ruleName, groupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSWafRuleGroupExists("aws_waf_rule_group.test", &group),
testAccCheckAWSWafRuleGroupExists(resourceName, &group),
testAccCheckAWSWafRuleGroupDisappears(&group),
),
ExpectNonEmptyPlan: true,
Expand All @@ -155,6 +168,7 @@ func TestAccAWSWafRuleGroup_changeActivatedRules(t *testing.T) {
ruleName1 := fmt.Sprintf("tfacc%s", acctest.RandString(5))
ruleName2 := fmt.Sprintf("tfacc%s", acctest.RandString(5))
ruleName3 := fmt.Sprintf("tfacc%s", acctest.RandString(5))
resourceName := "aws_waf_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -165,41 +179,46 @@ func TestAccAWSWafRuleGroup_changeActivatedRules(t *testing.T) {
Config: testAccAWSWafRuleGroupConfig(ruleName1, groupName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRuleExists("aws_waf_rule.test", &rule0),
testAccCheckAWSWafRuleGroupExists("aws_waf_rule_group.test", &groupBefore),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "name", groupName),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "activated_rule.#", "1"),
testAccCheckAWSWafRuleGroupExists(resourceName, &groupBefore),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "1"),
computeWafActivatedRuleWithRuleId(&rule0, "COUNT", 50, &idx0),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.action.0.type", &idx0, "COUNT"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.priority", &idx0, "50"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.type", &idx0, waf.WafRuleTypeRegular),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.action.0.type", &idx0, "COUNT"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.priority", &idx0, "50"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.type", &idx0, waf.WafRuleTypeRegular),
),
},
{
Config: testAccAWSWafRuleGroupConfig_changeActivatedRules(ruleName1, ruleName2, ruleName3, groupName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "name", groupName),
resource.TestCheckResourceAttr("aws_waf_rule_group.test", "activated_rule.#", "3"),
testAccCheckAWSWafRuleGroupExists("aws_waf_rule_group.test", &groupAfter),
resource.TestCheckResourceAttr(resourceName, "name", groupName),
resource.TestCheckResourceAttr(resourceName, "activated_rule.#", "3"),
testAccCheckAWSWafRuleGroupExists(resourceName, &groupAfter),

testAccCheckAWSWafRuleExists("aws_waf_rule.test", &rule1),
computeWafActivatedRuleWithRuleId(&rule1, "BLOCK", 10, &idx1),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.action.0.type", &idx1, "BLOCK"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.priority", &idx1, "10"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.type", &idx1, waf.WafRuleTypeRegular),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.action.0.type", &idx1, "BLOCK"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.priority", &idx1, "10"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.type", &idx1, waf.WafRuleTypeRegular),

testAccCheckAWSWafRuleExists("aws_waf_rule.test2", &rule2),
computeWafActivatedRuleWithRuleId(&rule2, "COUNT", 1, &idx2),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.action.0.type", &idx2, "COUNT"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.priority", &idx2, "1"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.type", &idx2, waf.WafRuleTypeRegular),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.action.0.type", &idx2, "COUNT"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.priority", &idx2, "1"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.type", &idx2, waf.WafRuleTypeRegular),

testAccCheckAWSWafRuleExists("aws_waf_rule.test3", &rule3),
computeWafActivatedRuleWithRuleId(&rule3, "BLOCK", 15, &idx3),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.action.0.type", &idx3, "BLOCK"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.priority", &idx3, "15"),
testCheckResourceAttrWithIndexesAddr("aws_waf_rule_group.test", "activated_rule.%d.type", &idx3, waf.WafRuleTypeRegular),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.action.0.type", &idx3, "BLOCK"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.priority", &idx3, "15"),
testCheckResourceAttrWithIndexesAddr(resourceName, "activated_rule.%d.type", &idx3, waf.WafRuleTypeRegular),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -231,6 +250,7 @@ func computeWafActivatedRuleWithRuleId(rule *waf.Rule, actionType string, priori
func TestAccAWSWafRuleGroup_noActivatedRules(t *testing.T) {
var group waf.RuleGroup
groupName := fmt.Sprintf("test%s", acctest.RandString(5))
resourceName := "aws_waf_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSWaf(t) },
Expand All @@ -240,11 +260,11 @@ func TestAccAWSWafRuleGroup_noActivatedRules(t *testing.T) {
{
Config: testAccAWSWafRuleGroupConfig_noActivatedRules(groupName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSWafRuleGroupExists("aws_waf_rule_group.test", &group),
testAccCheckAWSWafRuleGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(
"aws_waf_rule_group.test", "name", groupName),
resourceName, "name", groupName),
resource.TestCheckResourceAttr(
"aws_waf_rule_group.test", "activated_rule.#", "0"),
resourceName, "activated_rule.#", "0"),
),
},
},
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/waf_rule_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the WAF rule group.

## Import

WAF Rule Group can be imported using the id, e.g.

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