-
Notifications
You must be signed in to change notification settings - Fork 18
/
instance_pool_show.go
184 lines (155 loc) · 6.11 KB
/
instance_pool_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
package cmd
import (
"fmt"
"strings"
"github.com/dustin/go-humanize"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type instancePoolShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
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"`
IPv6 bool `json:"ipv6" outputLabel:"IPv6"`
SSHKey string `json:"ssh_key"`
Size int64 `json:"size"`
DiskSize string `json:"disk_size"`
InstancePrefix string `json:"instance_prefix"`
State string `json:"state"`
Labels map[string]string `json:"labels"`
Instances []string `json:"instances"`
}
func (o *instancePoolShowOutput) Type() string { return "Instance Pool" }
func (o *instancePoolShowOutput) toJSON() { outputJSON(o) }
func (o *instancePoolShowOutput) toText() { outputText(o) }
func (o *instancePoolShowOutput) toTable() { outputTable(o) }
type instancePoolShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
InstancePool string `cli-arg:"#" cli-usage:"NAME|ID"`
ShowUserData bool `cli-flag:"user-data" cli-short:"u" cli-usage:"show cloud-init user data configuration"`
Zone string `cli-short:"z" cli-usage:"Instance Pool zone"`
}
func (c *instancePoolShowCmd) cmdAliases() []string { return gShowAlias }
func (c *instancePoolShowCmd) cmdShort() string { return "Show an Instance Pool details" }
func (c *instancePoolShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows an Instance Pool details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&instancePoolShowOutput{}), ", "))
}
func (c *instancePoolShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instancePoolShowCmd) cmdRun(cmd *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
instancePool, err := cs.FindInstancePool(ctx, c.Zone, c.InstancePool)
if err != nil {
return err
}
if c.ShowUserData {
if instancePool.UserData != nil {
userData, err := decodeUserData(*instancePool.UserData)
if err != nil {
return fmt.Errorf("error decoding user data: %w", err)
}
cmd.Print(userData)
}
return nil
}
out := instancePoolShowOutput{
AntiAffinityGroups: make([]string, 0),
Description: defaultString(instancePool.Description, ""),
DiskSize: humanize.IBytes(uint64(*instancePool.DiskSize << 30)),
ElasticIPs: make([]string, 0),
ID: *instancePool.ID,
IPv6: defaultBool(instancePool.IPv6Enabled, false),
InstancePrefix: defaultString(instancePool.InstancePrefix, ""),
Instances: make([]string, 0),
Labels: func() (v map[string]string) {
if instancePool.Labels != nil {
v = *instancePool.Labels
}
return
}(),
Name: *instancePool.Name,
PrivateNetworks: make([]string, 0),
SSHKey: defaultString(instancePool.SSHKey, "-"),
SecurityGroups: make([]string, 0),
Size: *instancePool.Size,
State: *instancePool.State,
Zone: c.Zone,
}
if instancePool.AntiAffinityGroupIDs != nil {
for _, id := range *instancePool.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 instancePool.ElasticIPIDs != nil {
for _, id := range *instancePool.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())
}
}
if instancePool.InstanceIDs != nil {
for _, id := range *instancePool.InstanceIDs {
instance, err := cs.GetInstance(ctx, c.Zone, id)
if err != nil {
return fmt.Errorf("error retrieving Compute instance: %w", err)
}
out.Instances = append(out.Instances, *instance.Name)
}
}
instanceType, err := cs.GetInstanceType(ctx, c.Zone, *instancePool.InstanceTypeID)
if err != nil {
return err
}
out.InstanceType = fmt.Sprintf("%s.%s", *instanceType.Family, *instanceType.Size)
if instancePool.PrivateNetworkIDs != nil {
for _, id := range *instancePool.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 instancePool.SecurityGroupIDs != nil {
for _, id := range *instancePool.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, *instancePool.TemplateID)
if err != nil {
return fmt.Errorf("error retrieving template: %w", err)
}
out.Template = *template.Name
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(instancePoolCmd, &instancePoolShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
// FIXME: remove this someday.
cobra.CheckErr(registerCLICommand(deprecatedInstancePoolCmd, &instancePoolShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}