forked from hashicorp/terraform-provider-aws
/
resource_aws_waf_geo_match_set.go
157 lines (130 loc) · 4.2 KB
/
resource_aws_waf_geo_match_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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 {
// TODO: Replace with constant once it's available
// See https://github.com/aws/aws-sdk-go/issues/1856
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
}