-
Notifications
You must be signed in to change notification settings - Fork 18
/
instance_list.go
133 lines (106 loc) · 3.34 KB
/
instance_list.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
package cmd
import (
"fmt"
"os"
"strings"
"sync"
"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"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
const (
emptyIPAddressVisualization = "-"
)
type instanceListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Zone string `json:"zone"`
Type string `json:"type"`
IPAddress string `json:"ip_address"`
State string `json:"state"`
}
type instanceListOutput []instanceListItemOutput
func (o *instanceListOutput) ToJSON() { output.JSON(o) }
func (o *instanceListOutput) ToText() { output.Text(o) }
func (o *instanceListOutput) ToTable() { output.Table(o) }
type instanceListCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list"`
Zone string `cli-short:"z" cli-usage:"zone to filter results to"`
}
func (c *instanceListCmd) cmdAliases() []string { return gListAlias }
func (c *instanceListCmd) cmdShort() string { return "List Compute instances" }
func (c *instanceListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists Compute instances.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&instanceListItemOutput{}), ", "))
}
func (c *instanceListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceListCmd) cmdRun(_ *cobra.Command, _ []string) error {
var zones []string
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
}
out := make(instanceListOutput, 0)
res := make(chan instanceListItemOutput)
done := make(chan struct{})
var instanceTypes sync.Map
go func() {
for instance := range res {
out = append(out, instance)
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
list, err := globalstate.EgoscaleClient.ListInstances(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Compute instances in zone %s: %w", zone, err)
}
for _, i := range list {
var instanceType *egoscale.InstanceType
instanceTypeI, cached := instanceTypes.Load(*i.InstanceTypeID)
if cached {
instanceType = instanceTypeI.(*egoscale.InstanceType)
} else {
instanceType, err = globalstate.EgoscaleClient.GetInstanceType(ctx, zone, *i.InstanceTypeID)
if err != nil {
return fmt.Errorf(
"unable to retrieve Compute instance type %q: %w",
*i.InstanceTypeID,
err)
}
instanceTypes.Store(*i.InstanceTypeID, instanceType)
}
res <- instanceListItemOutput{
ID: *i.ID,
Name: *i.Name,
Zone: zone,
Type: fmt.Sprintf("%s.%s", *instanceType.Family, *instanceType.Size),
IPAddress: utils.DefaultIP(i.PublicIPAddress, emptyIPAddressVisualization),
State: *i.State,
}
}
return nil
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
}
close(res)
<-done
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(instanceCmd, &instanceListCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}