-
Notifications
You must be signed in to change notification settings - Fork 18
/
vm_update.go
90 lines (77 loc) · 2.07 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
}
userDataCompress, err := cmd.Flags().GetBool("cloud-init-compress")
if err != nil {
return err
}
if userDataPath != "" {
vmEdit.UserData, err = getUserDataFromFile(userDataPath, userDataCompress)
if err != nil {
return err
}
edited = true
}
if edited {
if _, err = cs.RequestWithContext(gContext, &vmEdit); err != nil {
return fmt.Errorf("unable to update Compute instance: %s", err)
}
}
if cmd.Flags().Changed("reverse-dns") {
reverseDNS, err := cmd.Flags().GetString("reverse-dns")
if err != nil {
return err
}
if _, err = cs.RequestWithContext(gContext, &egoscale.UpdateReverseDNSForVirtualMachine{
ID: vm.ID,
DomainName: reverseDNS,
}); err != nil {
return fmt.Errorf("unable to update Compute instance reverse DNS: %s", err)
}
}
if !gQuiet {
return output(showVM(vm.ID.String()))
}
return nil
},
}
func init() {
vmUpdateCmd.Flags().String("name", "", "instance display name")
vmUpdateCmd.Flags().String("cloud-init-file", "", "path to a cloud-init user data file")
vmUpdateCmd.Flags().BoolP("cloud-init-compress", "", false, "compress instance cloud-init user data")
vmUpdateCmd.Flags().String("reverse-dns", "", "instance reverse DNS record")
vmCmd.AddCommand(vmUpdateCmd)
}