-
Notifications
You must be signed in to change notification settings - Fork 18
/
vm_scale.go
78 lines (65 loc) · 1.76 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cmd
import (
"errors"
"fmt"
"log"
"strings"
"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",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
so, err := cmd.Flags().GetString("service-offering")
if err != nil {
return err
}
serviceoffering, err := getServiceOfferingByName(so)
if err != nil {
return err
}
errs := []error{}
for _, v := range args {
if err := scaleVirtualMachine(v, *serviceoffering.ID); err != nil {
errs = append(errs, fmt.Errorf("could not scale %q: %s", v, err))
}
}
if len(errs) == 1 {
return errs[0]
}
if len(errs) > 1 {
var b strings.Builder
for _, err := range errs {
if _, e := fmt.Fprintln(&b, err); e != nil {
return e
}
}
return errors.New(b.String())
}
return nil
},
}
// scaleVirtualMachine scale a virtual machine instance Async with context
func scaleVirtualMachine(vmName string, serviceofferingID egoscale.UUID) error {
vm, err := getVMWithNameOrID(vmName)
if err != nil {
return err
}
if vm.State != (string)(egoscale.VirtualMachineStopped) {
return fmt.Errorf("this operation is not permitted if your VM is not stopped")
}
_, err = asyncRequest(&egoscale.ScaleVirtualMachine{ID: vm.ID, ServiceOfferingID: &serviceofferingID}, fmt.Sprintf("Scaling %q ", vm.Name))
return err
}
func init() {
vmCmd.AddCommand(vmScaleCmd)
vmScaleCmd.Flags().StringP("service-offering", "o", "", "<name | id> (micro|tiny|small|medium|large|extra-large|huge|mega|titan")
if err := vmScaleCmd.MarkFlagRequired("service-offering"); err != nil {
log.Fatal(err)
}
}