forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_load_balancer_policy.go
352 lines (285 loc) · 10.4 KB
/
resource_aws_load_balancer_policy.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package aws
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsLoadBalancerPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLoadBalancerPolicyCreate,
Read: resourceAwsLoadBalancerPolicyRead,
Update: resourceAwsLoadBalancerPolicyUpdate,
Delete: resourceAwsLoadBalancerPolicyDelete,
Schema: map[string]*schema.Schema{
"load_balancer_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy_type_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy_attribute": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
func resourceAwsLoadBalancerPolicyCreate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
attributes := []*elb.PolicyAttribute{}
if attributedata, ok := d.GetOk("policy_attribute"); ok {
attributeSet := attributedata.(*schema.Set).List()
for _, attribute := range attributeSet {
data := attribute.(map[string]interface{})
attributes = append(attributes, &elb.PolicyAttribute{
AttributeName: aws.String(data["name"].(string)),
AttributeValue: aws.String(data["value"].(string)),
})
}
}
lbspOpts := &elb.CreateLoadBalancerPolicyInput{
LoadBalancerName: aws.String(d.Get("load_balancer_name").(string)),
PolicyName: aws.String(d.Get("policy_name").(string)),
PolicyTypeName: aws.String(d.Get("policy_type_name").(string)),
PolicyAttributes: attributes,
}
if _, err := elbconn.CreateLoadBalancerPolicy(lbspOpts); err != nil {
return fmt.Errorf("Error creating LoadBalancerPolicy: %s", err)
}
d.SetId(fmt.Sprintf("%s:%s",
*lbspOpts.LoadBalancerName,
*lbspOpts.PolicyName))
return resourceAwsLoadBalancerPolicyRead(d, meta)
}
func resourceAwsLoadBalancerPolicyRead(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
loadBalancerName, policyName := resourceAwsLoadBalancerPolicyParseId(d.Id())
request := &elb.DescribeLoadBalancerPoliciesInput{
LoadBalancerName: aws.String(loadBalancerName),
PolicyNames: []*string{aws.String(policyName)},
}
getResp, err := elbconn.DescribeLoadBalancerPolicies(request)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "PolicyNotFound" {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving policy: %s", err)
}
if len(getResp.PolicyDescriptions) != 1 {
return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions)
}
policyDesc := getResp.PolicyDescriptions[0]
policyTypeName := policyDesc.PolicyTypeName
policyAttributes := policyDesc.PolicyAttributeDescriptions
attributes := []map[string]string{}
for _, a := range policyAttributes {
pair := make(map[string]string)
pair["name"] = *a.AttributeName
pair["value"] = *a.AttributeValue
if (*policyTypeName == "SSLNegotiationPolicyType") && (*a.AttributeValue == "false") {
continue
}
attributes = append(attributes, pair)
}
d.Set("policy_name", policyName)
d.Set("policy_type_name", policyTypeName)
d.Set("load_balancer_name", loadBalancerName)
d.Set("policy_attribute", attributes)
return nil
}
func resourceAwsLoadBalancerPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
reassignments := Reassignment{}
loadBalancerName, policyName := resourceAwsLoadBalancerPolicyParseId(d.Id())
assigned, err := resourceAwsLoadBalancerPolicyAssigned(policyName, loadBalancerName, elbconn)
if err != nil {
return fmt.Errorf("Error determining assignment status of Load Balancer Policy %s: %s", policyName, err)
}
if assigned {
reassignments, err = resourceAwsLoadBalancerPolicyUnassign(policyName, loadBalancerName, elbconn)
if err != nil {
return fmt.Errorf("Error unassigning Load Balancer Policy %s: %s", policyName, err)
}
}
request := &elb.DeleteLoadBalancerPolicyInput{
LoadBalancerName: aws.String(loadBalancerName),
PolicyName: aws.String(policyName),
}
if _, err := elbconn.DeleteLoadBalancerPolicy(request); err != nil {
return fmt.Errorf("Error deleting Load Balancer Policy %s: %s", d.Id(), err)
}
err = resourceAwsLoadBalancerPolicyCreate(d, meta)
for _, listenerAssignment := range reassignments.listenerPolicies {
if _, err := elbconn.SetLoadBalancerPoliciesOfListener(listenerAssignment); err != nil {
return fmt.Errorf("Error setting LoadBalancerPoliciesOfListener: %s", err)
}
}
for _, backendServerAssignment := range reassignments.backendServerPolicies {
if _, err := elbconn.SetLoadBalancerPoliciesForBackendServer(backendServerAssignment); err != nil {
return fmt.Errorf("Error setting LoadBalancerPoliciesForBackendServer: %s", err)
}
}
return resourceAwsLoadBalancerPolicyRead(d, meta)
}
func resourceAwsLoadBalancerPolicyDelete(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
loadBalancerName, policyName := resourceAwsLoadBalancerPolicyParseId(d.Id())
assigned, err := resourceAwsLoadBalancerPolicyAssigned(policyName, loadBalancerName, elbconn)
if err != nil {
return fmt.Errorf("Error determining assignment status of Load Balancer Policy %s: %s", policyName, err)
}
if assigned {
_, err := resourceAwsLoadBalancerPolicyUnassign(policyName, loadBalancerName, elbconn)
if err != nil {
return fmt.Errorf("Error unassigning Load Balancer Policy %s: %s", policyName, err)
}
}
request := &elb.DeleteLoadBalancerPolicyInput{
LoadBalancerName: aws.String(loadBalancerName),
PolicyName: aws.String(policyName),
}
if _, err := elbconn.DeleteLoadBalancerPolicy(request); err != nil {
return fmt.Errorf("Error deleting Load Balancer Policy %s: %s", d.Id(), err)
}
d.SetId("")
return nil
}
func resourceAwsLoadBalancerPolicyParseId(id string) (string, string) {
parts := strings.SplitN(id, ":", 2)
return parts[0], parts[1]
}
func resourceAwsLoadBalancerPolicyAssigned(policyName, loadBalancerName string, elbconn *elb.ELB) (bool, error) {
describeElbOpts := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{aws.String(loadBalancerName)},
}
describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
if ec2err.Code() == "LoadBalancerNotFound" {
return false, nil
}
}
return false, fmt.Errorf("Error retrieving ELB description: %s", err)
}
if len(describeResp.LoadBalancerDescriptions) != 1 {
return false, fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
}
lb := describeResp.LoadBalancerDescriptions[0]
assigned := false
for _, backendServer := range lb.BackendServerDescriptions {
for _, name := range backendServer.PolicyNames {
if policyName == *name {
assigned = true
break
}
}
}
for _, listener := range lb.ListenerDescriptions {
for _, name := range listener.PolicyNames {
if policyName == *name {
assigned = true
break
}
}
}
return assigned, nil
}
type Reassignment struct {
backendServerPolicies []*elb.SetLoadBalancerPoliciesForBackendServerInput
listenerPolicies []*elb.SetLoadBalancerPoliciesOfListenerInput
}
func resourceAwsLoadBalancerPolicyUnassign(policyName, loadBalancerName string, elbconn *elb.ELB) (Reassignment, error) {
reassignments := Reassignment{}
describeElbOpts := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{aws.String(loadBalancerName)},
}
describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
if err != nil {
if ec2err, ok := err.(awserr.Error); ok {
if ec2err.Code() == "LoadBalancerNotFound" {
return reassignments, nil
}
}
return reassignments, fmt.Errorf("Error retrieving ELB description: %s", err)
}
if len(describeResp.LoadBalancerDescriptions) != 1 {
return reassignments, fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
}
lb := describeResp.LoadBalancerDescriptions[0]
for _, backendServer := range lb.BackendServerDescriptions {
policies := []*string{}
for _, name := range backendServer.PolicyNames {
if policyName != *name {
policies = append(policies, name)
}
}
if len(backendServer.PolicyNames) != len(policies) {
setOpts := &elb.SetLoadBalancerPoliciesForBackendServerInput{
LoadBalancerName: aws.String(loadBalancerName),
InstancePort: aws.Int64(*backendServer.InstancePort),
PolicyNames: policies,
}
reassignOpts := &elb.SetLoadBalancerPoliciesForBackendServerInput{
LoadBalancerName: aws.String(loadBalancerName),
InstancePort: aws.Int64(*backendServer.InstancePort),
PolicyNames: backendServer.PolicyNames,
}
reassignments.backendServerPolicies = append(reassignments.backendServerPolicies, reassignOpts)
_, err = elbconn.SetLoadBalancerPoliciesForBackendServer(setOpts)
if err != nil {
return reassignments, fmt.Errorf("Error Setting Load Balancer Policies for Backend Server: %s", err)
}
}
}
for _, listener := range lb.ListenerDescriptions {
policies := []*string{}
for _, name := range listener.PolicyNames {
if policyName != *name {
policies = append(policies, name)
}
}
if len(listener.PolicyNames) != len(policies) {
setOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
LoadBalancerName: aws.String(loadBalancerName),
LoadBalancerPort: aws.Int64(*listener.Listener.LoadBalancerPort),
PolicyNames: policies,
}
reassignOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
LoadBalancerName: aws.String(loadBalancerName),
LoadBalancerPort: aws.Int64(*listener.Listener.LoadBalancerPort),
PolicyNames: listener.PolicyNames,
}
reassignments.listenerPolicies = append(reassignments.listenerPolicies, reassignOpts)
_, err = elbconn.SetLoadBalancerPoliciesOfListener(setOpts)
if err != nil {
return reassignments, fmt.Errorf("Error Setting Load Balancer Policies of Listener: %s", err)
}
}
}
return reassignments, nil
}