forked from hashicorp/terraform-provider-aws
/
resource_aws_waf_size_constraint_set.go
133 lines (106 loc) · 3.85 KB
/
resource_aws_waf_size_constraint_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
package aws
import (
"fmt"
"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"
)
func resourceAwsWafSizeConstraintSet() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWafSizeConstraintSetCreate,
Read: resourceAwsWafSizeConstraintSetRead,
Update: resourceAwsWafSizeConstraintSetUpdate,
Delete: resourceAwsWafSizeConstraintSetDelete,
Schema: wafSizeConstraintSetSchema(),
}
}
func resourceAwsWafSizeConstraintSetCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
log.Printf("[INFO] Creating SizeConstraintSet: %s", d.Get("name").(string))
wr := newWafRetryer(conn)
out, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
params := &waf.CreateSizeConstraintSetInput{
ChangeToken: token,
Name: aws.String(d.Get("name").(string)),
}
return conn.CreateSizeConstraintSet(params)
})
if err != nil {
return fmt.Errorf("Error creating SizeConstraintSet: %s", err)
}
resp := out.(*waf.CreateSizeConstraintSetOutput)
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 SizeConstraintSet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
d.Set("name", resp.SizeConstraintSet.Name)
d.Set("size_constraints", flattenWafSizeConstraints(resp.SizeConstraintSet.SizeConstraints))
return nil
}
func resourceAwsWafSizeConstraintSetUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
if d.HasChange("size_constraints") {
o, n := d.GetChange("size_constraints")
oldConstraints, newConstraints := o.(*schema.Set).List(), n.(*schema.Set).List()
err := updateSizeConstraintSetResource(d.Id(), oldConstraints, newConstraints, conn)
if err != nil {
return fmt.Errorf("Error updating SizeConstraintSet: %s", err)
}
}
return resourceAwsWafSizeConstraintSetRead(d, meta)
}
func resourceAwsWafSizeConstraintSetDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
oldConstraints := d.Get("size_constraints").(*schema.Set).List()
if len(oldConstraints) > 0 {
noConstraints := []interface{}{}
err := updateSizeConstraintSetResource(d.Id(), oldConstraints, noConstraints, conn)
if err != nil {
return fmt.Errorf("Error deleting SizeConstraintSet: %s", err)
}
}
wr := newWafRetryer(conn)
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.DeleteSizeConstraintSetInput{
ChangeToken: token,
SizeConstraintSetId: aws.String(d.Id()),
}
return conn.DeleteSizeConstraintSet(req)
})
if err != nil {
return fmt.Errorf("Error deleting SizeConstraintSet: %s", err)
}
return nil
}
func updateSizeConstraintSetResource(id string, oldS, newS []interface{}, conn *waf.WAF) error {
wr := newWafRetryer(conn)
_, err := wr.RetryWithToken(func(token *string) (interface{}, error) {
req := &waf.UpdateSizeConstraintSetInput{
ChangeToken: token,
SizeConstraintSetId: aws.String(id),
Updates: diffWafSizeConstraints(oldS, newS),
}
log.Printf("[INFO] Updating WAF Size Constraint constraints: %s", req)
return conn.UpdateSizeConstraintSet(req)
})
if err != nil {
return fmt.Errorf("Error updating SizeConstraintSet: %s", err)
}
return nil
}