-
Notifications
You must be signed in to change notification settings - Fork 56
/
load_balancer.go
350 lines (294 loc) · 12.3 KB
/
load_balancer.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
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const lbPath = "/v2/load-balancers"
// LoadBalancerService is the interface to interact with the server endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/load-balancer
type LoadBalancerService interface {
Create(ctx context.Context, createReq *LoadBalancerReq) (*LoadBalancer, *http.Response, error)
Get(ctx context.Context, lbID string) (*LoadBalancer, *http.Response, error)
Update(ctx context.Context, lbID string, updateReq *LoadBalancerReq) error
Delete(ctx context.Context, lbID string) error
List(ctx context.Context, options *ListOptions) ([]LoadBalancer, *Meta, *http.Response, error)
CreateForwardingRule(ctx context.Context, lbID string, rule *ForwardingRule) (*ForwardingRule, *http.Response, error)
GetForwardingRule(ctx context.Context, lbID string, ruleID string) (*ForwardingRule, *http.Response, error)
DeleteForwardingRule(ctx context.Context, lbID string, RuleID string) error
ListForwardingRules(ctx context.Context, lbID string, options *ListOptions) ([]ForwardingRule, *Meta, *http.Response, error)
ListFirewallRules(ctx context.Context, lbID string, options *ListOptions) ([]LBFirewallRule, *Meta, *http.Response, error)
GetFirewallRule(ctx context.Context, lbID string, ruleID string) (*LBFirewallRule, *http.Response, error)
}
// LoadBalancerHandler handles interaction with the server methods for the Vultr API
type LoadBalancerHandler struct {
client *Client
}
// LoadBalancer represent the structure of a load balancer
type LoadBalancer struct {
ID string `json:"id,omitempty"`
DateCreated string `json:"date_created,omitempty"`
Region string `json:"region,omitempty"`
Label string `json:"label,omitempty"`
Status string `json:"status,omitempty"`
IPV4 string `json:"ipv4,omitempty"`
IPV6 string `json:"ipv6,omitempty"`
Instances []string `json:"instances,omitempty"`
Nodes int `json:"nodes,omitempty"`
HealthCheck *HealthCheck `json:"health_check,omitempty"`
GenericInfo *GenericInfo `json:"generic_info,omitempty"`
SSLInfo *bool `json:"has_ssl,omitempty"`
HTTP2 *bool `json:"http2,omitempty"`
HTTP3 *bool `json:"http3,omitempty"`
ForwardingRules []ForwardingRule `json:"forwarding_rules,omitempty"`
FirewallRules []LBFirewallRule `json:"firewall_rules,omitempty"`
}
// LoadBalancerReq gives options for creating or updating a load balancer
type LoadBalancerReq struct {
Region string `json:"region,omitempty"`
Label string `json:"label,omitempty"`
Instances []string `json:"instances,omitempty"`
Nodes int `json:"nodes,omitempty"`
HealthCheck *HealthCheck `json:"health_check,omitempty"`
StickySessions *StickySessions `json:"sticky_session,omitempty"`
ForwardingRules []ForwardingRule `json:"forwarding_rules,omitempty"`
SSL *SSL `json:"ssl,omitempty"`
SSLRedirect *bool `json:"ssl_redirect,omitempty"`
HTTP2 *bool `json:"http2,omitempty"`
HTTP3 *bool `json:"http3,omitempty"`
ProxyProtocol *bool `json:"proxy_protocol,omitempty"`
BalancingAlgorithm string `json:"balancing_algorithm,omitempty"`
FirewallRules []LBFirewallRule `json:"firewall_rules,omitempty"`
Timeout int `json:"timeout,omitempty"`
VPC *string `json:"vpc,omitempty"`
}
// InstanceList represents instances that are attached to your load balancer
type InstanceList struct {
InstanceList []string
}
// HealthCheck represents your health check configuration for your load balancer.
type HealthCheck struct {
Protocol string `json:"protocol,omitempty"`
Port int `json:"port,omitempty"`
Path string `json:"path,omitempty"`
CheckInterval int `json:"check_interval,omitempty"`
ResponseTimeout int `json:"response_timeout,omitempty"`
UnhealthyThreshold int `json:"unhealthy_threshold,omitempty"`
HealthyThreshold int `json:"healthy_threshold,omitempty"`
}
// GenericInfo represents generic configuration of your load balancer
type GenericInfo struct {
BalancingAlgorithm string `json:"balancing_algorithm,omitempty"`
Timeout int `json:"timeout,omitempty"`
SSLRedirect *bool `json:"ssl_redirect,omitempty"`
StickySessions *StickySessions `json:"sticky_sessions,omitempty"`
ProxyProtocol *bool `json:"proxy_protocol,omitempty"`
VPC string `json:"vpc,omitempty"`
}
// StickySessions represents cookie for your load balancer
type StickySessions struct {
CookieName string `json:"cookie_name,omitempty"`
}
// ForwardingRules represent a list of forwarding rules
type ForwardingRules struct {
ForwardRuleList []ForwardingRule `json:"forwarding_rules,omitempty"`
}
// ForwardingRule represent a single forwarding rule
type ForwardingRule struct {
RuleID string `json:"id,omitempty"`
FrontendProtocol string `json:"frontend_protocol,omitempty"`
FrontendPort int `json:"frontend_port,omitempty"`
BackendProtocol string `json:"backend_protocol,omitempty"`
BackendPort int `json:"backend_port,omitempty"`
}
// LBFirewallRule represent a single firewall rule
type LBFirewallRule struct {
RuleID string `json:"id,omitempty"`
Port int `json:"port,omitempty"`
IPType string `json:"ip_type,omitempty"`
Source string `json:"source,omitempty"`
}
// SSL represents valid SSL config
type SSL struct {
PrivateKey string `json:"private_key,omitempty"`
Certificate string `json:"certificate,omitempty"`
Chain string `json:"chain,omitempty"`
}
type lbsBase struct {
LoadBalancers []LoadBalancer `json:"load_balancers"`
Meta *Meta `json:"meta"`
}
type lbBase struct {
LoadBalancer *LoadBalancer `json:"load_balancer"`
}
type lbRulesBase struct {
ForwardingRules []ForwardingRule `json:"forwarding_rules"`
Meta *Meta `json:"meta"`
}
type lbRuleBase struct {
ForwardingRule *ForwardingRule `json:"forwarding_rule"`
}
type lbFWRulesBase struct {
FirewallRules []LBFirewallRule `json:"firewall_rules"`
Meta *Meta `json:"meta"`
}
type lbFWRuleBase struct {
FirewallRule *LBFirewallRule `json:"firewall_rule"`
}
// Create a load balancer
func (l *LoadBalancerHandler) Create(ctx context.Context, createReq *LoadBalancerReq) (*LoadBalancer, *http.Response, error) {
req, err := l.client.NewRequest(ctx, http.MethodPost, lbPath, createReq)
if err != nil {
return nil, nil, err
}
var lb = new(lbBase)
resp, err := l.client.DoWithContext(ctx, req, &lb)
if err != nil {
return nil, resp, err
}
return lb.LoadBalancer, resp, nil
}
// Get a load balancer
func (l *LoadBalancerHandler) Get(ctx context.Context, lbID string) (*LoadBalancer, *http.Response, error) {
uri := fmt.Sprintf("%s/%s", lbPath, lbID)
req, err := l.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
var lb = new(lbBase)
resp, err := l.client.DoWithContext(ctx, req, lb)
if err != nil {
return nil, resp, err
}
return lb.LoadBalancer, resp, nil
}
// Update updates your your load balancer
func (l *LoadBalancerHandler) Update(ctx context.Context, lbID string, updateReq *LoadBalancerReq) error {
uri := fmt.Sprintf("%s/%s", lbPath, lbID)
req, err := l.client.NewRequest(ctx, http.MethodPatch, uri, updateReq)
if err != nil {
return err
}
_, err = l.client.DoWithContext(ctx, req, nil)
return err
}
// Delete a load balancer subscription.
func (l *LoadBalancerHandler) Delete(ctx context.Context, lbID string) error {
uri := fmt.Sprintf("%s/%s", lbPath, lbID)
req, err := l.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = l.client.DoWithContext(ctx, req, nil)
return err
}
// List all load balancer subscriptions on the current account.
func (l *LoadBalancerHandler) List(ctx context.Context, options *ListOptions) ([]LoadBalancer, *Meta, *http.Response, error) { //nolint:dupl
req, err := l.client.NewRequest(ctx, http.MethodGet, lbPath, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
lbs := new(lbsBase)
resp, err := l.client.DoWithContext(ctx, req, &lbs)
if err != nil {
return nil, nil, resp, err
}
return lbs.LoadBalancers, lbs.Meta, resp, nil
}
// CreateForwardingRule will create a new forwarding rule for your load balancer subscription.
// Note the RuleID will be returned in the ForwardingRule struct
func (l *LoadBalancerHandler) CreateForwardingRule(ctx context.Context, lbID string, rule *ForwardingRule) (*ForwardingRule, *http.Response, error) { //nolint:lll
uri := fmt.Sprintf("%s/%s/forwarding-rules", lbPath, lbID)
req, err := l.client.NewRequest(ctx, http.MethodPost, uri, rule)
if err != nil {
return nil, nil, err
}
fwRule := new(lbRuleBase)
resp, err := l.client.DoWithContext(ctx, req, fwRule)
if err != nil {
return nil, resp, err
}
return fwRule.ForwardingRule, resp, nil
}
// GetForwardingRule will get a forwarding rule from your load balancer subscription.
func (l *LoadBalancerHandler) GetForwardingRule(ctx context.Context, lbID, ruleID string) (*ForwardingRule, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/forwarding-rules/%s", lbPath, lbID, ruleID)
req, err := l.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
fwRule := new(lbRuleBase)
resp, err := l.client.DoWithContext(ctx, req, fwRule)
if err != nil {
return nil, resp, err
}
return fwRule.ForwardingRule, resp, nil
}
// ListForwardingRules lists all forwarding rules for a load balancer subscription
func (l *LoadBalancerHandler) ListForwardingRules(ctx context.Context, lbID string, options *ListOptions) ([]ForwardingRule, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := fmt.Sprintf("%s/%s/forwarding-rules", lbPath, lbID)
req, err := l.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
fwRules := new(lbRulesBase)
resp, err := l.client.DoWithContext(ctx, req, &fwRules)
if err != nil {
return nil, nil, resp, err
}
return fwRules.ForwardingRules, fwRules.Meta, resp, nil
}
// DeleteForwardingRule removes a forwarding rule from a load balancer subscription
func (l *LoadBalancerHandler) DeleteForwardingRule(ctx context.Context, lbID, ruleID string) error {
uri := fmt.Sprintf("%s/%s/forwarding-rules/%s", lbPath, lbID, ruleID)
req, err := l.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = l.client.DoWithContext(ctx, req, nil)
return err
}
// GetFirewallRule will get a firewall rule from your load balancer subscription.
func (l *LoadBalancerHandler) GetFirewallRule(ctx context.Context, lbID, ruleID string) (*LBFirewallRule, *http.Response, error) {
uri := fmt.Sprintf("%s/%s/firewall-rules/%s", lbPath, lbID, ruleID)
req, err := l.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
fwRule := new(lbFWRuleBase)
resp, err := l.client.DoWithContext(ctx, req, fwRule)
if err != nil {
return nil, resp, err
}
return fwRule.FirewallRule, resp, nil
}
// ListFirewallRules lists all firewall rules for a load balancer subscription
func (l *LoadBalancerHandler) ListFirewallRules(ctx context.Context, lbID string, options *ListOptions) ([]LBFirewallRule, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := fmt.Sprintf("%s/%s/firewall-rules", lbPath, lbID)
req, err := l.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
fwRules := new(lbFWRulesBase)
resp, err := l.client.DoWithContext(ctx, req, &fwRules)
if err != nil {
return nil, nil, resp, err
}
return fwRules.FirewallRules, fwRules.Meta, resp, nil
}