-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_show.go
203 lines (171 loc) · 6.66 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/pkg/userdata"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)
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"`
DeployTarget string `json:"deploy_target"`
SecurityGroups []string `json:"security_groups"`
PrivateInstance string `json:"private-instance" outputLabel:"Private Instance"`
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"`
ReverseDNS string `json:"reverse_dns" outputLabel:"Reverse DNS"`
}
func (o *instanceShowOutput) Type() string { return "Compute instance" }
func (o *instanceShowOutput) ToJSON() { output.JSON(o) }
func (o *instanceShowOutput) ToText() { output.Text(o) }
func (o *instanceShowOutput) ToTable() { output.Table(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(output.TemplateAnnotations(&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(account.CurrentAccount.Environment, c.Zone))
instance, err := globalstate.EgoscaleClient.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 := userdata.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: utils.DefaultIP(instance.PublicIPAddress, "-"),
IPv6Address: utils.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: utils.DefaultString(instance.SSHKey, "-"),
SecurityGroups: make([]string, 0),
State: *instance.State,
Zone: c.Zone,
}
out.PrivateInstance = "No"
if instance.PublicIPAssignment != nil && *instance.PublicIPAssignment == "none" {
out.PrivateInstance = "Yes"
}
if instance.AntiAffinityGroupIDs != nil {
for _, id := range *instance.AntiAffinityGroupIDs {
antiAffinityGroup, err := globalstate.EgoscaleClient.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)
}
}
out.DeployTarget = "-"
if instance.DeployTargetID != nil {
DeployTarget, err := globalstate.EgoscaleClient.GetDeployTarget(ctx, c.Zone, *instance.DeployTargetID)
if err != nil {
return fmt.Errorf("error retrieving Deploy Target: %w", err)
}
out.DeployTarget = *DeployTarget.Name
}
if instance.ElasticIPIDs != nil {
for _, id := range *instance.ElasticIPIDs {
elasticIP, err := globalstate.EgoscaleClient.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 := globalstate.EgoscaleClient.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 := globalstate.EgoscaleClient.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 := globalstate.EgoscaleClient.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 := globalstate.EgoscaleClient.GetTemplate(ctx, c.Zone, *instance.TemplateID)
if err != nil {
return err
}
out.Template = *template.Name
rdns, err := globalstate.EgoscaleClient.GetInstanceReverseDNS(ctx, c.Zone, *instance.ID)
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
out.ReverseDNS = ""
} else {
return err
}
}
out.ReverseDNS = rdns
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(instanceCmd, &instanceShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}