-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_list.go
50 lines (40 loc) · 1005 Bytes
/
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
package cmd
import (
"os"
"strings"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var vmlistCmd = &cobra.Command{
Use: "list",
Short: "List all the virtual machines instances",
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return cmd.Usage()
}
return listVMs()
},
}
func listVMs() error {
vm := &egoscale.VirtualMachine{}
vms, err := cs.ListWithContext(gContext, vm)
if err != nil {
return err
}
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Name", "Security Group", "IP Address", "Status", "Zone", "ID"})
for _, key := range vms {
vm := key.(*egoscale.VirtualMachine)
sgs := getSecurityGroup(vm)
sgName := strings.Join(sgs, " - ")
table.Append([]string{vm.Name, sgName, vm.IP().String(), vm.State, vm.ZoneName, vm.ID.String()})
}
table.Render()
return nil
}
func init() {
vmCmd.AddCommand(vmlistCmd)
}