-
Notifications
You must be signed in to change notification settings - Fork 20
/
nlb_service_update.go
165 lines (134 loc) · 4.88 KB
/
nlb_service_update.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
package cmd
import (
"errors"
"fmt"
"strings"
"time"
exov2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type nlbServiceUpdateCmd struct {
_ bool `cli-cmd:"update"`
NetworkLoadBalancer string `cli-arg:"#" cli-usage:"LOAD-BALANCER-NAME|ID"`
Service string `cli-arg:"#" cli-usage:"SERVICE-NAME|ID"`
Description string `cli-usage:"service description"`
HealthcheckInterval int64 `cli-usage:"service health checking interval in seconds"`
HealthcheckMode string `cli-usage:"service health checking mode (tcp|http|https)"`
HealthcheckPort int64 `cli-usage:"service health checking port"`
HealthcheckRetries int64 `cli-usage:"service health checking retries"`
HealthcheckTLSSNI string `cli-flag:"healthcheck-tls-sni" cli-usage:"service health checking server name to present with SNI in https mode"`
HealthcheckTimeout int64 `cli-usage:"service health checking timeout in seconds"`
HealthcheckURI string `cli-usage:"service health checking URI (required in http(s) mode)"`
Name string `cli-usage:"service name"`
Port int64 `cli-usage:"service port"`
Protocol string `cli-usage:"service network protocol (tcp|udp)"`
Strategy string `cli-usage:"load balancing strategy (round-robin|source-hash)"`
TargetPort int64 `cli-usage:"port to forward traffic to on target instances"`
Zone string `cli-short:"z" cli-usage:"Network Load Balancer zone"`
}
func (c *nlbServiceUpdateCmd) cmdAliases() []string { return nil }
func (c *nlbServiceUpdateCmd) cmdShort() string { return "Update a Network Load Balancer service" }
func (c *nlbServiceUpdateCmd) cmdLong() string {
return fmt.Sprintf(`This command updates a Network Load Balancer service.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&nlbServiceShowOutput{}), ", "))
}
func (c *nlbServiceUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *nlbServiceUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var (
service *exov2.NetworkLoadBalancerService
updated bool
)
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
nlb, err := cs.FindNetworkLoadBalancer(ctx, c.Zone, c.NetworkLoadBalancer)
if err != nil {
return err
}
for _, s := range nlb.Services {
if *s.ID == c.Service || *s.Name == c.Service {
service = s
break
}
}
if service == nil {
return errors.New("service not found")
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Description)) {
service.Description = &c.Description
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckInterval)) {
hcInterval := time.Duration(c.HealthcheckInterval) * time.Second
service.Healthcheck.Interval = &hcInterval
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckMode)) {
service.Healthcheck.Mode = &c.HealthcheckMode
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckPort)) {
hcPort := uint16(c.HealthcheckPort)
service.Healthcheck.Port = &hcPort
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckRetries)) {
service.Healthcheck.Retries = &c.HealthcheckRetries
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckTLSSNI)) {
service.Healthcheck.TLSSNI = &c.HealthcheckTLSSNI
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckTimeout)) {
hcTimeout := time.Duration(c.HealthcheckTimeout) * time.Second
service.Healthcheck.Timeout = &hcTimeout
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckURI)) {
service.Healthcheck.URI = &c.HealthcheckURI
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Name)) {
service.Name = &c.Name
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Port)) {
port := uint16(c.Port)
service.Port = &port
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Protocol)) {
service.Protocol = &c.Protocol
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Strategy)) {
service.Strategy = &c.Strategy
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.TargetPort)) {
targetPort := uint16(c.TargetPort)
service.TargetPort = &targetPort
updated = true
}
decorateAsyncOperation(fmt.Sprintf("Updating service %q...", c.Service), func() {
if updated {
if err = nlb.UpdateService(ctx, service); err != nil {
return
}
}
})
if err != nil {
return err
}
if !gQuiet {
return output(showNLBService(c.Zone, *nlb.ID, *service.ID))
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(nlbServiceCmd, &nlbServiceUpdateCmd{}))
}