-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_update.go
73 lines (61 loc) · 1.42 KB
/
vm_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
package cmd
import (
"fmt"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var vmUpdateCmd = &cobra.Command{
Use: "update NAME|ID",
Short: "Update a Compute instance properties",
ValidArgsFunction: completeVMNames,
RunE: func(cmd *cobra.Command, args []string) error {
var (
vmEdit egoscale.UpdateVirtualMachine
edited bool
)
if len(args) != 1 {
return cmd.Usage()
}
vm, err := getVirtualMachineByNameOrID(args[0])
if err != nil {
return err
}
vmEdit.ID = vm.ID
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if cmd.Flags().Changed("name") {
vmEdit.Name = name
vmEdit.DisplayName = name
edited = true
}
userDataPath, err := cmd.Flags().GetString("cloud-init-file")
if err != nil {
return err
}
if userDataPath != "" {
vmEdit.UserData, err = getUserDataFromFile(userDataPath)
if err != nil {
return err
}
edited = true
}
if edited {
_, err = cs.RequestWithContext(gContext, &vmEdit)
if err != nil {
return fmt.Errorf("unable to update Compute instance: %s", err)
}
if !gQuiet {
fmt.Println("Compute instance updated successfully")
}
return nil
}
return cmd.Usage()
},
}
func init() {
vmUpdateCmd.Flags().String("name", "", "display name")
vmUpdateCmd.Flags().String("cloud-init-file", "", "path to a cloud-init user data file")
vmCmd.AddCommand(vmUpdateCmd)
}