forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodebalancer_configs.go
338 lines (298 loc) · 12.7 KB
/
nodebalancer_configs.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
package linodego
import (
"context"
"encoding/json"
"fmt"
)
// NodeBalancerConfig objects allow a NodeBalancer to accept traffic on a new port
type NodeBalancerConfig struct {
ID int `json:"id"`
Port int `json:"port"`
Protocol ConfigProtocol `json:"protocol"`
ProxyProtocol ConfigProxyProtocol `json:"proxy_protocol"`
Algorithm ConfigAlgorithm `json:"algorithm"`
Stickiness ConfigStickiness `json:"stickiness"`
Check ConfigCheck `json:"check"`
CheckInterval int `json:"check_interval"`
CheckAttempts int `json:"check_attempts"`
CheckPath string `json:"check_path"`
CheckBody string `json:"check_body"`
CheckPassive bool `json:"check_passive"`
CheckTimeout int `json:"check_timeout"`
CipherSuite ConfigCipher `json:"cipher_suite"`
NodeBalancerID int `json:"nodebalancer_id"`
SSLCommonName string `json:"ssl_commonname"`
SSLFingerprint string `json:"ssl_fingerprint"`
SSLCert string `json:"ssl_cert"`
SSLKey string `json:"ssl_key"`
NodesStatus *NodeBalancerNodeStatus `json:"nodes_status"`
}
// ConfigAlgorithm constants start with Algorithm and include Linode API NodeBalancer Config Algorithms
type ConfigAlgorithm string
// ConfigAlgorithm constants reflect the NodeBalancer Config Algorithm
const (
AlgorithmRoundRobin ConfigAlgorithm = "roundrobin"
AlgorithmLeastConn ConfigAlgorithm = "leastconn"
AlgorithmSource ConfigAlgorithm = "source"
)
// ConfigStickiness constants start with Stickiness and include Linode API NodeBalancer Config Stickiness
type ConfigStickiness string
// ConfigStickiness constants reflect the node stickiness method for a NodeBalancer Config
const (
StickinessNone ConfigStickiness = "none"
StickinessTable ConfigStickiness = "table"
StickinessHTTPCookie ConfigStickiness = "http_cookie"
)
// ConfigCheck constants start with Check and include Linode API NodeBalancer Config Check methods
type ConfigCheck string
// ConfigCheck constants reflect the node health status checking method for a NodeBalancer Config
const (
CheckNone ConfigCheck = "none"
CheckConnection ConfigCheck = "connection"
CheckHTTP ConfigCheck = "http"
CheckHTTPBody ConfigCheck = "http_body"
)
// ConfigProtocol constants start with Protocol and include Linode API Nodebalancer Config protocols
type ConfigProtocol string
// ConfigProtocol constants reflect the protocol used by a NodeBalancer Config
const (
ProtocolHTTP ConfigProtocol = "http"
ProtocolHTTPS ConfigProtocol = "https"
ProtocolTCP ConfigProtocol = "tcp"
)
// ConfigProxyProtocol constants start with ProxyProtocol and include Linode API NodeBalancer Config proxy protocol versions
type ConfigProxyProtocol string
// ConfigProxyProtocol constatns reflect the proxy protocol version used by a NodeBalancer Config
const (
ProxyProtocolNone ConfigProxyProtocol = "none"
ProxyProtocolV1 ConfigProxyProtocol = "v1"
ProxyProtocolV2 ConfigProxyProtocol = "v2"
)
// ConfigCipher constants start with Cipher and include Linode API NodeBalancer Config Cipher values
type ConfigCipher string
// ConfigCipher constants reflect the preferred cipher set for a NodeBalancer Config
const (
CipherRecommended ConfigCipher = "recommended"
CipherLegacy ConfigCipher = "legacy"
)
// NodeBalancerNodeStatus represents the total number of nodes whose status is Up or Down
type NodeBalancerNodeStatus struct {
Up int `json:"up"`
Down int `json:"down"`
}
// NodeBalancerConfigCreateOptions are permitted by CreateNodeBalancerConfig
type NodeBalancerConfigCreateOptions struct {
Port int `json:"port"`
Protocol ConfigProtocol `json:"protocol,omitempty"`
ProxyProtocol ConfigProxyProtocol `json:"proxy_protocol,omitempty"`
Algorithm ConfigAlgorithm `json:"algorithm,omitempty"`
Stickiness ConfigStickiness `json:"stickiness,omitempty"`
Check ConfigCheck `json:"check,omitempty"`
CheckInterval int `json:"check_interval,omitempty"`
CheckAttempts int `json:"check_attempts,omitempty"`
CheckPath string `json:"check_path,omitempty"`
CheckBody string `json:"check_body,omitempty"`
CheckPassive *bool `json:"check_passive,omitempty"`
CheckTimeout int `json:"check_timeout,omitempty"`
CipherSuite ConfigCipher `json:"cipher_suite,omitempty"`
SSLCert string `json:"ssl_cert,omitempty"`
SSLKey string `json:"ssl_key,omitempty"`
Nodes []NodeBalancerNodeCreateOptions `json:"nodes,omitempty"`
}
// NodeBalancerConfigRebuildOptions used by RebuildNodeBalancerConfig
type NodeBalancerConfigRebuildOptions struct {
Port int `json:"port"`
Protocol ConfigProtocol `json:"protocol,omitempty"`
ProxyProtocol ConfigProxyProtocol `json:"proxy_protocol,omitempty"`
Algorithm ConfigAlgorithm `json:"algorithm,omitempty"`
Stickiness ConfigStickiness `json:"stickiness,omitempty"`
Check ConfigCheck `json:"check,omitempty"`
CheckInterval int `json:"check_interval,omitempty"`
CheckAttempts int `json:"check_attempts,omitempty"`
CheckPath string `json:"check_path,omitempty"`
CheckBody string `json:"check_body,omitempty"`
CheckPassive *bool `json:"check_passive,omitempty"`
CheckTimeout int `json:"check_timeout,omitempty"`
CipherSuite ConfigCipher `json:"cipher_suite,omitempty"`
SSLCert string `json:"ssl_cert,omitempty"`
SSLKey string `json:"ssl_key,omitempty"`
Nodes []NodeBalancerNodeCreateOptions `json:"nodes"`
}
// NodeBalancerConfigUpdateOptions are permitted by UpdateNodeBalancerConfig
type NodeBalancerConfigUpdateOptions NodeBalancerConfigCreateOptions
// GetCreateOptions converts a NodeBalancerConfig to NodeBalancerConfigCreateOptions for use in CreateNodeBalancerConfig
func (i NodeBalancerConfig) GetCreateOptions() NodeBalancerConfigCreateOptions {
return NodeBalancerConfigCreateOptions{
Port: i.Port,
Protocol: i.Protocol,
ProxyProtocol: i.ProxyProtocol,
Algorithm: i.Algorithm,
Stickiness: i.Stickiness,
Check: i.Check,
CheckInterval: i.CheckInterval,
CheckAttempts: i.CheckAttempts,
CheckTimeout: i.CheckTimeout,
CheckPath: i.CheckPath,
CheckBody: i.CheckBody,
CheckPassive: copyBool(&i.CheckPassive),
CipherSuite: i.CipherSuite,
SSLCert: i.SSLCert,
SSLKey: i.SSLKey,
}
}
// GetUpdateOptions converts a NodeBalancerConfig to NodeBalancerConfigUpdateOptions for use in UpdateNodeBalancerConfig
func (i NodeBalancerConfig) GetUpdateOptions() NodeBalancerConfigUpdateOptions {
return NodeBalancerConfigUpdateOptions{
Port: i.Port,
Protocol: i.Protocol,
ProxyProtocol: i.ProxyProtocol,
Algorithm: i.Algorithm,
Stickiness: i.Stickiness,
Check: i.Check,
CheckInterval: i.CheckInterval,
CheckAttempts: i.CheckAttempts,
CheckPath: i.CheckPath,
CheckBody: i.CheckBody,
CheckPassive: copyBool(&i.CheckPassive),
CheckTimeout: i.CheckTimeout,
CipherSuite: i.CipherSuite,
SSLCert: i.SSLCert,
SSLKey: i.SSLKey,
}
}
// GetRebuildOptions converts a NodeBalancerConfig to NodeBalancerConfigRebuildOptions for use in RebuildNodeBalancerConfig
func (i NodeBalancerConfig) GetRebuildOptions() NodeBalancerConfigRebuildOptions {
return NodeBalancerConfigRebuildOptions{
Port: i.Port,
Protocol: i.Protocol,
ProxyProtocol: i.ProxyProtocol,
Algorithm: i.Algorithm,
Stickiness: i.Stickiness,
Check: i.Check,
CheckInterval: i.CheckInterval,
CheckAttempts: i.CheckAttempts,
CheckTimeout: i.CheckTimeout,
CheckPath: i.CheckPath,
CheckBody: i.CheckBody,
CheckPassive: copyBool(&i.CheckPassive),
CipherSuite: i.CipherSuite,
SSLCert: i.SSLCert,
SSLKey: i.SSLKey,
Nodes: make([]NodeBalancerNodeCreateOptions, 0),
}
}
// NodeBalancerConfigsPagedResponse represents a paginated NodeBalancerConfig API response
type NodeBalancerConfigsPagedResponse struct {
*PageOptions
Data []NodeBalancerConfig `json:"data"`
}
// endpointWithID gets the endpoint URL for NodeBalancerConfig
func (NodeBalancerConfigsPagedResponse) endpointWithID(c *Client, id int) string {
endpoint, err := c.NodeBalancerConfigs.endpointWithParams(id)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends NodeBalancerConfigs when processing paginated NodeBalancerConfig responses
func (resp *NodeBalancerConfigsPagedResponse) appendData(r *NodeBalancerConfigsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListNodeBalancerConfigs lists NodeBalancerConfigs
func (c *Client) ListNodeBalancerConfigs(ctx context.Context, nodebalancerID int, opts *ListOptions) ([]NodeBalancerConfig, error) {
response := NodeBalancerConfigsPagedResponse{}
err := c.listHelperWithID(ctx, &response, nodebalancerID, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetNodeBalancerConfig gets the template with the provided ID
func (c *Client) GetNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int) (*NodeBalancerConfig, error) {
e, err := c.NodeBalancerConfigs.endpointWithParams(nodebalancerID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, configID)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&NodeBalancerConfig{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig), nil
}
// CreateNodeBalancerConfig creates a NodeBalancerConfig
func (c *Client) CreateNodeBalancerConfig(ctx context.Context, nodebalancerID int, nodebalancerConfig NodeBalancerConfigCreateOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.endpointWithParams(nodebalancerID)
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&NodeBalancerConfig{})
if bodyData, err := json.Marshal(nodebalancerConfig); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig), nil
}
// UpdateNodeBalancerConfig updates the NodeBalancerConfig with the specified id
func (c *Client) UpdateNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int, updateOpts NodeBalancerConfigUpdateOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.endpointWithParams(nodebalancerID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, configID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig), nil
}
// DeleteNodeBalancerConfig deletes the NodeBalancerConfig with the specified id
func (c *Client) DeleteNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int) error {
e, err := c.NodeBalancerConfigs.endpointWithParams(nodebalancerID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, configID)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}
// RebuildNodeBalancerConfig updates the NodeBalancer with the specified id
func (c *Client) RebuildNodeBalancerConfig(ctx context.Context, nodeBalancerID int, configID int, rebuildOpts NodeBalancerConfigRebuildOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.endpointWithParams(nodeBalancerID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d/rebuild", e, configID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{})
if bodyData, err := json.Marshal(rebuildOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig), nil
}