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

resource/aws_waf(regional)_web_acl: Properly read rules into Terraform state #5342

Merged
merged 1 commit into from
Jul 27, 2018
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
38 changes: 20 additions & 18 deletions aws/resource_aws_waf_web_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand All @@ -22,66 +21,66 @@ func resourceAwsWafWebAcl() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"default_action": &schema.Schema{
"default_action": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"metric_name": &schema.Schema{
"metric_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateWafMetricName,
},
"rules": &schema.Schema{
"rules": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": &schema.Schema{
Type: schema.TypeSet,
"action": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"override_action": &schema.Schema{
Type: schema.TypeSet,
"override_action": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"priority": &schema.Schema{
"priority": {
Type: schema.TypeInt,
Required: true,
},
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Default: waf.WafRuleTypeRegular,
Expand All @@ -91,7 +90,7 @@ func resourceAwsWafWebAcl() *schema.Resource {
waf.WafRuleTypeGroup,
}, false),
},
"rule_id": &schema.Schema{
"rule_id": {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -132,7 +131,7 @@ func resourceAwsWafWebAclRead(d *schema.ResourceData, meta interface{}) error {

resp, err := conn.GetWebACL(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
log.Printf("[WARN] WAF ACL (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
Expand All @@ -149,6 +148,9 @@ func resourceAwsWafWebAclRead(d *schema.ResourceData, meta interface{}) error {
}
d.Set("name", resp.WebACL.Name)
d.Set("metric_name", resp.WebACL.MetricName)
if err := d.Set("rules", flattenWafWebAclRules(resp.WebACL.Rules)); err != nil {
return fmt.Errorf("error setting rules: %s", err)
}

return nil
}
Expand Down Expand Up @@ -205,7 +207,7 @@ func updateWebAclResource(d *schema.ResourceData, meta interface{}, ChangeAction
var aclRuleUpdate *waf.WebACLUpdate
switch aclRule["type"].(string) {
case waf.WafRuleTypeGroup:
overrideAction := aclRule["override_action"].(*schema.Set).List()[0].(map[string]interface{})
overrideAction := aclRule["override_action"].([]interface{})[0].(map[string]interface{})
aclRuleUpdate = &waf.WebACLUpdate{
Action: aws.String(ChangeAction),
ActivatedRule: &waf.ActivatedRule{
Expand All @@ -216,7 +218,7 @@ func updateWebAclResource(d *schema.ResourceData, meta interface{}, ChangeAction
},
}
default:
action := aclRule["action"].(*schema.Set).List()[0].(map[string]interface{})
action := aclRule["action"].([]interface{})[0].(map[string]interface{})
aclRuleUpdate = &waf.WebACLUpdate{
Action: aws.String(ChangeAction),
ActivatedRule: &waf.ActivatedRule{
Expand Down
15 changes: 2 additions & 13 deletions aws/resource_aws_waf_web_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform/terraform"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform/helper/acctest"
)
Expand Down Expand Up @@ -43,8 +42,6 @@ func TestAccAWSWafWebAcl_basic(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// The WAF ACL rule resource doesn't GET rules
ImportStateVerifyIgnore: []string{"rules"},
},
},
})
Expand Down Expand Up @@ -80,8 +77,6 @@ func TestAccAWSWafWebAcl_group(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// The WAF ACL rule resource doesn't GET rules
ImportStateVerifyIgnore: []string{"rules"},
},
},
})
Expand Down Expand Up @@ -134,8 +129,6 @@ func TestAccAWSWafWebAcl_changeNameForceNew(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// The WAF ACL rule resource doesn't GET rules
ImportStateVerifyIgnore: []string{"rules"},
},
},
})
Expand Down Expand Up @@ -188,8 +181,6 @@ func TestAccAWSWafWebAcl_changeDefaultAction(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// The WAF ACL rule resource doesn't GET rules
ImportStateVerifyIgnore: []string{"rules"},
},
},
})
Expand Down Expand Up @@ -279,10 +270,8 @@ func testAccCheckAWSWafWebAclDestroy(s *terraform.State) error {
}

// Return nil if the WebACL is already destroyed
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "WAFNonexistentItemException" {
return nil
}
if isAWSErr(err, waf.ErrCodeNonexistentItemException, "") {
continue
}

return err
Expand Down
48 changes: 25 additions & 23 deletions aws/resource_aws_wafregional_web_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,65 @@ func resourceAwsWafRegionalWebAcl() *schema.Resource {
Delete: resourceAwsWafRegionalWebAclDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"default_action": &schema.Schema{
"default_action": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"metric_name": &schema.Schema{
"metric_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"rule": &schema.Schema{
"rule": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": &schema.Schema{
"action": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"override_action": &schema.Schema{
"override_action": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"priority": &schema.Schema{
"priority": {
Type: schema.TypeInt,
Required: true,
},
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Default: waf.WafRuleTypeRegular,
Expand All @@ -87,7 +87,7 @@ func resourceAwsWafRegionalWebAcl() *schema.Resource {
waf.WafRuleTypeGroup,
}, false),
},
"rule_id": &schema.Schema{
"rule_id": {
Type: schema.TypeString,
Required: true,
},
Expand Down Expand Up @@ -141,7 +141,9 @@ func resourceAwsWafRegionalWebAclRead(d *schema.ResourceData, meta interface{})
d.Set("default_action", flattenDefaultActionWR(resp.WebACL.DefaultAction))
d.Set("name", resp.WebACL.Name)
d.Set("metric_name", resp.WebACL.MetricName)
d.Set("rule", flattenWafWebAclRules(resp.WebACL.Rules))
if err := d.Set("rule", flattenWafWebAclRules(resp.WebACL.Rules)); err != nil {
return fmt.Errorf("error setting rule: %s", err)
}

return nil
}
Expand Down Expand Up @@ -238,27 +240,27 @@ func flattenDefaultActionWR(n *waf.WafAction) []map[string]interface{} {
return m.MapList()
}

func flattenWafWebAclRules(ts []*waf.ActivatedRule) []interface{} {
out := make([]interface{}, len(ts), len(ts))
func flattenWafWebAclRules(ts []*waf.ActivatedRule) []map[string]interface{} {
out := make([]map[string]interface{}, len(ts), len(ts))
for i, r := range ts {
m := make(map[string]interface{})

switch *r.Type {
switch aws.StringValue(r.Type) {
case waf.WafRuleTypeGroup:
actionMap := map[string]interface{}{
"type": *r.OverrideAction.Type,
"type": aws.StringValue(r.OverrideAction.Type),
}
m["override_action"] = []interface{}{actionMap}
m["override_action"] = []map[string]interface{}{actionMap}
default:
actionMap := map[string]interface{}{
"type": *r.Action.Type,
"type": aws.StringValue(r.Action.Type),
}
m["action"] = []interface{}{actionMap}
m["action"] = []map[string]interface{}{actionMap}
}

m["priority"] = *r.Priority
m["rule_id"] = *r.RuleId
m["type"] = *r.Type
m["priority"] = int(aws.Int64Value(r.Priority))
m["rule_id"] = aws.StringValue(r.RuleId)
m["type"] = aws.StringValue(r.Type)
out[i] = m
}
return out
Expand Down
2 changes: 0 additions & 2 deletions website/docs/r/waf_web_acl.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,3 @@ WAF Web ACL can be imported using the `id`, e.g.
```
$ terraform import aws_waf_web_acl.main 0c8e583e-18f3-4c13-9e2a-67c4805d2f94
```

~> **Note:** The `rules` will not be imported.