Skip to content

Commit

Permalink
Added AWS Resource WAF SizeConstraintSet (#9689)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmaansh21 authored and stack72 committed Oct 28, 2016
1 parent 0f8611c commit cc8f111
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 3 deletions.
5 changes: 3 additions & 2 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,10 @@ func Provider() terraform.ResourceProvider {
"aws_vpn_gateway": resourceAwsVpnGateway(),
"aws_vpn_gateway_attachment": resourceAwsVpnGatewayAttachment(),
"aws_waf_byte_match_set": resourceAwsWafByteMatchSet(),
"aws_waf_web_acl": resourceAwsWafWebAcl(),
"aws_waf_rule": resourceAwsWafRule(),
"aws_waf_ipset": resourceAwsWafIPSet(),
"aws_waf_rule": resourceAwsWafRule(),
"aws_waf_size_constraint_set": resourceAwsWafSizeConstraintSet(),
"aws_waf_web_acl": resourceAwsWafWebAcl(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
191 changes: 191 additions & 0 deletions builtin/providers/aws/resource_aws_waf_size_constraint_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package aws

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/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsWafSizeConstraintSet() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWafSizeConstraintSetCreate,
Read: resourceAwsWafSizeConstraintSetRead,
Update: resourceAwsWafSizeConstraintSetUpdate,
Delete: resourceAwsWafSizeConstraintSetDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"size_constraints": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field_to_match": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"comparison_operator": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"size": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"text_transformation": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}

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

log.Printf("[INFO] Creating SizeConstraintSet: %s", d.Get("name").(string))

// ChangeToken
var ct *waf.GetChangeTokenInput

res, err := conn.GetChangeToken(ct)
if err != nil {
return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err)
}

params := &waf.CreateSizeConstraintSetInput{
ChangeToken: res.ChangeToken,
Name: aws.String(d.Get("name").(string)),
}

resp, err := conn.CreateSizeConstraintSet(params)

if err != nil {
return errwrap.Wrapf("[ERROR] Error creating SizeConstraintSet: {{err}}", err)
}

d.SetId(*resp.SizeConstraintSet.SizeConstraintSetId)

return resourceAwsWafSizeConstraintSetUpdate(d, meta)
}

func resourceAwsWafSizeConstraintSetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
log.Printf("[INFO] Reading SizeConstraintSet: %s", d.Get("name").(string))
params := &waf.GetSizeConstraintSetInput{
SizeConstraintSetId: aws.String(d.Id()),
}

resp, err := conn.GetSizeConstraintSet(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
log.Printf("[WARN] WAF IPSet (%s) not found, error code (404)", d.Id())
d.SetId("")
return nil
}

return err
}

d.Set("name", resp.SizeConstraintSet.Name)

return nil
}

func resourceAwsWafSizeConstraintSetUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Updating SizeConstraintSet: %s", d.Get("name").(string))
err := updateSizeConstraintSetResource(d, meta, waf.ChangeActionInsert)
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating SizeConstraintSet: {{err}}", err)
}
return resourceAwsWafSizeConstraintSetRead(d, meta)
}

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

log.Printf("[INFO] Deleting SizeConstraintSet: %s", d.Get("name").(string))
err := updateSizeConstraintSetResource(d, meta, waf.ChangeActionDelete)
if err != nil {
return errwrap.Wrapf("[ERROR] Error deleting SizeConstraintSet: {{err}}", err)
}

var ct *waf.GetChangeTokenInput

resp, err := conn.GetChangeToken(ct)

req := &waf.DeleteSizeConstraintSetInput{
ChangeToken: resp.ChangeToken,
SizeConstraintSetId: aws.String(d.Id()),
}

_, err = conn.DeleteSizeConstraintSet(req)

if err != nil {
return errwrap.Wrapf("[ERROR] Error deleting SizeConstraintSet: {{err}}", err)
}

return nil
}

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

var ct *waf.GetChangeTokenInput

resp, err := conn.GetChangeToken(ct)
if err != nil {
return errwrap.Wrapf("[ERROR] Error getting change token: {{err}}", err)
}

req := &waf.UpdateSizeConstraintSetInput{
ChangeToken: resp.ChangeToken,
SizeConstraintSetId: aws.String(d.Id()),
}

sizeConstraints := d.Get("size_constraints").(*schema.Set)
for _, sizeConstraint := range sizeConstraints.List() {
sc := sizeConstraint.(map[string]interface{})
sizeConstraintUpdate := &waf.SizeConstraintSetUpdate{
Action: aws.String(ChangeAction),
SizeConstraint: &waf.SizeConstraint{
FieldToMatch: expandFieldToMatch(sc["field_to_match"].(*schema.Set).List()[0].(map[string]interface{})),
ComparisonOperator: aws.String(sc["comparison_operator"].(string)),
Size: aws.Int64(int64(sc["size"].(int))),
TextTransformation: aws.String(sc["text_transformation"].(string)),
},
}
req.Updates = append(req.Updates, sizeConstraintUpdate)
}

_, err = conn.UpdateSizeConstraintSet(req)
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating SizeConstraintSet: {{err}}", err)
}

return nil
}
Loading

0 comments on commit cc8f111

Please sign in to comment.