Skip to content

Commit

Permalink
Merge pull request hashicorp#3898 from terraform-providers/f-waf-rule…
Browse files Browse the repository at this point in the history
…-group

New Resource: aws_waf_rule_group
  • Loading branch information
radeksimko authored Mar 27, 2018
2 parents 6bf80d9 + 62f1910 commit 5396384
Show file tree
Hide file tree
Showing 6 changed files with 738 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ func Provider() terraform.ResourceProvider {
"aws_waf_rate_based_rule": resourceAwsWafRateBasedRule(),
"aws_waf_regex_pattern_set": resourceAwsWafRegexPatternSet(),
"aws_waf_rule": resourceAwsWafRule(),
"aws_waf_rule_group": resourceAwsWafRuleGroup(),
"aws_waf_size_constraint_set": resourceAwsWafSizeConstraintSet(),
"aws_waf_web_acl": resourceAwsWafWebAcl(),
"aws_waf_xss_match_set": resourceAwsWafXssMatchSet(),
Expand Down
187 changes: 187 additions & 0 deletions aws/resource_aws_waf_rule_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsWafRuleGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWafRuleGroupCreate,
Read: resourceAwsWafRuleGroupRead,
Update: resourceAwsWafRuleGroupUpdate,
Delete: resourceAwsWafRuleGroupDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"metric_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateWafMetricName,
},
"activated_rule": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"priority": {
Type: schema.TypeInt,
Required: true,
},
"rule_id": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: waf.WafRuleTypeRegular,
},
},
},
},
},
}
}

func resourceAwsWafRuleGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn

wr := newWafRetryer(conn, "global")
out, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
params := &waf.CreateRuleGroupInput{
ChangeToken: token,
MetricName: aws.String(d.Get("metric_name").(string)),
Name: aws.String(d.Get("name").(string)),
}

return conn.CreateRuleGroup(params)
})
if err != nil {
return err
}
resp := out.(*waf.CreateRuleGroupOutput)
d.SetId(*resp.RuleGroup.RuleGroupId)
return resourceAwsWafRuleGroupUpdate(d, meta)
}

func resourceAwsWafRuleGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn

params := &waf.GetRuleGroupInput{
RuleGroupId: aws.String(d.Id()),
}

resp, err := conn.GetRuleGroup(params)
if err != nil {
if isAWSErr(err, "WAFNonexistentItemException", "") {
log.Printf("[WARN] WAF Rule Group (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

return err
}

rResp, err := conn.ListActivatedRulesInRuleGroup(&waf.ListActivatedRulesInRuleGroupInput{
RuleGroupId: aws.String(d.Id()),
})

d.Set("activated_rule", flattenWafActivatedRules(rResp.ActivatedRules))
d.Set("name", resp.RuleGroup.Name)
d.Set("metric_name", resp.RuleGroup.MetricName)

return nil
}

func resourceAwsWafRuleGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn

if d.HasChange("activated_rule") {
o, n := d.GetChange("activated_rule")
oldRules, newRules := o.(*schema.Set).List(), n.(*schema.Set).List()

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

return resourceAwsWafRuleGroupRead(d, meta)
}

func resourceAwsWafRuleGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn

oldRules := d.Get("activated_rule").(*schema.Set).List()
err := deleteWafRuleGroup(d.Id(), oldRules, conn)
if err != nil {
return err
}

return nil
}

func deleteWafRuleGroup(id string, oldRules []interface{}, conn *waf.WAF) error {
if len(oldRules) > 0 {
noRules := []interface{}{}
err := updateWafRuleGroupResource(id, oldRules, noRules, conn)
if err != nil {
return fmt.Errorf("Error updating WAF Rule Group Predicates: %s", err)
}
}

wr := newWafRetryer(conn, "global")
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.DeleteRuleGroupInput{
ChangeToken: token,
RuleGroupId: aws.String(id),
}
log.Printf("[INFO] Deleting WAF Rule Group")
return conn.DeleteRuleGroup(req)
})
if err != nil {
return fmt.Errorf("Error deleting WAF Rule Group: %s", err)
}
return nil
}

func updateWafRuleGroupResource(id string, oldRules, newRules []interface{}, conn *waf.WAF) error {
wr := newWafRetryer(conn, "global")
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.UpdateRuleGroupInput{
ChangeToken: token,
RuleGroupId: aws.String(id),
Updates: diffWafRuleGroupActivatedRules(oldRules, newRules),
}

return conn.UpdateRuleGroup(req)
})
if err != nil {
return fmt.Errorf("Error Updating WAF Rule Group: %s", err)
}

return nil
}
Loading

0 comments on commit 5396384

Please sign in to comment.