-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathload_balancers.go
756 lines (637 loc) · 26 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/*
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 (
_ "embed"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
"github.com/digitalocean/doctl/do"
"github.com/digitalocean/godo"
"github.com/spf13/cobra"
)
var (
//go:embed forwarding_detail.txt
forwardingDetail string
//go:embed lb_detail.txt
lbDetail string
)
// LoadBalancer creates the load balancer command.
func LoadBalancer() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "load-balancer",
Short: "Display commands to manage load balancers",
Aliases: []string{"lb"},
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.`,
},
}
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`."
CmdBuilder(cmd, RunLoadBalancerGet, "get <load-balancer-id>", "Retrieve a load balancer", "Use this command to retrieve information about a load balancer instance, including:\n\n"+lbDetail, Writer,
aliasOpt("g"), displayerType(&displayers.LoadBalancer{}))
cmdLoadBalancerCreate := CmdBuilder(cmd, RunLoadBalancerCreate, "create",
"Create a new load balancer", "Use this command to create a new load balancer on your account. Valid forwarding rules are:\n"+forwardingDetail, Writer, aliasOpt("c"))
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgLoadBalancerName, "", "",
"The load balancer's name", requiredOpt())
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgRegionSlug, "", "",
"The load balancer's region, e.g.: `nyc1`")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgSizeSlug, "", "",
fmt.Sprintf("The load balancer's size, e.g.: `lb-small`. Only one of %s and %s should be used", doctl.ArgSizeSlug, doctl.ArgSizeUnit))
AddIntFlag(cmdLoadBalancerCreate, doctl.ArgSizeUnit, "", 0,
fmt.Sprintf("The load balancer's size, e.g.: 1. Only one of %s and %s should be used", doctl.ArgSizeUnit, doctl.ArgSizeSlug))
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgLoadBalancerType, "", "", "The type of load balancer, e.g.: `REGIONAL` or `GLOBAL`")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgVPCUUID, "", "", "The UUID of the VPC to create the load balancer in")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgLoadBalancerAlgorithm, "",
"round_robin", "This field has been deprecated. You can no longer specify an algorithm for load balancers.")
AddBoolFlag(cmdLoadBalancerCreate, doctl.ArgRedirectHTTPToHTTPS, "", false,
"Redirects HTTP requests to the load balancer on port 80 to HTTPS on port 443")
AddBoolFlag(cmdLoadBalancerCreate, doctl.ArgEnableProxyProtocol, "", false,
"enable proxy protocol")
AddBoolFlag(cmdLoadBalancerCreate, doctl.ArgEnableBackendKeepalive, "", false,
"enable keepalive connections to backend target droplets")
AddBoolFlag(cmdLoadBalancerCreate, doctl.ArgDisableLetsEncryptDNSRecords, "", false,
"disable automatic DNS record creation for Let's Encrypt certificates that are added to the load balancer")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgTagName, "", "", "The name of a tag. All Droplets with this tag applied will be assigned to the load balancer.")
AddStringSliceFlag(cmdLoadBalancerCreate, doctl.ArgDropletIDs, "", []string{},
"A comma-separated list of Droplet IDs to add to the load balancer, e.g.: `12,33`")
AddStringFlag(cmdLoadBalancerCreate, 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(cmdLoadBalancerCreate, 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(cmdLoadBalancerCreate, doctl.ArgForwardingRules, "", "",
forwardingRulesTxt)
AddBoolFlag(cmdLoadBalancerCreate, doctl.ArgCommandWait, "", false, "Boolean that specifies whether to wait for a load balancer to complete before returning control to the terminal")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgProjectID, "", "", "Indicates which project to associate the Load Balancer with. If not specified, the Load Balancer will be placed in your default project.")
AddIntFlag(cmdLoadBalancerCreate, doctl.ArgHTTPIdleTimeoutSeconds, "", 0, "HTTP idle timeout that configures the idle timeout for http connections on the load balancer")
AddStringSliceFlag(cmdLoadBalancerCreate, doctl.ArgAllowList, "", []string{},
"A comma-separated list of ALLOW rules for the load balancer, e.g.: `ip:1.2.3.4,cidr:1.2.0.0/16`")
AddStringSliceFlag(cmdLoadBalancerCreate, doctl.ArgDenyList, "", []string{},
"A comma-separated list of DENY rules for the load balancer, e.g.: `ip:1.2.3.4,cidr:1.2.0.0/16`")
AddStringSliceFlag(cmdLoadBalancerCreate, doctl.ArgLoadBalancerDomains, "", []string{},
"A comma-separated list of domains required to ingress traffic to a global load balancer, e.g.: `name:test-domain-1 is_managed:true certificate_id:test-cert-id-1` ")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgGlobalLoadBalancerSettings, "", "",
"Target protocol and port settings for ingressing traffic to a global load balancer, e.g.: `target_protocol:http,target_port:80` ")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgGlobalLoadBalancerCDNSettings, "", "",
"CDN cache settings global load balancer, e.g.: `is_enabled:true` ")
AddStringSliceFlag(cmdLoadBalancerCreate, doctl.ArgTargetLoadBalancerIDs, "", []string{},
"A comma-separated list of Load Balancer IDs to add as target to the global load balancer ")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgLoadBalancerNetwork, "", "", "The type of network the load balancer is accessible from, e.g.: `EXTERNAL` or `INTERNAL`")
AddStringFlag(cmdLoadBalancerCreate, doctl.ArgLoadBalancerNetworkStack, "", "", "The network stack type determines the allocation of ipv4/ipv6 addresses to the load balancer, e.g.: `IPV4` or `DUALSTACK`"+
" (NOTE: this feature is in private preview, contact DigitalOcean support to review its public availability.)")
cmdRecordUpdate := CmdBuilder(cmd, RunLoadBalancerUpdate, "update <load-balancer-id>",
"Update a load balancer's configuration", `Use this command to update the configuration of a specified load balancer. Using all applicable flags, the command should contain a full representation of the load balancer including existing attributes, such as the load balancer's name, region, forwarding rules, and Droplet IDs. Any attribute that is not provided is reset to its default value.`, Writer, aliasOpt("u"))
AddStringFlag(cmdRecordUpdate, doctl.ArgLoadBalancerName, "", "",
"The load balancer's name")
AddStringFlag(cmdRecordUpdate, doctl.ArgRegionSlug, "", "",
"The load balancer's region, e.g.: `nyc1`")
AddStringFlag(cmdRecordUpdate, doctl.ArgSizeSlug, "", "",
fmt.Sprintf("The load balancer's size, e.g.: `lb-small`. Only one of %s and %s should be used", doctl.ArgSizeSlug, doctl.ArgSizeUnit))
AddIntFlag(cmdRecordUpdate, doctl.ArgSizeUnit, "", 0,
fmt.Sprintf("The load balancer's size, e.g.: 1. Only one of %s and %s should be used", doctl.ArgSizeUnit, doctl.ArgSizeSlug))
AddStringFlag(cmdRecordUpdate, doctl.ArgVPCUUID, "", "", "The UUID of the VPC to create the load balancer in")
AddStringFlag(cmdRecordUpdate, doctl.ArgLoadBalancerAlgorithm, "",
"round_robin", "This field has been deprecated. You can no longer specify an algorithm for load balancers.")
AddBoolFlag(cmdRecordUpdate, doctl.ArgRedirectHTTPToHTTPS, "", false,
"Flag to redirect HTTP requests to the load balancer on port 80 to HTTPS on port 443")
AddBoolFlag(cmdRecordUpdate, doctl.ArgEnableProxyProtocol, "", false,
"enable proxy protocol")
AddBoolFlag(cmdRecordUpdate, doctl.ArgEnableBackendKeepalive, "", false,
"enable keepalive connections to backend target droplets")
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)
AddBoolFlag(cmdRecordUpdate, doctl.ArgDisableLetsEncryptDNSRecords, "", false,
"disable automatic DNS record creation for Let's Encrypt certificates that are added to the load balancer")
AddStringFlag(cmdRecordUpdate, doctl.ArgProjectID, "", "",
"Indicates which project to associate the Load Balancer with. If not specified, the Load Balancer will be placed in your default project.")
AddStringSliceFlag(cmdRecordUpdate, doctl.ArgAllowList, "", nil,
"A comma-separated list of ALLOW rules for the load balancer, e.g.: `ip:1.2.3.4,cidr:1.2.0.0/16`")
AddStringSliceFlag(cmdRecordUpdate, doctl.ArgDenyList, "", nil,
"A comma-separated list of DENY rules for the load balancer, e.g.: `ip:1.2.3.4,cidr:1.2.0.0/16`")
AddStringSliceFlag(cmdRecordUpdate, doctl.ArgLoadBalancerDomains, "", []string{},
"A comma-separated list of domains required to ingress traffic to a global load balancer, e.g.: `name:test-domain-1 is_managed:true certificate_id:test-cert-id-1` ")
AddStringFlag(cmdRecordUpdate, doctl.ArgGlobalLoadBalancerSettings, "", "",
"Target protocol and port settings for ingressing traffic to a global load balancer, e.g.: `target_protocol:http,target_port:80` ")
AddStringFlag(cmdRecordUpdate, doctl.ArgGlobalLoadBalancerCDNSettings, "", "",
"CDN cache settings global load balancer, e.g.: `is_enabled:true` ")
AddStringSliceFlag(cmdRecordUpdate, doctl.ArgTargetLoadBalancerIDs, "", []string{},
"A comma-separated list of Load Balancer IDs to add as target to the global load balancer ")
CmdBuilder(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:\n\n"+lbDetail, Writer,
aliasOpt("ls"), displayerType(&displayers.LoadBalancer{}))
cmdRunRecordDelete := CmdBuilder(cmd, RunLoadBalancerDelete, "delete <load-balancer-id>",
"Permanently delete a load balancer", `Use this command to permanently delete the specified load balancer. This is irreversible.`, Writer, aliasOpt("d", "rm"))
AddBoolFlag(cmdRunRecordDelete, doctl.ArgForce, doctl.ArgShortForce, false,
"Delete the load balancer without a confirmation prompt")
cmdAddDroplets := CmdBuilder(cmd, RunLoadBalancerAddDroplets, "add-droplets <load-balancer-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 := CmdBuilder(cmd, RunLoadBalancerRemoveDroplets,
"remove-droplets <load-balancer-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 := CmdBuilder(cmd, RunLoadBalancerAddForwardingRules,
"add-forwarding-rules <load-balancer-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:\n"+forwardingDetail, Writer)
AddStringFlag(cmdAddForwardingRules, doctl.ArgForwardingRules, "", "", forwardingRulesTxt)
cmdRemoveForwardingRules := CmdBuilder(cmd, RunLoadBalancerRemoveForwardingRules,
"remove-forwarding-rules <load-balancer-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:\n"+forwardingDetail, Writer)
AddStringFlag(cmdRemoveForwardingRules, doctl.ArgForwardingRules, "", "", forwardingRulesTxt)
cmdRunCachePurge := CmdBuilder(cmd, RunLoadBalancerPurgeCache, "purge-cache <load-balancer-id>",
"Purges CDN cache for a global load balancer", `Use this command to purge the CDN cache for specified global load balancer.`, Writer)
AddBoolFlag(cmdRunCachePurge, doctl.ArgForce, doctl.ArgShortForce, false,
"Purge the global load balancer CDN cache without a confirmation prompt ")
return cmd
}
// RunLoadBalancerGet retrieves an existing load balancer by its identifier.
func RunLoadBalancerGet(c *CmdConfig) error {
err := ensureOneArg(c)
if err != nil {
return err
}
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
}
wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait)
if err != nil {
return err
}
if wait {
lbs := c.LoadBalancers()
notice("Load balancer creation is in progress, waiting for load balancer to become active")
err := waitForActiveLoadBalancer(lbs, lb.ID)
if err != nil {
return fmt.Errorf(
"load balancer couldn't enter `active` state: %v",
err,
)
}
lb, _ = lbs.Get(lb.ID)
}
notice("Load balancer created")
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 {
err := ensureOneArg(c)
if err != nil {
return err
}
lbID := c.Args[0]
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
if force || AskForConfirmDelete("load balancer", 1) == nil {
lbs := c.LoadBalancers()
if err := lbs.Delete(lbID); err != nil {
return err
}
} else {
return errOperationAborted
}
return nil
}
// RunLoadBalancerAddDroplets adds droplets to a load balancer.
func RunLoadBalancerAddDroplets(c *CmdConfig) error {
err := ensureOneArg(c)
if err != nil {
return err
}
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 {
err := ensureOneArg(c)
if err != nil {
return err
}
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 {
err := ensureOneArg(c)
if err != nil {
return err
}
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 {
err := ensureOneArg(c)
if err != nil {
return err
}
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...)
}
// RunLoadBalancerPurgeCache purges cache for a global load balancer by its identifier.
func RunLoadBalancerPurgeCache(c *CmdConfig) error {
err := ensureOneArg(c)
if err != nil {
return err
}
lbID := c.Args[0]
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
if err != nil {
return err
}
if force || AskForConfirm("purge CDN cache for global load balancer") == nil {
lbs := c.LoadBalancers()
if err := lbs.PurgeCache(lbID); err != nil {
return err
}
} else {
return errOperationAborted
}
return nil
}
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 extractDomains(s []string) (domains []*godo.LBDomain, err error) {
if len(s) == 0 {
return domains, err
}
for _, v := range s {
domain := new(godo.LBDomain)
if err := fillStructFromStringSliceArgs(domain, v, " "); err != nil {
return nil, err
}
domains = append(domains, domain)
}
return domains, err
}
func fillStructFromStringSliceArgs(obj any, s string, delimiter string) error {
if len(s) == 0 {
return nil
}
kvs := strings.Split(s, delimiter)
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.Uint32:
if v64, err := strconv.ParseUint(val, 10, 32); err == nil {
f.Set(reflect.ValueOf(uint32(v64)))
}
case reflect.String:
f.Set(reflect.ValueOf(val))
case reflect.Map:
for _, kvPair := range strings.Split(val, " ") {
kv := strings.Split(kvPair, "=")
if len(kv) == 2 {
if v32, err := strconv.ParseUint(kv[1], 10, 32); err == nil {
if f.IsZero() {
f.Set(reflect.MakeMap(f.Type()))
}
f.SetMapIndex(reflect.ValueOf(kv[0]), reflect.ValueOf(uint32(v32)))
}
}
}
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
size, err := c.Doit.GetString(c.NS, doctl.ArgSizeSlug)
if err != nil {
return err
}
r.SizeSlug = size
sizeUnit, err := c.Doit.GetInt(c.NS, doctl.ArgSizeUnit)
if err != nil {
return err
}
r.SizeUnit = uint32(sizeUnit)
lbType, err := c.Doit.GetString(c.NS, doctl.ArgLoadBalancerType)
if err != nil {
return err
}
r.Type = strings.ToUpper(lbType)
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
vpcUUID, err := c.Doit.GetString(c.NS, doctl.ArgVPCUUID)
if err != nil {
return err
}
r.VPCUUID = vpcUUID
redirectHTTPToHTTPS, err := c.Doit.GetBool(c.NS, doctl.ArgRedirectHTTPToHTTPS)
if err != nil {
return err
}
r.RedirectHttpToHttps = redirectHTTPToHTTPS
enableProxyProtocol, err := c.Doit.GetBool(c.NS, doctl.ArgEnableProxyProtocol)
if err != nil {
return err
}
r.EnableProxyProtocol = enableProxyProtocol
enableBackendKeepalive, err := c.Doit.GetBool(c.NS, doctl.ArgEnableBackendKeepalive)
if err != nil {
return err
}
r.EnableBackendKeepalive = enableBackendKeepalive
disableLetsEncryptDNSRecords, err := c.Doit.GetBool(c.NS, doctl.ArgDisableLetsEncryptDNSRecords)
if err != nil {
return err
}
r.DisableLetsEncryptDNSRecords = &disableLetsEncryptDNSRecords
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
projectID, err := c.Doit.GetString(c.NS, doctl.ArgProjectID)
if err != nil {
return err
}
r.ProjectID = projectID
httpIdleTimeout, err := c.Doit.GetInt(c.NS, doctl.ArgHTTPIdleTimeoutSeconds)
if err != nil {
return err
}
if httpIdleTimeout != 0 {
t := uint64(httpIdleTimeout)
r.HTTPIdleTimeoutSeconds = &t
}
allowRules, allowflagSet, err := c.Doit.GetStringSliceIsFlagSet(c.NS, doctl.ArgAllowList)
if err != nil {
return err
}
denyRules, denyFlagSet, err := c.Doit.GetStringSliceIsFlagSet(c.NS, doctl.ArgDenyList)
if err != nil {
return err
}
if allowflagSet || denyFlagSet {
firewall := new(godo.LBFirewall)
firewall.Allow = allowRules
firewall.Deny = denyRules
r.Firewall = firewall
}
dms, err := c.Doit.GetStringSlice(c.NS, doctl.ArgLoadBalancerDomains)
if err != nil {
return err
}
domains, err := extractDomains(dms)
if err != nil {
return err
}
r.Domains = domains
glbs, err := c.Doit.GetString(c.NS, doctl.ArgGlobalLoadBalancerSettings)
if err != nil {
return err
}
glbSettings := new(godo.GLBSettings)
if err := fillStructFromStringSliceArgs(glbSettings, glbs, ","); err != nil {
return err
}
if glbSettings.TargetProtocol != "" && glbSettings.TargetPort != 0 {
r.GLBSettings = glbSettings
}
cdns, err := c.Doit.GetString(c.NS, doctl.ArgGlobalLoadBalancerCDNSettings)
if err != nil {
return err
}
cdnSettings := new(godo.CDNSettings)
if err := fillStructFromStringSliceArgs(cdnSettings, cdns, ","); err != nil {
return err
}
if r.GLBSettings != nil {
r.GLBSettings.CDN = cdnSettings
}
lbIDsList, err := c.Doit.GetStringSlice(c.NS, doctl.ArgTargetLoadBalancerIDs)
if err != nil {
return err
}
r.TargetLoadBalancerIDs = lbIDsList
network, err := c.Doit.GetString(c.NS, doctl.ArgLoadBalancerNetwork)
if err != nil {
return err
}
r.Network = strings.ToUpper(network)
networkStack, err := c.Doit.GetString(c.NS, doctl.ArgLoadBalancerNetworkStack)
if err != nil {
return err
}
r.NetworkStack = strings.ToUpper(networkStack)
return nil
}
func waitForActiveLoadBalancer(lbs do.LoadBalancersService, lbID string) error {
const maxAttempts = 180
const wantStatus = "active"
const errStatus = "errored"
attempts := 0
printNewLineSet := false
for i := 0; i < maxAttempts; i++ {
if attempts != 0 {
fmt.Fprint(os.Stderr, ".")
if !printNewLineSet {
printNewLineSet = true
defer fmt.Fprintln(os.Stderr)
}
}
lb, err := lbs.Get(lbID)
if err != nil {
return err
}
if lb.Status == errStatus {
return fmt.Errorf(
"load balancer (%s) entered status `errored`",
lbID,
)
}
if lb.Status == wantStatus {
return nil
}
attempts++
time.Sleep(10 * time.Second)
}
return fmt.Errorf(
"timeout waiting for load balancer (%s) to become active",
lbID,
)
}