forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodebalancer_configs.go
304 lines (270 loc) · 12.2 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
package linodego
import (
"context"
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
)
// 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"`
}
// endpoint gets the endpoint URL for NodeBalancerConfig
func (NodeBalancerConfigsPagedResponse) endpoint(ids ...any) string {
id := ids[0].(int)
return fmt.Sprintf("nodebalancers/%d/configs", id)
}
func (resp *NodeBalancerConfigsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(NodeBalancerConfigsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*NodeBalancerConfigsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListNodeBalancerConfigs lists NodeBalancerConfigs
func (c *Client) ListNodeBalancerConfigs(ctx context.Context, nodebalancerID int, opts *ListOptions) ([]NodeBalancerConfig, error) {
response := NodeBalancerConfigsPagedResponse{}
err := c.listHelper(ctx, &response, opts, nodebalancerID)
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 := fmt.Sprintf("nodebalancers/%d/configs/%d", nodebalancerID, configID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{})
r, err := coupleAPIErrors(req.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, opts NodeBalancerConfigCreateOptions) (*NodeBalancerConfig, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("nodebalancers/%d/configs", nodebalancerID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{}).SetBody(string(body))
r, err := coupleAPIErrors(req.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, opts NodeBalancerConfigUpdateOptions) (*NodeBalancerConfig, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("nodebalancers/%d/configs/%d", nodebalancerID, configID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{}).SetBody(string(body))
r, err := coupleAPIErrors(req.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 := fmt.Sprintf("nodebalancers/%d/configs/%d", nodebalancerID, 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, opts NodeBalancerConfigRebuildOptions) (*NodeBalancerConfig, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("nodebalancers/%d/configs/%d/rebuild", nodeBalancerID, configID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig), nil
}