-
Notifications
You must be signed in to change notification settings - Fork 20
/
elastic_ip_update.go
163 lines (133 loc) · 5.58 KB
/
elastic_ip_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
package cmd
import (
"fmt"
"strings"
"time"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type elasticIPUpdateCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"update"`
ElasticIP string `cli-arg:"#" cli-usage:"IP-ADDRESS|ID"`
Description string `cli-usage:"Elastic IP description"`
HealthcheckInterval int64 `cli-usage:"managed Elastic IP health checking interval in seconds"`
HealthcheckMode string `cli-usage:"managed Elastic IP health checking mode (tcp|http|https)"`
HealthcheckPort int64 `cli-usage:"managed Elastic IP health checking port"`
HealthcheckStrikesFail int64 `cli-usage:"number of failed attempts before considering a managed Elastic IP health check unhealthy"`
HealthcheckStrikesOK int64 `cli-usage:"number of successful attempts before considering a managed Elastic IP health check healthy"`
HealthcheckTLSSNI string `cli-flag:"healthcheck-tls-sni" cli-usage:"managed Elastic IP health checking server name to present with SNI in https mode"`
HealthcheckTLSSSkipVerify bool `cli-flag:"healthcheck-tls-skip-verify" cli-usage:"disable TLS certificate verification for managed Elastic IP health checking in https mode"`
HealthcheckTimeout int64 `cli-usage:"managed Elastic IP health checking timeout in seconds"`
HealthcheckURI string `cli-usage:"managed Elastic IP health checking URI (required in http(s) mode)"`
Zone string `cli-short:"z" cli-usage:"Elastic IP zone"`
}
func (c *elasticIPUpdateCmd) cmdAliases() []string { return nil }
func (c *elasticIPUpdateCmd) cmdShort() string {
return "Update an Elastic IP"
}
func (c *elasticIPUpdateCmd) cmdLong() string {
return fmt.Sprintf(`This command updates a Compute instance Elastic IP.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&elasticIPShowOutput{}), ", "))
}
func (c *elasticIPUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *elasticIPUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var updated bool
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
elasticIP, err := cs.FindElasticIP(ctx, c.Zone, c.ElasticIP)
if err != nil {
return err
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Description)) {
elasticIP.Description = &c.Description
updated = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.HealthcheckMode)) {
if elasticIP.Healthcheck == nil {
elasticIP.Healthcheck = new(egoscale.ElasticIPHealthcheck)
}
elasticIP.Healthcheck.Mode = &c.HealthcheckMode
updated = true
}
for _, flag := range []string{
mustCLICommandFlagName(c, &c.HealthcheckInterval),
mustCLICommandFlagName(c, &c.HealthcheckPort),
mustCLICommandFlagName(c, &c.HealthcheckStrikesFail),
mustCLICommandFlagName(c, &c.HealthcheckStrikesOK),
mustCLICommandFlagName(c, &c.HealthcheckTLSSNI),
mustCLICommandFlagName(c, &c.HealthcheckTLSSSkipVerify),
mustCLICommandFlagName(c, &c.HealthcheckTimeout),
mustCLICommandFlagName(c, &c.HealthcheckURI),
} {
if cmd.Flags().Changed(flag) && elasticIP.Healthcheck == nil {
return fmt.Errorf("--%s cannot be used on a non-managed Elastic IP", flag)
}
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckInterval); cmd.Flags().Changed(flag) {
interval := time.Duration(c.HealthcheckInterval) * time.Second
elasticIP.Healthcheck.Interval = &interval
updated = true
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckPort); cmd.Flags().Changed(flag) {
port := uint16(c.HealthcheckPort)
elasticIP.Healthcheck.Port = &port
updated = true
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckStrikesFail); cmd.Flags().Changed(flag) {
elasticIP.Healthcheck.StrikesFail = &c.HealthcheckStrikesFail
updated = true
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckStrikesOK); cmd.Flags().Changed(flag) {
elasticIP.Healthcheck.StrikesOK = &c.HealthcheckStrikesOK
updated = true
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckStrikesOK); cmd.Flags().Changed(flag) {
elasticIP.Healthcheck.StrikesOK = &c.HealthcheckStrikesOK
updated = true
}
if elasticIP.Healthcheck != nil && *elasticIP.Healthcheck.Mode == "https" {
if flag := mustCLICommandFlagName(c, &c.HealthcheckTLSSSkipVerify); cmd.Flags().Changed(flag) {
elasticIP.Healthcheck.TLSSkipVerify = &c.HealthcheckTLSSSkipVerify
updated = true
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckTLSSNI); cmd.Flags().Changed(flag) {
elasticIP.Healthcheck.TLSSNI = &c.HealthcheckTLSSNI
updated = true
}
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckTimeout); cmd.Flags().Changed(flag) {
timeout := time.Duration(c.HealthcheckTimeout) * time.Second
elasticIP.Healthcheck.Timeout = &timeout
updated = true
}
if flag := mustCLICommandFlagName(c, &c.HealthcheckURI); cmd.Flags().Changed(flag) {
elasticIP.Healthcheck.URI = &c.HealthcheckURI
updated = true
}
if updated {
decorateAsyncOperation(fmt.Sprintf("Updating Elastic IP %s...", c.ElasticIP), func() {
err = cs.UpdateElasticIP(ctx, c.Zone, elasticIP)
})
if err != nil {
return err
}
}
if !gQuiet {
return (&elasticIPShowCmd{
cliCommandSettings: c.cliCommandSettings,
ElasticIP: *elasticIP.ID,
Zone: c.Zone,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(elasticIPCmd, &elasticIPUpdateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}