-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_show.go
104 lines (72 loc) · 2.07 KB
/
vm_show.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
92
93
94
95
96
97
98
99
100
101
102
103
104
package cmd
import (
"fmt"
"os"
"strings"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// showCmd represents the show command
var vmShowCmd = &cobra.Command{
Use: "show <name | id>",
Short: "Show virtual machine details",
Aliases: gShowAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
return showVM(args[0])
},
}
func showVM(name string) error {
vm, err := getVirtualMachineByNameOrID(name)
if err != nil {
return err
}
resp, err := cs.GetWithContext(gContext, &egoscale.Template{
IsFeatured: true,
ID: vm.TemplateID,
ZoneID: vm.ZoneID,
})
if err != nil {
return err
}
temp := resp.(*egoscale.Template)
resp, err = cs.GetWithContext(gContext, &egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
})
if err != nil {
return err
}
volume := resp.(*egoscale.Volume)
table := table.NewTable(os.Stdout)
table.SetHeader([]string{vm.Name})
table.Append([]string{"State", vm.State})
table.Append([]string{"OS Template", vm.TemplateName})
table.Append([]string{"Region", vm.ZoneName})
table.Append([]string{"Instance Type", vm.ServiceOfferingName})
table.Append([]string{"Disk", fmt.Sprintf("%d GB", volume.Size>>30)})
table.Append([]string{"Instance Hostname", vm.Name})
table.Append([]string{"Instance Display Name", vm.DisplayName})
username, ok := temp.Details["username"]
if !ok {
return fmt.Errorf("template %q: failed to get username", temp.Name)
}
table.Append([]string{"Instance Username", username})
table.Append([]string{"Created on", vm.Created})
table.Append([]string{"Base SSH Key", vm.KeyPair})
sgs := getSecurityGroup(vm)
sgName := strings.Join(sgs, " - ")
table.Append([]string{"Security Groups", sgName})
ags := getAffinityGroups(vm)
table.Append([]string{"Affinity Groups", strings.Join(ags, " - ")})
table.Append([]string{"Instance IP", vm.IP().String()})
table.Append([]string{"ID", vm.ID.String()})
table.Render()
return nil
}
func init() {
vmCmd.AddCommand(vmShowCmd)
}