-
Notifications
You must be signed in to change notification settings - Fork 18
/
instance_update.go
117 lines (93 loc) · 3.21 KB
/
instance_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
package cmd
import (
"errors"
"fmt"
"strings"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type instanceUpdateCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"update"`
Instance string `cli-arg:"#" cli-usage:"NAME|ID"`
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"`
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(outputterTemplateAnnotations(&instanceShowOutput{}), ", "),
)
}
func (c *instanceUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
var updatedInstance, updatedRDNS bool
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
instance, err := cs.FindInstance(ctx, c.Zone, c.Instance)
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return fmt.Errorf("resource not found in zone %q", c.Zone)
}
return err
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Labels)) {
instance.Labels = &c.Labels
updatedInstance = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Name)) {
instance.Name = &c.Name
updatedInstance = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.CloudInitFile)) {
userData, err := getUserDataFromFile(c.CloudInitFile, c.CloudInitCompress)
if err != nil {
return fmt.Errorf("error parsing cloud-init user data: %w", err)
}
instance.UserData = &userData
updatedInstance = true
}
if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.ReverseDNS)) {
updatedRDNS = true
}
if updatedInstance || updatedRDNS {
decorateAsyncOperation(fmt.Sprintf("Updating instance %q...", c.Instance), func() {
if updatedInstance {
if err = cs.UpdateInstance(ctx, c.Zone, instance); err != nil {
return
}
}
if updatedRDNS {
if c.ReverseDNS == "" {
err = cs.DeleteInstanceReverseDNS(ctx, c.Zone, *instance.ID)
} else {
err = cs.UpdateInstanceReverseDNS(ctx, c.Zone, *instance.ID, c.ReverseDNS)
}
}
})
if err != nil {
return err
}
}
if !gQuiet {
return (&instanceShowCmd{
cliCommandSettings: c.cliCommandSettings,
Instance: *instance.ID,
Zone: c.Zone,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(instanceCmd, &instanceUpdateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}