-
Notifications
You must be signed in to change notification settings - Fork 20
/
nlb_service_update.go
176 lines (152 loc) · 4.72 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
166
167
168
169
170
171
172
173
174
175
176
package cmd
import (
"errors"
"fmt"
"time"
"github.com/exoscale/egoscale"
apiv2 "github.com/exoscale/egoscale/api/v2"
"github.com/spf13/cobra"
)
var nlbServiceUpdateCmd = &cobra.Command{
Use: "update <NLB name | ID> <service name | ID>",
Short: "Update a Network Load Balancer service",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
cmdExitOnUsageError(cmd, "missing arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
svc *egoscale.NetworkLoadBalancerService
nlbRef = args[0]
svcRef = args[1]
)
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
ctx := apiv2.WithEndpoint(gContext, apiv2.NewReqEndpoint(gCurrentAccount.Environment, zone))
nlb, err := lookupNLB(ctx, zone, nlbRef)
if err != nil {
return err
}
for _, s := range nlb.Services {
if s.ID == svcRef || s.Name == svcRef {
svc = s
break
}
}
if svc == nil {
return errors.New("service not found")
}
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if cmd.Flags().Changed("name") {
svc.Name = name
}
description, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
if cmd.Flags().Changed("description") {
svc.Description = description
}
protocol, err := cmd.Flags().GetString("protocol")
if err != nil {
return err
}
if cmd.Flags().Changed("protocol") {
svc.Protocol = protocol
}
port, err := cmd.Flags().GetUint16("port")
if err != nil {
return err
}
if cmd.Flags().Changed("port") {
svc.Port = port
}
targetPort, err := cmd.Flags().GetUint16("target-port")
if err != nil {
return err
}
if cmd.Flags().Changed("target-port") {
svc.TargetPort = targetPort
}
strategy, err := cmd.Flags().GetString("strategy")
if err != nil {
return err
}
if cmd.Flags().Changed("strategy") {
svc.Strategy = strategy
}
healthcheckMode, err := cmd.Flags().GetString("healthcheck-mode")
if err != nil {
return err
}
if cmd.Flags().Changed("healthcheck-mode") {
svc.Healthcheck.Mode = healthcheckMode
}
healthcheckURI, err := cmd.Flags().GetString("healthcheck-uri")
if err != nil {
return err
}
if cmd.Flags().Changed("healthcheck-uri") {
svc.Healthcheck.URI = healthcheckURI
}
healthcheckPort, err := cmd.Flags().GetUint16("healthcheck-port")
if err != nil {
return err
}
if cmd.Flags().Changed("healthcheck-port") {
svc.Healthcheck.Port = healthcheckPort
}
healthcheckInterval, err := cmd.Flags().GetInt64("healthcheck-interval")
if err != nil {
return err
}
if cmd.Flags().Changed("healthcheck-interval") {
svc.Healthcheck.Interval = time.Duration(healthcheckInterval) * time.Second
}
healthcheckTimeout, err := cmd.Flags().GetInt64("healthcheck-timeout")
if err != nil {
return err
}
if cmd.Flags().Changed("healthcheck-timeout") {
svc.Healthcheck.Timeout = time.Duration(healthcheckTimeout) * time.Second
}
healthcheckRetries, err := cmd.Flags().GetInt64("healthcheck-retries")
if err != nil {
return err
}
if cmd.Flags().Changed("healthcheck-retries") {
svc.Healthcheck.Retries = healthcheckRetries
}
if err := nlb.UpdateService(ctx, svc); err != nil {
return fmt.Errorf("unable to update service: %s", err)
}
if !gQuiet {
return output(showNLBService(zone, nlb.ID, svc.ID))
}
return nil
},
}
func init() {
nlbServiceUpdateCmd.Flags().StringP("zone", "z", "", "Network Load Balancer zone")
nlbServiceUpdateCmd.Flags().String("name", "", "service name")
nlbServiceUpdateCmd.Flags().String("description", "", "service description")
nlbServiceUpdateCmd.Flags().String("protocol", "", "protocol of the service (tcp|udp)")
nlbServiceUpdateCmd.Flags().Uint16("port", 0, "service port")
nlbServiceUpdateCmd.Flags().Uint16("target-port", 0, "port to forward traffic to on target instances")
nlbServiceUpdateCmd.Flags().String("strategy", "", "load balancing strategy (round-robin|source-hash)")
nlbServiceUpdateCmd.Flags().String("healthcheck-mode", "", "service health checking mode (tcp|http)")
nlbServiceUpdateCmd.Flags().String("healthcheck-uri", "", "service health checking URI (required in http mode)")
nlbServiceUpdateCmd.Flags().Uint16("healthcheck-port", 0, "service health checking port")
nlbServiceUpdateCmd.Flags().Int64("healthcheck-interval", 0, "service health checking interval in seconds")
nlbServiceUpdateCmd.Flags().Int64("healthcheck-timeout", 0, "service health checking timeout in seconds")
nlbServiceUpdateCmd.Flags().Int64("healthcheck-retries", 0, "service health checking retries")
nlbServiceCmd.AddCommand(nlbServiceUpdateCmd)
}