-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_resize.go
91 lines (76 loc) · 2 KB
/
vm_resize.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
package cmd
import (
"fmt"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var vmResizeCmd = &cobra.Command{
Use: "resize NAME|ID",
Short: "Resize a Compute instance disk",
ValidArgsFunction: completeVMNames,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
return cmdCheckRequiredFlags(cmd, []string{"disk"})
},
RunE: func(cmd *cobra.Command, args []string) error {
diskValue, err := cmd.Flags().GetInt64("disk")
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
tasks := make([]task, 0, len(args))
for _, v := range args {
if !force {
if !askQuestion(fmt.Sprintf("Are you sure you want to resize Compute instance disk %q?", v)) {
continue
}
}
task, err := resizeVirtualMachine(v, diskValue)
if err != nil {
return err
}
tasks = append(tasks, *task)
}
taskResponses := asyncTasks(tasks)
errors := filterErrors(taskResponses)
if len(errors) > 0 {
return errors[0]
}
return nil
},
}
func resizeVirtualMachine(vmName string, diskValue int64) (*task, error) {
vm, err := getVirtualMachineByNameOrID(vmName)
if err != nil {
return nil, err
}
state := (string)(egoscale.VirtualMachineStopped)
if vm.State != state {
return nil, fmt.Errorf("this operation is not permitted while your Compute instance is running; stop it before issuing that command again")
}
resp, err := cs.GetWithContext(gContext, egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
})
if err != nil {
return nil, err
}
resizeVolume := &egoscale.ResizeVolume{
ID: resp.(*egoscale.Volume).ID,
Size: diskValue,
}
return &task{
resizeVolume,
fmt.Sprintf("Resizing %q ", vm.Name),
}, err
}
func init() {
vmCmd.AddCommand(vmResizeCmd)
vmResizeCmd.Flags().Int64P("disk", "d", 0, "Disk size in GB")
vmResizeCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
}