-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathload_balancers.go
451 lines (377 loc) · 16.4 KB
/
load_balancers.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
"github.com/digitalocean/doctl/do"
"github.com/digitalocean/godo"
"github.com/spf13/cobra"
)
// LoadBalancer creates the load balancer command.
func LoadBalancer() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "load-balancer",
Short: "Display commands to manage load balancers",
Long: `The sub-commands of ` + "`" + `doctl compute load-balancer` + "`" + ` manage your load balancers.
With the load-balancer command, you can list, create, or delete load balancers, and manage their configuration details.`,
},
}
lbDetail := `
- The load balancer's ID
- The load balancer's name
- The load balancer's IP address
- The load balancer's traffic algorithm. Must
be either ` + "`" + `round_robin` + "`" + ` or ` + "`" + `least_connections` + "`" + `
- The current state of the load balancer. This can be ` + "`" + `new` + "`" + `, ` + "`" + `active` + "`" + `, or ` + "`" + `errored` + "`" + `.
- The load balancer's creation date, in ISO8601 combined date and time format.
- The load balancer's forwarding rules. See ` + "`" + `doctl compute load-balancer add-forwarding-rules --help` + "`" + ` for a list.
- The ` + "`" + `health_check` + "`" + ` settings for the load balancer.
- The ` + "`" + `sticky_sessions` + "`" + ` settings for the load balancer.
- The datacenter region the load balancer is located in.
- The Droplet tag corresponding to the Droplets assigned to the load balancer.
- The IDs of the Droplets assigned to the load balancer.
- Whether HTTP request to the load balancer on port 80 will be redirected to HTTPS on port 443.
- Whether the PROXY protocol is in use on the load balancer.
`
forwardingDetail := `
- ` + "`" + `entry_protocol` + "`" + `: The entry protocol used for traffic to the load balancer. Possible values are: ` + "`" + `http` + "`" + `, ` + "`" + `https` + "`" + `, ` + "`" + `http2` + "`" + `, or ` + "`" + `tcp` + "`" + `.
- ` + "`" + `entry_port` + "`" + `: The entry port used for incoming traffic to the load balancer.
- ` + "`" + `target_protocol` + "`" + `: The target protocol used for traffic from the load balancer to the backend Droplets. Possible values are: ` + "`" + `http` + "`" + `, ` + "`" + `https` + "`" + `, ` + "`" + `http2` + "`" + `, or ` + "`" + `tcp` + "`" + `.
- ` + "`" + `target_port` + "`" + `: The target port used to send traffic from the load balancer to the backend Droplets.
- ` + "`" + `certificate_id` + "`" + `: The ID of the TLS certificate used for SSL termination, if enabled. Can be obtained with ` + "`" + `doctl certificate list` + "`" + `
- ` + "`" + `tls_passthrough` + "`" + `: Whether SSL passthrough is enabled on the load balancer.
`
forwardingRulesTxt := "A comma-separated list of key-value pairs representing forwarding rules, which define how traffic is routed, e.g.: `entry_protocol:tcp, entry_port:3306, target_protocol:tcp, target_port:3306`. Use a quoted string of space-separated values for multiple rules"
CmdBuilderWithDocs(cmd, RunLoadBalancerGet, "get <id>", "Retrieve a load balancer", "Use this command to retrieve information about a load balancer instance, including:"+lbDetail, Writer,
aliasOpt("g"), displayerType(&displayers.LoadBalancer{}))
cmdRecordCreate := CmdBuilderWithDocs(cmd, RunLoadBalancerCreate, "create",
"Create a new load balancer", "Use this command to create a new load balancer on your account. Valid forwarding rules are:"+forwardingDetail, Writer, aliasOpt("c"))
AddStringFlag(cmdRecordCreate, doctl.ArgLoadBalancerName, "", "",
"The load balancer's name", requiredOpt())
AddStringFlag(cmdRecordCreate, doctl.ArgRegionSlug, "", "",
"The load balancer's region, e.g.: `nyc1`", requiredOpt())
AddStringFlag(cmdRecordCreate, doctl.ArgLoadBalancerAlgorithm, "",
"round_robin", "The algorithm to use when traffic is distributed across your Droplets; possible values: `round_robin` or `least_connections`")
AddBoolFlag(cmdRecordCreate, doctl.ArgRedirectHttpToHttps, "", false,
"Redirects HTTP requests to the load balancer on port 80 to HTTPS on port 443")
AddStringFlag(cmdRecordCreate, doctl.ArgTagName, "", "", "droplet tag name")
AddStringSliceFlag(cmdRecordCreate, doctl.ArgDropletIDs, "", []string{},
"A comma-separated list of Droplet IDs to add to the load balancer, e.g.: `12,33`")
AddStringFlag(cmdRecordCreate, doctl.ArgStickySessions, "", "",
"A comma-separated list of key-value pairs representing a list of active sessions, e.g.: `type:cookies, cookie_name:DO-LB, cookie_ttl_seconds:5`")
AddStringFlag(cmdRecordCreate, doctl.ArgHealthCheck, "", "",
"A comma-separated list of key-value pairs representing recent health check results, e.g.: `protocol:http, port:80, path:/index.html, check_interval_seconds:10, response_timeout_seconds:5, healthy_threshold:5, unhealthy_threshold:3`")
AddStringFlag(cmdRecordCreate, doctl.ArgForwardingRules, "", "",
forwardingRulesTxt)
cmdRecordUpdate := CmdBuilderWithDocs(cmd, RunLoadBalancerUpdate, "update <id>",
"Update a load balancer's configuration", `Use this command to update the configuration of a specified load balancer.`, Writer, aliasOpt("u"))
AddStringFlag(cmdRecordUpdate, doctl.ArgLoadBalancerName, "", "",
"The load balancer's name", requiredOpt())
AddStringFlag(cmdRecordUpdate, doctl.ArgRegionSlug, "", "",
"The load balancer's region, e.g.: `nyc1`", requiredOpt())
AddStringFlag(cmdRecordUpdate, doctl.ArgLoadBalancerAlgorithm, "",
"round_robin", "The algorithm to use when traffic is distributed across your Droplets; possible values: `round_robin` or `least_connections`")
AddBoolFlag(cmdRecordUpdate, doctl.ArgRedirectHttpToHttps, "", false,
"Flag to redirect HTTP requests to the load balancer on port 80 to HTTPS on port 443")
AddStringFlag(cmdRecordUpdate, doctl.ArgTagName, "", "", "Assigns Droplets with the specified tag to the load balancer")
AddStringSliceFlag(cmdRecordUpdate, doctl.ArgDropletIDs, "", []string{},
"A comma-separated list of Droplet IDs, e.g.: `215,378`")
AddStringFlag(cmdRecordUpdate, doctl.ArgStickySessions, "", "",
"A comma-separated list of key-value pairs representing a list of active sessions, e.g.: `type:cookies, cookie_name:DO-LB, cookie_ttl_seconds:5`")
AddStringFlag(cmdRecordUpdate, doctl.ArgHealthCheck, "", "",
"A comma-separated list of key-value pairs representing recent health check results, e.g.: `protocol:http, port:80, path:/index.html, check_interval_seconds:10, response_timeout_seconds:5, healthy_threshold:5, unhealthy_threshold:3`")
AddStringFlag(cmdRecordUpdate, doctl.ArgForwardingRules, "", "", forwardingRulesTxt)
CmdBuilderWithDocs(cmd, RunLoadBalancerList, "list", "List load balancers", "Use this command to get a list of the load balancers on your account, including the following information for each:"+lbDetail, Writer,
aliasOpt("ls"), displayerType(&displayers.LoadBalancer{}))
cmdRunRecordDelete := CmdBuilderWithDocs(cmd, RunLoadBalancerDelete, "delete <id>",
"Permanently delete a load balancer", `Use this command to permanently delete the speicified load balancer. This is irreversable.`, Writer, aliasOpt("d", "rm"))
AddBoolFlag(cmdRunRecordDelete, doctl.ArgForce, doctl.ArgShortForce, false,
"Delete the load balancer without a confirmation prompt")
cmdAddDroplets := CmdBuilderWithDocs(cmd, RunLoadBalancerAddDroplets, "add-droplets <id>",
"Add Droplets to a load balancer", `Use this command to add Droplets to a load balancer.`, Writer)
AddStringSliceFlag(cmdAddDroplets, doctl.ArgDropletIDs, "", []string{},
"A comma-separated list of IDs of Droplet to add to the load balancer, example value: `12,33`")
cmdRemoveDroplets := CmdBuilderWithDocs(cmd, RunLoadBalancerRemoveDroplets,
"remove-droplets <id>", "Remove Droplets from a load balancer", `Use this command to remove Droplets from a load balancer. This command does not destroy any Droplets.`, Writer)
AddStringSliceFlag(cmdRemoveDroplets, doctl.ArgDropletIDs, "", []string{},
"A comma-separated list of IDs of Droplets to remove from the load balancer, example value: `12,33`")
cmdAddForwardingRules := CmdBuilderWithDocs(cmd, RunLoadBalancerAddForwardingRules,
"add-forwarding-rules <id>", "Add forwarding rules to a load balancer", "Use this command to add forwarding rules to a load balancer, specified with the `--forwarding-rules` flag. Valid rules include:"+forwardingDetail, Writer)
AddStringFlag(cmdAddForwardingRules, doctl.ArgForwardingRules, "", "", forwardingRulesTxt)
cmdRemoveForwardingRules := CmdBuilderWithDocs(cmd, RunLoadBalancerRemoveForwardingRules,
"remove-forwarding-rules <id>", "Remove forwarding rules from a load balancer", "Use this command to remove forwarding rules from a load balancer, specified with the `--forwarding-rules` flag. Valid rules include:"+forwardingDetail, Writer)
AddStringFlag(cmdRemoveForwardingRules, doctl.ArgForwardingRules, "", "", forwardingRulesTxt)
return cmd
}
// RunLoadBalancerGet retrieves an existing load balancer by its identifier.
func RunLoadBalancerGet(c *CmdConfig) error {
if len(c.Args) != 1 {
return doctl.NewMissingArgsErr(c.NS)
}
id := c.Args[0]
lbs := c.LoadBalancers()
lb, err := lbs.Get(id)
if err != nil {
return err
}
item := &displayers.LoadBalancer{LoadBalancers: do.LoadBalancers{*lb}}
return c.Display(item)
}
// RunLoadBalancerList lists load balancers.
func RunLoadBalancerList(c *CmdConfig) error {
lbs := c.LoadBalancers()
list, err := lbs.List()
if err != nil {
return err
}
item := &displayers.LoadBalancer{LoadBalancers: list}
return c.Display(item)
}
// RunLoadBalancerCreate creates a new load balancer with a given configuration.
func RunLoadBalancerCreate(c *CmdConfig) error {
r := new(godo.LoadBalancerRequest)
if err := buildRequestFromArgs(c, r); err != nil {
return err
}
lbs := c.LoadBalancers()
lb, err := lbs.Create(r)
if err != nil {
return err
}
item := &displayers.LoadBalancer{LoadBalancers: do.LoadBalancers{*lb}}
return c.Display(item)
}
// RunLoadBalancerUpdate updates an existing load balancer with new configuration.
func RunLoadBalancerUpdate(c *CmdConfig) error {
if len(c.Args) == 0 {
return doctl.NewMissingArgsErr(c.NS)
}
lbID := c.Args[0]
r := new(godo.LoadBalancerRequest)
if err := buildRequestFromArgs(c, r); err != nil {
return err
}
lbs := c.LoadBalancers()
lb, err := lbs.Update(lbID, r)
if err != nil {
return err
}
item := &displayers.LoadBalancer{LoadBalancers: do.LoadBalancers{*lb}}
return c.Display(item)
}
// RunLoadBalancerDelete deletes a load balancer by its identifier.
func RunLoadBalancerDelete(c *CmdConfig) error {
if len(c.Args) != 1 {
return doctl.NewMissingArgsErr(c.NS)
}
lbID := c.Args[0]
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
if force || AskForConfirm("Delete this load balancer?") == nil {
lbs := c.LoadBalancers()
if err := lbs.Delete(lbID); err != nil {
return err
}
} else {
return fmt.Errorf("Operation aborted.")
}
return nil
}
// RunLoadBalancerAddDroplets adds droplets to a load balancer.
func RunLoadBalancerAddDroplets(c *CmdConfig) error {
if len(c.Args) != 1 {
return doctl.NewMissingArgsErr(c.NS)
}
lbID := c.Args[0]
dropletIDsList, err := c.Doit.GetStringSlice(c.NS, doctl.ArgDropletIDs)
if err != nil {
return err
}
dropletIDs, err := extractDropletIDs(dropletIDsList)
if err != nil {
return err
}
return c.LoadBalancers().AddDroplets(lbID, dropletIDs...)
}
// RunLoadBalancerRemoveDroplets removes droplets from a load balancer.
func RunLoadBalancerRemoveDroplets(c *CmdConfig) error {
if len(c.Args) != 1 {
return doctl.NewMissingArgsErr(c.NS)
}
lbID := c.Args[0]
dropletIDsList, err := c.Doit.GetStringSlice(c.NS, doctl.ArgDropletIDs)
if err != nil {
return err
}
dropletIDs, err := extractDropletIDs(dropletIDsList)
if err != nil {
return err
}
return c.LoadBalancers().RemoveDroplets(lbID, dropletIDs...)
}
// RunLoadBalancerAddForwardingRules adds forwarding rules to a load balancer.
func RunLoadBalancerAddForwardingRules(c *CmdConfig) error {
if len(c.Args) != 1 {
return doctl.NewMissingArgsErr(c.NS)
}
lbID := c.Args[0]
fra, err := c.Doit.GetString(c.NS, doctl.ArgForwardingRules)
if err != nil {
return err
}
forwardingRules, err := extractForwardingRules(fra)
if err != nil {
return err
}
return c.LoadBalancers().AddForwardingRules(lbID, forwardingRules...)
}
// RunLoadBalancerRemoveForwardingRules removes forwarding rules from a load balancer.
func RunLoadBalancerRemoveForwardingRules(c *CmdConfig) error {
if len(c.Args) != 1 {
return doctl.NewMissingArgsErr(c.NS)
}
lbID := c.Args[0]
fra, err := c.Doit.GetString(c.NS, doctl.ArgForwardingRules)
if err != nil {
return err
}
forwardingRules, err := extractForwardingRules(fra)
if err != nil {
return err
}
return c.LoadBalancers().RemoveForwardingRules(lbID, forwardingRules...)
}
func extractForwardingRules(s string) (forwardingRules []godo.ForwardingRule, err error) {
if len(s) == 0 {
return forwardingRules, err
}
list := strings.Split(s, " ")
for _, v := range list {
forwardingRule := new(godo.ForwardingRule)
if err := fillStructFromStringSliceArgs(forwardingRule, v); err != nil {
return nil, err
}
forwardingRules = append(forwardingRules, *forwardingRule)
}
return forwardingRules, err
}
func fillStructFromStringSliceArgs(obj interface{}, s string) error {
if len(s) == 0 {
return nil
}
kvs := strings.Split(s, ",")
m := map[string]string{}
for _, v := range kvs {
p := strings.Split(v, ":")
if len(p) == 2 {
m[p[0]] = p[1]
} else {
return fmt.Errorf("Unexpected input value %v: must be a key:value pair", p)
}
}
structValue := reflect.Indirect(reflect.ValueOf(obj))
structType := structValue.Type()
for i := 0; i < structType.NumField(); i++ {
f := structValue.Field(i)
jv := strings.Split(structType.Field(i).Tag.Get("json"), ",")[0]
if val, exists := m[jv]; exists {
switch f.Kind() {
case reflect.Bool:
if v, err := strconv.ParseBool(val); err == nil {
f.Set(reflect.ValueOf(v))
}
case reflect.Int:
if v, err := strconv.Atoi(val); err == nil {
f.Set(reflect.ValueOf(v))
}
case reflect.String:
f.Set(reflect.ValueOf(val))
default:
return fmt.Errorf("Unexpected type for struct field %v", val)
}
}
}
return nil
}
func buildRequestFromArgs(c *CmdConfig, r *godo.LoadBalancerRequest) error {
name, err := c.Doit.GetString(c.NS, doctl.ArgLoadBalancerName)
if err != nil {
return err
}
r.Name = name
region, err := c.Doit.GetString(c.NS, doctl.ArgRegionSlug)
if err != nil {
return err
}
r.Region = region
algorithm, err := c.Doit.GetString(c.NS, doctl.ArgLoadBalancerAlgorithm)
if err != nil {
return err
}
r.Algorithm = algorithm
tag, err := c.Doit.GetString(c.NS, doctl.ArgTagName)
if err != nil {
return err
}
r.Tag = tag
redirectHTTPToHTTPS, err := c.Doit.GetBool(c.NS, doctl.ArgRedirectHttpToHttps)
if err != nil {
return err
}
r.RedirectHttpToHttps = redirectHTTPToHTTPS
dropletIDsList, err := c.Doit.GetStringSlice(c.NS, doctl.ArgDropletIDs)
if err != nil {
return err
}
dropletIDs, err := extractDropletIDs(dropletIDsList)
if err != nil {
return err
}
r.DropletIDs = dropletIDs
ssa, err := c.Doit.GetString(c.NS, doctl.ArgStickySessions)
if err != nil {
return err
}
stickySession := new(godo.StickySessions)
if err := fillStructFromStringSliceArgs(stickySession, ssa); err != nil {
return err
}
r.StickySessions = stickySession
hca, err := c.Doit.GetString(c.NS, doctl.ArgHealthCheck)
if err != nil {
return err
}
healthCheck := new(godo.HealthCheck)
if err := fillStructFromStringSliceArgs(healthCheck, hca); err != nil {
return err
}
r.HealthCheck = healthCheck
fra, err := c.Doit.GetString(c.NS, doctl.ArgForwardingRules)
if err != nil {
return err
}
forwardingRules, err := extractForwardingRules(fra)
if err != nil {
return err
}
r.ForwardingRules = forwardingRules
return nil
}