forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_proxy_protocol_policy.go
267 lines (228 loc) · 7.87 KB
/
resource_aws_proxy_protocol_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
package aws
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsProxyProtocolPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsProxyProtocolPolicyCreate,
Read: resourceAwsProxyProtocolPolicyRead,
Update: resourceAwsProxyProtocolPolicyUpdate,
Delete: resourceAwsProxyProtocolPolicyDelete,
Schema: map[string]*schema.Schema{
"load_balancer": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"instance_ports": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
Set: schema.HashString,
},
},
}
}
func resourceAwsProxyProtocolPolicyCreate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbname := aws.String(d.Get("load_balancer").(string))
input := &elb.CreateLoadBalancerPolicyInput{
LoadBalancerName: elbname,
PolicyAttributes: []*elb.PolicyAttribute{
&elb.PolicyAttribute{
AttributeName: aws.String("ProxyProtocol"),
AttributeValue: aws.String("True"),
},
},
PolicyName: aws.String("TFEnableProxyProtocol"),
PolicyTypeName: aws.String("ProxyProtocolPolicyType"),
}
// Create a policy
log.Printf("[DEBUG] ELB create a policy %s from policy type %s",
*input.PolicyName, *input.PolicyTypeName)
if _, err := elbconn.CreateLoadBalancerPolicy(input); err != nil {
return fmt.Errorf("Error creating a policy %s: %s",
*input.PolicyName, err)
}
// Assign the policy name for use later
d.Partial(true)
d.SetId(fmt.Sprintf("%s:%s", *elbname, *input.PolicyName))
d.SetPartial("load_balancer")
log.Printf("[INFO] ELB PolicyName: %s", *input.PolicyName)
return resourceAwsProxyProtocolPolicyUpdate(d, meta)
}
func resourceAwsProxyProtocolPolicyRead(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbname := aws.String(d.Get("load_balancer").(string))
// Retrieve the current ELB policies for updating the state
req := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{elbname},
}
resp, err := elbconn.DescribeLoadBalancers(req)
if err != nil {
if isLoadBalancerNotFound(err) {
// The ELB is gone now, so just remove it from the state
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving ELB attributes: %s", err)
}
backends := flattenBackendPolicies(resp.LoadBalancerDescriptions[0].BackendServerDescriptions)
ports := []*string{}
for ip := range backends {
ipstr := strconv.Itoa(int(ip))
ports = append(ports, &ipstr)
}
d.Set("instance_ports", ports)
d.Set("load_balancer", *elbname)
return nil
}
func resourceAwsProxyProtocolPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbname := aws.String(d.Get("load_balancer").(string))
// Retrieve the current ELB policies for updating the state
req := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{elbname},
}
resp, err := elbconn.DescribeLoadBalancers(req)
if err != nil {
if isLoadBalancerNotFound(err) {
// The ELB is gone now, so just remove it from the state
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving ELB attributes: %s", err)
}
backends := flattenBackendPolicies(resp.LoadBalancerDescriptions[0].BackendServerDescriptions)
_, policyName := resourceAwsProxyProtocolPolicyParseId(d.Id())
d.Partial(true)
if d.HasChange("instance_ports") {
o, n := d.GetChange("instance_ports")
os := o.(*schema.Set)
ns := n.(*schema.Set)
remove := os.Difference(ns).List()
add := ns.Difference(os).List()
inputs := []*elb.SetLoadBalancerPoliciesForBackendServerInput{}
i, err := resourceAwsProxyProtocolPolicyRemove(policyName, remove, backends)
if err != nil {
return err
}
inputs = append(inputs, i...)
i, err = resourceAwsProxyProtocolPolicyAdd(policyName, add, backends)
if err != nil {
return err
}
inputs = append(inputs, i...)
for _, input := range inputs {
input.LoadBalancerName = elbname
if _, err := elbconn.SetLoadBalancerPoliciesForBackendServer(input); err != nil {
return fmt.Errorf("Error setting policy for backend: %s", err)
}
}
d.SetPartial("instance_ports")
}
return resourceAwsProxyProtocolPolicyRead(d, meta)
}
func resourceAwsProxyProtocolPolicyDelete(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbconn
elbname := aws.String(d.Get("load_balancer").(string))
// Retrieve the current ELB policies for updating the state
req := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{elbname},
}
var err error
resp, err := elbconn.DescribeLoadBalancers(req)
if err != nil {
if isLoadBalancerNotFound(err) {
// The ELB is gone now, so just remove it from the state
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving ELB attributes: %s", err)
}
backends := flattenBackendPolicies(resp.LoadBalancerDescriptions[0].BackendServerDescriptions)
ports := d.Get("instance_ports").(*schema.Set).List()
_, policyName := resourceAwsProxyProtocolPolicyParseId(d.Id())
inputs, err := resourceAwsProxyProtocolPolicyRemove(policyName, ports, backends)
if err != nil {
return fmt.Errorf("Error detaching a policy from backend: %s", err)
}
for _, input := range inputs {
input.LoadBalancerName = elbname
if _, err := elbconn.SetLoadBalancerPoliciesForBackendServer(input); err != nil {
return fmt.Errorf("Error setting policy for backend: %s", err)
}
}
pOpt := &elb.DeleteLoadBalancerPolicyInput{
LoadBalancerName: elbname,
PolicyName: aws.String(policyName),
}
if _, err := elbconn.DeleteLoadBalancerPolicy(pOpt); err != nil {
return fmt.Errorf("Error removing a policy from load balancer: %s", err)
}
return nil
}
func resourceAwsProxyProtocolPolicyRemove(policyName string, ports []interface{}, backends map[int64][]string) ([]*elb.SetLoadBalancerPoliciesForBackendServerInput, error) {
inputs := make([]*elb.SetLoadBalancerPoliciesForBackendServerInput, 0, len(ports))
for _, p := range ports {
ip, err := strconv.ParseInt(p.(string), 10, 64)
if err != nil {
return nil, fmt.Errorf("Error detaching the policy: %s", err)
}
newPolicies := []*string{}
curPolicies, found := backends[ip]
if !found {
// No policy for this instance port found, just skip it.
continue
}
for _, policy := range curPolicies {
if policy == policyName {
// remove the policy
continue
}
newPolicies = append(newPolicies, &policy)
}
inputs = append(inputs, &elb.SetLoadBalancerPoliciesForBackendServerInput{
InstancePort: &ip,
PolicyNames: newPolicies,
})
}
return inputs, nil
}
func resourceAwsProxyProtocolPolicyAdd(policyName string, ports []interface{}, backends map[int64][]string) ([]*elb.SetLoadBalancerPoliciesForBackendServerInput, error) {
inputs := make([]*elb.SetLoadBalancerPoliciesForBackendServerInput, 0, len(ports))
for _, p := range ports {
ip, err := strconv.ParseInt(p.(string), 10, 64)
if err != nil {
return nil, fmt.Errorf("Error attaching the policy: %s", err)
}
newPolicies := []*string{}
curPolicies := backends[ip]
for _, p := range curPolicies {
if p == policyName {
// Just remove it for now. It will be back later.
continue
} else {
newPolicies = append(newPolicies, &p)
}
}
newPolicies = append(newPolicies, aws.String(policyName))
inputs = append(inputs, &elb.SetLoadBalancerPoliciesForBackendServerInput{
InstancePort: &ip,
PolicyNames: newPolicies,
})
}
return inputs, nil
}
// resourceAwsProxyProtocolPolicyParseId takes an ID and parses it into
// it's constituent parts. You need two axes (LB name, policy name)
// to create or identify a proxy protocol policy in AWS's API.
func resourceAwsProxyProtocolPolicyParseId(id string) (string, string) {
parts := strings.SplitN(id, ":", 2)
return parts[0], parts[1]
}