-
Notifications
You must be signed in to change notification settings - Fork 18
/
sks_show.go
129 lines (112 loc) · 3.54 KB
/
sks_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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/cli/table"
)
type sksShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreationDate string `json:"creation_date"`
Zone string `json:"zone"`
Endpoint string `json:"endpoint"`
Version string `json:"version"`
ServiceLevel string `json:"service_level"`
CNI string `json:"cni"`
AddOns []string `json:"addons"`
State string `json:"state"`
Nodepools []sksNodepoolShowOutput `json:"nodepools"`
}
func (o *sksShowOutput) toJSON() { outputJSON(o) }
func (o *sksShowOutput) toText() { outputText(o) }
func (o *sksShowOutput) toTable() {
t := table.NewTable(os.Stdout)
defer t.Render()
t.SetHeader([]string{"SKS Cluster"})
t.Append([]string{"ID", o.ID})
t.Append([]string{"Name", o.Name})
t.Append([]string{"Description", o.Description})
t.Append([]string{"Zone", o.Zone})
t.Append([]string{"Creation Date", o.CreationDate})
t.Append([]string{"Endpoint", o.Endpoint})
t.Append([]string{"Version", o.Version})
t.Append([]string{"Service Level", o.ServiceLevel})
t.Append([]string{"CNI", o.CNI})
t.Append([]string{"Add-Ons", strings.Join(o.AddOns, "\n")})
t.Append([]string{"State", o.State})
t.Append([]string{"Nodepools", func() string {
if len(o.Nodepools) > 0 {
return strings.Join(
func() []string {
services := make([]string, len(o.Nodepools))
for i := range o.Nodepools {
services[i] = fmt.Sprintf("%s | %s",
o.Nodepools[i].ID,
o.Nodepools[i].Name)
}
return services
}(),
"\n")
}
return "n/a"
}()})
}
var sksShowCmd = &cobra.Command{
Use: "show NAME|ID",
Short: "Show a SKS cluster details",
Long: fmt.Sprintf(`This command shows a SKS cluster details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&sksShowOutput{}), ", ")),
Aliases: gShowAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
return output(showSKSCluster(zone, args[0]))
},
}
func showSKSCluster(zone, c string) (outputter, error) {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
cluster, err := lookupSKSCluster(ctx, zone, c)
if err != nil {
return nil, err
}
sksNodepools := make([]sksNodepoolShowOutput, 0)
for _, np := range cluster.Nodepools {
sksNodepools = append(sksNodepools, sksNodepoolShowOutput{
ID: np.ID,
Name: np.Name,
})
}
out := sksShowOutput{
ID: cluster.ID,
Name: cluster.Name,
Description: cluster.Description,
CreationDate: cluster.CreatedAt.String(),
Version: cluster.Version,
ServiceLevel: cluster.ServiceLevel,
CNI: cluster.CNI,
AddOns: cluster.AddOns,
Zone: zone,
Endpoint: cluster.Endpoint,
State: cluster.State,
Nodepools: sksNodepools,
}
return &out, nil
}
func init() {
sksShowCmd.Flags().StringP("zone", "z", "", "SKS cluster zone")
sksCmd.AddCommand(sksShowCmd)
}