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

New Resource: aws_waf_geo_match_set #3275

Merged
merged 1 commit into from
Mar 14, 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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ func Provider() terraform.ResourceProvider {
"aws_waf_web_acl": resourceAwsWafWebAcl(),
"aws_waf_xss_match_set": resourceAwsWafXssMatchSet(),
"aws_waf_sql_injection_match_set": resourceAwsWafSqlInjectionMatchSet(),
"aws_waf_geo_match_set": resourceAwsWafGeoMatchSet(),
"aws_wafregional_byte_match_set": resourceAwsWafRegionalByteMatchSet(),
"aws_wafregional_ipset": resourceAwsWafRegionalIPSet(),
"aws_wafregional_xss_match_set": resourceAwsWafRegionalXssMatchSet(),
Expand Down
200 changes: 200 additions & 0 deletions aws/resource_aws_waf_geo_match_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package aws

import (
"fmt"
"log"

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

func resourceAwsWafGeoMatchSet() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWafGeoMatchSetCreate,
Read: resourceAwsWafGeoMatchSetRead,
Update: resourceAwsWafGeoMatchSetUpdate,
Delete: resourceAwsWafGeoMatchSetDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"geo_match_constraint": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}

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

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

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

return conn.CreateGeoMatchSet(params)
})
if err != nil {
return errwrap.Wrapf("[ERROR] Error creating GeoMatchSet: {{err}}", err)
}
resp := out.(*waf.CreateGeoMatchSetOutput)

d.SetId(*resp.GeoMatchSet.GeoMatchSetId)

return resourceAwsWafGeoMatchSetUpdate(d, meta)
}

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

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

return err
}

d.Set("name", resp.GeoMatchSet.Name)
d.Set("geo_match_constraint", flattenWafGeoMatchConstraint(resp.GeoMatchSet.GeoMatchConstraints))

return nil
}

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

if d.HasChange("geo_match_constraint") {
o, n := d.GetChange("geo_match_constraint")
oldT, newT := o.(*schema.Set).List(), n.(*schema.Set).List()

err := updateGeoMatchSetResource(d.Id(), oldT, newT, conn)
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating GeoMatchSet: {{err}}", err)
}
}

return resourceAwsWafGeoMatchSetRead(d, meta)
}

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

oldConstraints := d.Get("geo_match_constraint").(*schema.Set).List()
if len(oldConstraints) > 0 {
noConstraints := []interface{}{}
err := updateGeoMatchSetResource(d.Id(), oldConstraints, noConstraints, conn)
if err != nil {
return fmt.Errorf("Error updating GeoMatchConstraint: %s", err)
}
}

wr := newWafRetryer(conn, "global")
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.DeleteGeoMatchSetInput{
ChangeToken: token,
GeoMatchSetId: aws.String(d.Id()),
}

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

return nil
}

func updateGeoMatchSetResource(id string, oldT, newT []interface{}, conn *waf.WAF) error {
wr := newWafRetryer(conn, "global")
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.UpdateGeoMatchSetInput{
ChangeToken: token,
GeoMatchSetId: aws.String(id),
Updates: diffWafGeoMatchSetConstraints(oldT, newT),
}

log.Printf("[INFO] Updating GeoMatchSet constraints: %s", req)
return conn.UpdateGeoMatchSet(req)
})
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating GeoMatchSet: {{err}}", err)
}

return nil
}

func flattenWafGeoMatchConstraint(ts []*waf.GeoMatchConstraint) []interface{} {
out := make([]interface{}, len(ts), len(ts))
for i, t := range ts {
m := make(map[string]interface{})
m["type"] = *t.Type
m["value"] = *t.Value
out[i] = m
}
return out
}

func diffWafGeoMatchSetConstraints(oldT, newT []interface{}) []*waf.GeoMatchSetUpdate {
updates := make([]*waf.GeoMatchSetUpdate, 0)

for _, od := range oldT {
constraint := od.(map[string]interface{})

if idx, contains := sliceContainsMap(newT, constraint); contains {
newT = append(newT[:idx], newT[idx+1:]...)
continue
}

updates = append(updates, &waf.GeoMatchSetUpdate{
Action: aws.String(waf.ChangeActionDelete),
GeoMatchConstraint: &waf.GeoMatchConstraint{
Type: aws.String(constraint["type"].(string)),
Value: aws.String(constraint["value"].(string)),
},
})
}

for _, nd := range newT {
constraint := nd.(map[string]interface{})

updates = append(updates, &waf.GeoMatchSetUpdate{
Action: aws.String(waf.ChangeActionInsert),
GeoMatchConstraint: &waf.GeoMatchConstraint{
Type: aws.String(constraint["type"].(string)),
Value: aws.String(constraint["value"].(string)),
},
})
}
return updates
}
Loading