-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_show.go
169 lines (142 loc) · 5.39 KB
/
instance_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/dustin/go-humanize"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type instanceShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
CreationDate string `json:"creation_date"`
InstanceType string `json:"instance_type"`
Template string `json:"template_id"`
Zone string `json:"zoneid"`
AntiAffinityGroups []string `json:"anti_affinity_groups" outputLabel:"Anti-Affinity Groups"`
SecurityGroups []string `json:"security_groups"`
PrivateNetworks []string `json:"private_networks"`
ElasticIPs []string `json:"elastic_ips" outputLabel:"Elastic IPs"`
IPAddress string `json:"ip_address"`
IPv6Address string `json:"ipv6_address" outputLabel:"IPv6 Address"`
SSHKey string `json:"ssh_key"`
DiskSize string `json:"disk_size"`
State string `json:"state"`
Labels map[string]string `json:"labels"`
}
func (o *instanceShowOutput) Type() string { return "Compute instance" }
func (o *instanceShowOutput) toJSON() { outputJSON(o) }
func (o *instanceShowOutput) toText() { outputText(o) }
func (o *instanceShowOutput) toTable() { outputTable(o) }
type instanceShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Instance string `cli-arg:"#" cli-usage:"NAME|ID"`
ShowUserData bool `cli-flag:"user-data" cli-short:"u" cli-usage:"show instance cloud-init user data configuration"`
Zone string `cli-short:"z" cli-usage:"instance zone"`
}
func (c *instanceShowCmd) cmdAliases() []string { return gShowAlias }
func (c *instanceShowCmd) cmdShort() string { return "Show a Compute instance details" }
func (c *instanceShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Compute instance details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&instanceShowOutput{}), ", "))
}
func (c *instanceShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceShowCmd) cmdRun(cmd *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
instance, err := cs.FindInstance(ctx, c.Zone, c.Instance)
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return fmt.Errorf("resource not found in zone %q", c.Zone)
}
return err
}
if c.ShowUserData {
if instance.UserData != nil {
userData, err := decodeUserData(*instance.UserData)
if err != nil {
return fmt.Errorf("error decoding user data: %w", err)
}
cmd.Print(userData)
}
return nil
}
out := instanceShowOutput{
AntiAffinityGroups: make([]string, 0),
CreationDate: instance.CreatedAt.String(),
DiskSize: humanize.IBytes(uint64(*instance.DiskSize << 30)),
ElasticIPs: make([]string, 0),
ID: *instance.ID,
IPAddress: defaultIP(instance.PublicIPAddress, "-"),
IPv6Address: defaultIP(instance.IPv6Address, "-"),
Labels: func() (v map[string]string) {
if instance.Labels != nil {
v = *instance.Labels
}
return
}(),
Name: *instance.Name,
PrivateNetworks: make([]string, 0),
SSHKey: defaultString(instance.SSHKey, "-"),
SecurityGroups: make([]string, 0),
State: *instance.State,
Zone: c.Zone,
}
if instance.AntiAffinityGroupIDs != nil {
for _, id := range *instance.AntiAffinityGroupIDs {
antiAffinityGroup, err := cs.GetAntiAffinityGroup(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Anti-Affinity Group: %w", err)
}
out.AntiAffinityGroups = append(out.AntiAffinityGroups, *antiAffinityGroup.Name)
}
}
if instance.ElasticIPIDs != nil {
for _, id := range *instance.ElasticIPIDs {
elasticIP, err := cs.GetElasticIP(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Elastic IP: %w", err)
}
out.ElasticIPs = append(out.ElasticIPs, elasticIP.IPAddress.String())
}
}
instanceType, err := cs.GetInstanceType(ctx, c.Zone, *instance.InstanceTypeID)
if err != nil {
return err
}
out.InstanceType = fmt.Sprintf("%s.%s", *instanceType.Family, *instanceType.Size)
if instance.PrivateNetworkIDs != nil {
for _, id := range *instance.PrivateNetworkIDs {
privateNetwork, err := cs.GetPrivateNetwork(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Private Network: %w", err)
}
out.PrivateNetworks = append(out.PrivateNetworks, *privateNetwork.Name)
}
}
if instance.SecurityGroupIDs != nil {
for _, id := range *instance.SecurityGroupIDs {
securityGroup, err := cs.GetSecurityGroup(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Security Group: %w", err)
}
out.SecurityGroups = append(out.SecurityGroups, *securityGroup.Name)
}
}
template, err := cs.GetTemplate(ctx, c.Zone, *instance.TemplateID)
if err != nil {
return err
}
out.Template = *template.Name
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(instanceCmd, &instanceShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}