forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadbalancer.go
257 lines (203 loc) · 7.02 KB
/
loadbalancer.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
package digitalocean
import (
"bytes"
"context"
"fmt"
"log"
"strings"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func loadbalancerStateRefreshFunc(client *godo.Client, loadbalancerId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
lb, _, err := client.LoadBalancers.Get(context.Background(), loadbalancerId)
if err != nil {
return nil, "", fmt.Errorf("Error issuing read request in LoadbalancerStateRefreshFunc to DigitalOcean for Load Balancer '%s': %s", loadbalancerId, err)
}
return lb, lb.Status, nil
}
}
func expandStickySessions(config []interface{}) *godo.StickySessions {
stickysessionConfig := config[0].(map[string]interface{})
stickySession := &godo.StickySessions{
Type: stickysessionConfig["type"].(string),
}
if v, ok := stickysessionConfig["cookie_name"]; ok {
stickySession.CookieName = v.(string)
}
if v, ok := stickysessionConfig["cookie_ttl_seconds"]; ok {
stickySession.CookieTtlSeconds = v.(int)
}
return stickySession
}
func expandLBFirewall(config []interface{}) *godo.LBFirewall {
firewallConfig := config[0].(map[string]interface{})
firewall := &godo.LBFirewall{}
if v, ok := firewallConfig["allow"]; ok {
firewall.Allow = v.([]string)
}
if v, ok := firewallConfig["deny"]; ok {
firewall.Deny = v.([]string)
}
return firewall
}
func expandHealthCheck(config []interface{}) *godo.HealthCheck {
healthcheckConfig := config[0].(map[string]interface{})
healthcheck := &godo.HealthCheck{
Protocol: healthcheckConfig["protocol"].(string),
Port: healthcheckConfig["port"].(int),
CheckIntervalSeconds: healthcheckConfig["check_interval_seconds"].(int),
ResponseTimeoutSeconds: healthcheckConfig["response_timeout_seconds"].(int),
UnhealthyThreshold: healthcheckConfig["unhealthy_threshold"].(int),
HealthyThreshold: healthcheckConfig["healthy_threshold"].(int),
}
if v, ok := healthcheckConfig["path"]; ok {
healthcheck.Path = v.(string)
}
return healthcheck
}
func expandForwardingRules(client *godo.Client, config []interface{}) ([]godo.ForwardingRule, error) {
forwardingRules := make([]godo.ForwardingRule, 0, len(config))
for _, rawRule := range config {
rule := rawRule.(map[string]interface{})
r := godo.ForwardingRule{
EntryPort: rule["entry_port"].(int),
EntryProtocol: rule["entry_protocol"].(string),
TargetPort: rule["target_port"].(int),
TargetProtocol: rule["target_protocol"].(string),
TlsPassthrough: rule["tls_passthrough"].(bool),
}
if name, nameOk := rule["certificate_name"]; nameOk {
certName := name.(string)
if certName != "" {
cert, err := findCertificateByName(client, certName)
if err != nil {
return nil, err
}
r.CertificateID = cert.ID
}
}
if id, idOk := rule["certificate_id"]; idOk && r.CertificateID == "" {
// When the certificate type is lets_encrypt, the certificate
// ID will change when it's renewed, so we have to rely on the
// certificate name as the primary identifier instead.
certName := id.(string)
if certName != "" {
cert, err := findCertificateByName(client, certName)
if err != nil {
if strings.Contains(err.Error(), "not found") {
log.Println("[DEBUG] Certificate not found looking up by name. Falling back to lookup by ID.")
cert, _, err = client.Certificates.Get(context.Background(), certName)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
r.CertificateID = cert.ID
}
}
forwardingRules = append(forwardingRules, r)
}
return forwardingRules, nil
}
func hashForwardingRules(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%d-", m["entry_port"].(int)))
buf.WriteString(fmt.Sprintf("%s-",
strings.ToLower(m["entry_protocol"].(string))))
buf.WriteString(fmt.Sprintf("%d-", m["target_port"].(int)))
buf.WriteString(fmt.Sprintf("%s-",
strings.ToLower(m["target_protocol"].(string))))
if v, ok := m["certificate_id"]; ok {
if v.(string) == "" {
if name, nameOk := m["certificate_name"]; nameOk {
buf.WriteString(fmt.Sprintf("%s-", name.(string)))
}
} else {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
}
if v, ok := m["tls_passthrough"]; ok {
buf.WriteString(fmt.Sprintf("%t-", v.(bool)))
}
return SDKHashString(buf.String())
}
func flattenDropletIds(list []int) *schema.Set {
flatSet := schema.NewSet(schema.HashInt, []interface{}{})
for _, v := range list {
flatSet.Add(v)
}
return flatSet
}
func flattenHealthChecks(health *godo.HealthCheck) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
if health != nil {
r := make(map[string]interface{})
r["protocol"] = (*health).Protocol
r["port"] = (*health).Port
r["path"] = (*health).Path
r["check_interval_seconds"] = (*health).CheckIntervalSeconds
r["response_timeout_seconds"] = (*health).ResponseTimeoutSeconds
r["unhealthy_threshold"] = (*health).UnhealthyThreshold
r["healthy_threshold"] = (*health).HealthyThreshold
result = append(result, r)
}
return result
}
func flattenStickySessions(session *godo.StickySessions) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
if session != nil {
r := make(map[string]interface{})
r["type"] = (*session).Type
r["cookie_name"] = (*session).CookieName
r["cookie_ttl_seconds"] = (*session).CookieTtlSeconds
result = append(result, r)
}
return result
}
func flattenLBFirewall(firewall *godo.LBFirewall) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
if firewall != nil {
r := make(map[string]interface{})
// TODO: Unsure if nested rules requiring flattening? (jrolheiser)
r["allow"] = flattenFirewallRules((*firewall).Allow)
r["deny"] = flattenFirewallRules((*firewall).Deny)
result = append(result, r)
}
return result
}
func flattenFirewallRules(rules []string) *schema.Set {
flatSet := schema.NewSet(schema.HashString, []interface{}{})
for _, v := range rules {
flatSet.Add(v)
}
return flatSet
}
func flattenForwardingRules(client *godo.Client, rules []godo.ForwardingRule) ([]map[string]interface{}, error) {
result := make([]map[string]interface{}, 0, 1)
for _, rule := range rules {
r := make(map[string]interface{})
r["entry_protocol"] = rule.EntryProtocol
r["entry_port"] = rule.EntryPort
r["target_protocol"] = rule.TargetProtocol
r["target_port"] = rule.TargetPort
r["tls_passthrough"] = rule.TlsPassthrough
if rule.CertificateID != "" {
// When the certificate type is lets_encrypt, the certificate
// ID will change when it's renewed, so we have to rely on the
// certificate name as the primary identifier instead.
cert, _, err := client.Certificates.Get(context.Background(), rule.CertificateID)
if err != nil {
return nil, err
}
r["certificate_id"] = cert.Name
r["certificate_name"] = cert.Name
}
result = append(result, r)
}
return result, nil
}