/
resource_aws_wafregional_web_acl_association.go
124 lines (102 loc) · 3.13 KB
/
resource_aws_wafregional_web_acl_association.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
package aws
import (
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/wafregional"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsWafRegionalWebAclAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsWafRegionalWebAclAssociationCreate,
Read: resourceAwsWafRegionalWebAclAssociationRead,
Delete: resourceAwsWafRegionalWebAclAssociationDelete,
Schema: map[string]*schema.Schema{
"web_acl_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsWafRegionalWebAclAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafregionalconn
log.Printf(
"[INFO] Creating WAF Regional Web ACL association: %s => %s",
d.Get("web_acl_id").(string),
d.Get("resource_arn").(string))
params := &wafregional.AssociateWebACLInput{
WebACLId: aws.String(d.Get("web_acl_id").(string)),
ResourceArn: aws.String(d.Get("resource_arn").(string)),
}
// create association and wait on retryable error
// no response body
var err error
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
_, err = conn.AssociateWebACL(params)
if err != nil {
if isAWSErr(err, wafregional.ErrCodeWAFUnavailableEntityException, "") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return err
}
// Store association id
d.SetId(fmt.Sprintf("%s:%s", *params.WebACLId, *params.ResourceArn))
return nil
}
func resourceAwsWafRegionalWebAclAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafregionalconn
webAclId, resourceArn := resourceAwsWafRegionalWebAclAssociationParseId(d.Id())
// List all resources for Web ACL and see if we get a match
params := &wafregional.ListResourcesForWebACLInput{
WebACLId: aws.String(webAclId),
}
resp, err := conn.ListResourcesForWebACL(params)
if err != nil {
return err
}
// Find match
found := false
for _, listResourceArn := range resp.ResourceArns {
if resourceArn == *listResourceArn {
found = true
break
}
}
if !found {
log.Printf("[WARN] WAF Regional Web ACL association (%s) not found, removing from state", d.Id())
d.SetId("")
}
return nil
}
func resourceAwsWafRegionalWebAclAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafregionalconn
_, resourceArn := resourceAwsWafRegionalWebAclAssociationParseId(d.Id())
log.Printf("[INFO] Deleting WAF Regional Web ACL association: %s", resourceArn)
params := &wafregional.DisassociateWebACLInput{
ResourceArn: aws.String(resourceArn),
}
// If action successful HTTP 200 response with an empty body
_, err := conn.DisassociateWebACL(params)
return err
}
func resourceAwsWafRegionalWebAclAssociationParseId(id string) (webAclId, resourceArn string) {
parts := strings.SplitN(id, ":", 2)
webAclId = parts[0]
resourceArn = parts[1]
return
}