-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_pool_show.go
194 lines (163 loc) · 6.59 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
185
186
187
188
189
190
191
192
193
194
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/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)
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() { output.JSON(o) }
func (o *instancePoolShowOutput) ToText() { output.Text(o) }
func (o *instancePoolShowOutput) ToTable() { output.Table(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(output.TemplateAnnotations(&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(account.CurrentAccount.Environment, c.Zone))
instancePool, err := globalstate.EgoscaleClient.FindInstancePool(ctx, c.Zone, c.InstancePool)
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 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: utils.DefaultString(instancePool.Description, ""),
DiskSize: humanize.IBytes(uint64(*instancePool.DiskSize << 30)),
ElasticIPs: make([]string, 0),
ID: *instancePool.ID,
IPv6: utils.DefaultBool(instancePool.IPv6Enabled, false),
InstancePrefix: utils.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: utils.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 := 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)
}
}
if instancePool.ElasticIPIDs != nil {
for _, id := range *instancePool.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())
}
}
if instancePool.InstanceIDs != nil {
for _, id := range *instancePool.InstanceIDs {
instance, err := globalstate.EgoscaleClient.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 := globalstate.EgoscaleClient.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 := 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 instancePool.SecurityGroupIDs != nil {
for _, id := range *instancePool.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, *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(),
}))
}