-
Notifications
You must be signed in to change notification settings - Fork 19
/
vm_show.go
117 lines (99 loc) · 3.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
package cmd
import (
"fmt"
"strings"
humanize "github.com/dustin/go-humanize"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type vmShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
CreationDate string `json:"creation_date"`
Size string `json:"size"`
DiskSize string `json:"disk_size"`
Template string `json:"template"`
Zone string `json:"zone"`
State string `json:"state"`
IPAddress string `json:"ip_address"`
Username string `json:"username"`
SSHKey string `json:"ssh_key"`
SecurityGroups []string `json:"security_groups,omitempty"`
AntiAffinityGroups []string `json:"antiaffinity_groups,omitempty" outputLabel:"Anti-Affinity Groups"`
PrivateNetworks []string `json:"private_networks,omitempty"`
}
func (o *vmShowOutput) Type() string { return "Instance" }
func (o *vmShowOutput) toJSON() { outputJSON(o) }
func (o *vmShowOutput) toText() { outputText(o) }
func (o *vmShowOutput) toTable() { outputTable(o) }
func init() {
vmCmd.AddCommand(&cobra.Command{
Use: "show <name | id>",
Short: "Show a virtual machine details",
Long: fmt.Sprintf(`This command shows a Compute instance details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&vmShowOutput{}), ", ")),
Aliases: gShowAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
return output(showVM(args[0]))
},
})
}
func showVM(name string) (outputter, error) {
vm, err := getVirtualMachineByNameOrID(name)
if err != nil {
return nil, err
}
resp, err := cs.GetWithContext(gContext, &egoscale.Template{
IsFeatured: true,
ID: vm.TemplateID,
ZoneID: vm.ZoneID,
})
if err != nil {
return nil, err
}
template := resp.(*egoscale.Template)
resp, err = cs.GetWithContext(gContext, &egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
})
if err != nil {
return nil, err
}
volume := resp.(*egoscale.Volume)
out := vmShowOutput{
ID: vm.ID.String(),
Name: vm.DisplayName,
CreationDate: vm.Created,
Size: vm.ServiceOfferingName,
Template: vm.TemplateName,
Zone: vm.ZoneName,
State: vm.State,
DiskSize: humanize.IBytes(uint64(volume.Size)),
IPAddress: vm.IP().String(),
Username: "n/a",
SSHKey: vm.KeyPair,
SecurityGroups: make([]string, len(vm.SecurityGroup)),
AntiAffinityGroups: make([]string, len(vm.AffinityGroup)),
PrivateNetworks: make([]string, 0),
}
for i, sg := range vm.SecurityGroup {
out.SecurityGroups[i] = sg.Name
}
for i, aag := range vm.AffinityGroup {
out.AntiAffinityGroups[i] = aag.Name
}
for _, nic := range vm.Nic {
if nic.IsDefault {
continue
}
out.PrivateNetworks = append(out.PrivateNetworks, nic.NetworkName)
}
if username, ok := template.Details["username"]; ok {
out.Username = username
}
return &out, nil
}