-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_scale.go
64 lines (52 loc) · 1.45 KB
/
vm_scale.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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// scaleCmd represents the scale command
var vmScaleCmd = &cobra.Command{
Use: "scale <vm name> [vm name] ...",
Short: "Scale virtual machine",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
return cmdCheckRequiredFlags(cmd, []string{"service-offering"})
},
RunE: func(cmd *cobra.Command, args []string) error {
so, err := cmd.Flags().GetString("service-offering")
if err != nil {
return err
}
serviceoffering, err := getServiceOfferingByName(so)
if err != nil {
return err
}
tasks := make([]task, 0, len(args))
for _, v := range args {
vm, err := getVirtualMachineByNameOrID(v)
if err != nil {
return err
}
if vm.State != (string)(egoscale.VirtualMachineStopped) {
fmt.Fprintf(os.Stderr, "this operation is not permitted if your VM is not stopped\n")
}
tasks = append(tasks, task{
&egoscale.ScaleVirtualMachine{ID: vm.ID, ServiceOfferingID: serviceoffering.ID},
fmt.Sprintf("Scaling %q ", vm.Name),
})
}
taskResponses := asyncTasks(tasks)
errors := filterErrors(taskResponses)
if len(errors) > 0 {
return errors[0]
}
return nil
},
}
func init() {
vmCmd.AddCommand(vmScaleCmd)
vmScaleCmd.Flags().StringP("service-offering", "o", "", "<name | id> (micro|tiny|small|medium|large|extra-large|huge|mega|titan")
}