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

r/waf_rule_group - refactor to use keyvaluetags #10799

Merged
merged 9 commits into from
Nov 8, 2019
37 changes: 22 additions & 15 deletions aws/resource_aws_waf_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func resourceAwsWafRuleGroup() *schema.Resource {
},
},
"tags": tagsSchema(),
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -95,7 +99,18 @@ func resourceAwsWafRuleGroupCreate(d *schema.ResourceData, meta interface{}) err
}
resp := out.(*waf.CreateRuleGroupOutput)
d.SetId(*resp.RuleGroup.RuleGroupId)
return resourceAwsWafRuleGroupUpdate(d, meta)

activatedRules := d.Get("activated_rule").(*schema.Set).List()
if len(activatedRules) > 0 {
noActivatedRules := []interface{}{}

err := updateWafRuleGroupResource(d.Id(), noActivatedRules, activatedRules, conn)
if err != nil {
return fmt.Errorf("Error Updating WAF Rule Group: %s", err)
}
}

return resourceAwsWafRuleGroupRead(d, meta)
}

func resourceAwsWafRuleGroupRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -107,7 +122,7 @@ func resourceAwsWafRuleGroupRead(d *schema.ResourceData, meta interface{}) error

resp, err := conn.GetRuleGroup(params)
if err != nil {
if isAWSErr(err, "WAFNonexistentItemException", "") {
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
log.Printf("[WARN] WAF Rule Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Expand All @@ -129,14 +144,13 @@ func resourceAwsWafRuleGroupRead(d *schema.ResourceData, meta interface{}) error
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("rulegroup/%s", d.Id()),
}.String()
d.Set("arn", arn)

tagList, err := conn.ListTagsForResource(&waf.ListTagsForResourceInput{
ResourceARN: aws.String(arn),
})
tags, err := keyvaluetags.WafListTags(conn, arn)
if err != nil {
return fmt.Errorf("Failed to get WAF Rule Group parameter tags for %s: %s", d.Get("name"), err)
return fmt.Errorf("error listing tags for WAF Rule Group (%s): %s", arn, err)
}
if err := d.Set("tags", keyvaluetags.WafKeyValueTags(tagList.TagInfoForResource.TagList).IgnoreAws().Map()); err != nil {
if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand All @@ -163,14 +177,7 @@ func resourceAwsWafRuleGroupUpdate(d *schema.ResourceData, meta interface{}) err
if d.HasChange("tags") {
o, n := d.GetChange("tags")

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

if err := keyvaluetags.WafUpdateTags(conn, arn, o, n); err != nil {
if err := keyvaluetags.WafUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}
Expand Down
4 changes: 3 additions & 1 deletion aws/resource_aws_waf_rule_group_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 @@ -88,6 +89,7 @@ func TestAccAWSWafRuleGroup_basic(t *testing.T) {
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),
testAccMatchResourceAttrGlobalARN(resourceName, "arn", "waf", regexp.MustCompile(`rulegroup/.+`)),
),
},
{
Expand Down Expand Up @@ -385,7 +387,7 @@ func testAccCheckAWSWafRuleGroupDestroy(s *terraform.State) error {
}
}

if isAWSErr(err, "WAFNonexistentItemException", "") {
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
return nil
}

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/waf_rule_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

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

## Import

Expand Down