-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathinstance_update.go
More file actions
168 lines (138 loc) · 5.15 KB
/
instance_update.go
File metadata and controls
168 lines (138 loc) · 5.15 KB
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
package instance
import (
"fmt"
"strings"
"github.com/spf13/cobra"
exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/pkg/userdata"
"github.com/exoscale/cli/utils"
v3 "github.com/exoscale/egoscale/v3"
)
type instanceUpdateCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"update"`
Instance string `cli-arg:"#" cli-usage:"NAME|ID"`
AppConsistentSnapshot bool `cli-flag:"application-consistent-snapshot-enabled" cli-usage:"update instance application-consistent snapshots"`
CloudInitFile string `cli-flag:"cloud-init" cli-short:"c" cli-usage:"instance cloud-init user data configuration file path"`
CloudInitCompress bool `cli-flag:"cloud-init-compress" cli-usage:"compress instance cloud-init user data"`
Labels map[string]string `cli-flag:"label" cli-usage:"instance label (format: key=value)"`
Name string `cli-short:"n" cli-usage:"instance name"`
Protection bool `cli-flag:"protection" cli-usage:"delete protection; set --protection=false to disable instance protection"`
Zone string `cli-short:"z" cli-usage:"instance zone"`
ReverseDNS string `cli-usage:"Reverse DNS Domain"`
}
func (c *instanceUpdateCmd) CmdAliases() []string { return nil }
func (c *instanceUpdateCmd) CmdShort() string { return "Update an Instance " }
func (c *instanceUpdateCmd) CmdLong() string {
return fmt.Sprintf(`This command updates an Instance .
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&InstanceShowOutput{}), ", "),
)
}
func (c *instanceUpdateCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceUpdateCmd) CmdRun(cmd *cobra.Command, _ []string) error {
var updatedInstance, updatedRDNS bool
ctx := exocmd.GContext
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone))
if err != nil {
return err
}
instances, err := client.ListInstances(ctx)
if err != nil {
return err
}
instance, err := findInstance(instances, c.Instance, c.Zone)
if err != nil {
return err
}
updateRequest := v3.UpdateInstanceRequest{}
updateRDNSRequest := v3.UpdateReverseDNSInstanceRequest{}
if cmd.Flags().Changed(exocmd.MustCLICommandFlagName(c, &c.Labels)) {
// To be fixed in the API spec: allow clearing all labels by setting
// an empty map[string]string (rightnow being omited)
updateRequest.Labels = c.Labels
updatedInstance = true
}
if cmd.Flags().Changed(exocmd.MustCLICommandFlagName(c, &c.Name)) {
updateRequest.Name = c.Name
updatedInstance = true
}
if cmd.Flags().Changed(exocmd.MustCLICommandFlagName(c, &c.CloudInitFile)) {
userData, err := userdata.GetUserDataFromFile(c.CloudInitFile, c.CloudInitCompress)
if err != nil {
return fmt.Errorf("error parsing cloud-init user data: %w", err)
}
updateRequest.UserData = userData
updatedInstance = true
}
if cmd.Flags().Changed(exocmd.MustCLICommandFlagName(c, &c.ReverseDNS)) {
updateRDNSRequest.DomainName = c.ReverseDNS
updatedRDNS = true
}
if cmd.Flags().Changed(exocmd.MustCLICommandFlagName(c, &c.AppConsistentSnapshot)) {
updateRequest.ApplicationConsistentSnapshotEnabled = &c.AppConsistentSnapshot
updatedInstance = true
}
if updatedInstance || updatedRDNS || cmd.Flags().Changed(exocmd.MustCLICommandFlagName(c, &c.Protection)) {
if updatedInstance {
op, err := client.UpdateInstance(ctx, instance.ID, updateRequest)
if err != nil {
return err
}
utils.DecorateAsyncOperation(fmt.Sprintf("Updating instance %q...", c.Instance), func() {
_, err = client.Wait(ctx, op, v3.OperationStateSuccess)
})
if err != nil {
return err
}
}
if updatedRDNS {
op, err := client.UpdateReverseDNSInstance(ctx, instance.ID, updateRDNSRequest)
if err != nil {
return err
}
utils.DecorateAsyncOperation(fmt.Sprintf("Updating instance reverse DNS %q...", c.Instance), func() {
_, err = client.Wait(ctx, op, v3.OperationStateSuccess)
})
if err != nil {
return err
}
}
if cmd.Flags().Changed(exocmd.MustCLICommandFlagName(c, &c.Protection)) {
var op *v3.Operation
var err error
if c.Protection {
op, err = client.AddInstanceProtection(ctx, instance.ID)
} else {
op, err = client.RemoveInstanceProtection(ctx, instance.ID)
}
if err != nil {
return err
}
utils.DecorateAsyncOperation(fmt.Sprintf("Updating instance protection %q to %v...", c.Instance, c.Protection), func() {
_, err = client.Wait(ctx, op, v3.OperationStateSuccess)
})
if err != nil {
return err
}
}
}
if !globalstate.Quiet {
return (&instanceShowCmd{
CliCommandSettings: c.CliCommandSettings,
Instance: instance.ID.String(),
Zone: v3.ZoneName(c.Zone),
}).CmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(instanceCmd, &instanceUpdateCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}