-
Notifications
You must be signed in to change notification settings - Fork 20
/
zone.go
80 lines (62 loc) · 1.93 KB
/
zone.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
package cmd
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
)
var (
// allZones represents the list of known Exoscale zones, in case we need it without performing API lookup.
allZones = []string{
string(oapi.ZoneNameAtVie1),
string(oapi.ZoneNameAtVie2),
string(oapi.ZoneNameBgSof1),
string(oapi.ZoneNameChDk2),
string(oapi.ZoneNameChGva2),
string(oapi.ZoneNameDeFra1),
string(oapi.ZoneNameDeMuc1),
}
)
type zoneListItemOutput struct {
Name string `json:"name"`
}
type zoneListOutput []zoneListItemOutput
func (o *zoneListOutput) ToJSON() { output.JSON(o) }
func (o *zoneListOutput) ToText() { output.Text(o) }
func (o *zoneListOutput) ToTable() { output.Table(o) }
func (o zoneListOutput) Len() int { return len(o) }
func (o zoneListOutput) Swap(x, y int) { o[x], o[y] = o[y], o[x] }
func (o zoneListOutput) Less(x, y int) bool { return o[x].Name < o[y].Name }
func init() {
RootCmd.AddCommand(&cobra.Command{
Use: "zone",
Aliases: []string{"zones"},
Short: "List all available zones",
Long: fmt.Sprintf(`This command lists available Exoscale zones.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&zoneListOutput{}), ", ")),
RunE: func(cmd *cobra.Command, args []string) error {
return printOutput(listZones())
},
})
}
func listZones() (output.Outputter, error) {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone))
zones, err := globalstate.EgoscaleClient.ListZones(ctx)
if err != nil {
return nil, err
}
out := zoneListOutput{}
for _, zone := range zones {
out = append(out, zoneListItemOutput{
Name: zone,
})
}
sort.Sort(out)
return &out, nil
}