-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_list.go
76 lines (62 loc) · 1.58 KB
/
vm_list.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
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type vmListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Size string `json:"size"`
Zone string `json:"zone"`
State string `json:"state"`
IPAddress string `json:"ip_address"`
}
type vmListOutput []vmListItemOutput
func (o *vmListOutput) toJSON() { outputJSON(o) }
func (o *vmListOutput) toText() { outputText(o) }
func (o *vmListOutput) toTable() { outputTable(o) }
func (o *vmListOutput) names() []string {
names := make([]string, len(*o))
for i, item := range *o {
names[i] = item.Name
}
return names
}
func init() {
vmCmd.AddCommand(&cobra.Command{
Use: "list",
Short: "List Compute instances",
Long: fmt.Sprintf(`This command lists existing Compute instances.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&vmListOutput{}), ", ")),
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return cmd.Usage()
}
return output(listVMs())
},
})
}
func listVMs() (outputter, error) {
vm := &egoscale.VirtualMachine{}
vms, err := cs.ListWithContext(gContext, vm)
if err != nil {
return nil, err
}
out := vmListOutput{}
for _, key := range vms {
vm := key.(*egoscale.VirtualMachine)
out = append(out, vmListItemOutput{
ID: vm.ID.String(),
Name: vm.Name,
Size: vm.ServiceOfferingName,
Zone: vm.ZoneName,
State: vm.State,
IPAddress: vm.IP().String(),
})
}
return &out, nil
}