-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas_type_show.go
339 lines (285 loc) · 9.3 KB
/
dbaas_type_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cmd
import (
"fmt"
"os"
"strings"
"github.com/dustin/go-humanize"
"github.com/exoscale/cli/table"
exo "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type dbaasTypePlanListItemOutput struct {
Name string `json:"name"`
Nodes int64 `json:"nodes"`
NodeCPUs int64 `json:"node_cpus"`
NodeMemory int64 `json:"node_memory"`
DiskSpace int64 `json:"disk_space"`
Authorized bool `json:"authorized"`
}
type dbaasTypePlanListOutput []dbaasTypePlanListItemOutput
func (o *dbaasTypePlanListOutput) toJSON() { outputJSON(o) }
func (o *dbaasTypePlanListOutput) toText() { outputText(o) }
func (o *dbaasTypePlanListOutput) toTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Name", "# Nodes", "# CPUs", "Node Memory", "Disk Space", "Authorized"})
defer t.Render()
for _, p := range *o {
t.Append([]string{
p.Name,
fmt.Sprint(p.Nodes),
fmt.Sprint(p.NodeCPUs),
humanize.Bytes(uint64(p.NodeMemory)),
humanize.Bytes(uint64(p.DiskSpace)),
fmt.Sprint(p.Authorized),
})
}
}
type dbaasTypePlanBackupOutput struct {
Interval *int64 `json:"interval"`
MaxCount *int64 `json:"max_count"`
RecoveryMode *string `json:"recovery_mode"`
FrequentIntervalMinutes *int64 `json:"frequent_interval_minutes"`
FrequentOldestAgeMinutes *int64 `json:"frequent_oldest_age_minutes"`
InfrequentIntervalMinutes *int64 `json:"infrequent_interval_minutes"`
InfrequentOldestAgeMinutes *int64 `json:"infrequent_oldest_age_minutes"`
}
func (o *dbaasTypePlanBackupOutput) toJSON() { outputJSON(o) }
func (o *dbaasTypePlanBackupOutput) toText() { outputText(o) }
func (o *dbaasTypePlanBackupOutput) toTable() {
t := table.NewTable(os.Stdout)
defer t.Render()
t.Append([]string{"Backup interval (hours)", Int64PtrFormatOutput(o.Interval)})
t.Append([]string{"Max backups", Int64PtrFormatOutput(o.MaxCount)})
t.Append([]string{"Recovery mode", defaultString(o.RecoveryMode, "")})
t.Append([]string{"Frequent backup interval", Int64PtrFormatOutput(o.FrequentIntervalMinutes)})
t.Append([]string{"Frequent backup max age", Int64PtrFormatOutput(o.FrequentOldestAgeMinutes)})
t.Append([]string{"Infrequent backup interval", Int64PtrFormatOutput(o.InfrequentIntervalMinutes)})
t.Append([]string{"Infrequent backup max age", Int64PtrFormatOutput(o.InfrequentOldestAgeMinutes)})
}
type dbaasTypeShowOutput struct {
Name string `json:"name"`
Description string `json:"description"`
AvailableVersions []string `json:"available_versions"`
DefaultVersion string `json:"default_version"`
}
func (o *dbaasTypeShowOutput) toJSON() { outputJSON(o) }
func (o *dbaasTypeShowOutput) toText() { outputText(o) }
func (o *dbaasTypeShowOutput) toTable() {
t := table.NewTable(os.Stdout)
defer t.Render()
t.Append([]string{"Name", o.Name})
t.Append([]string{"Description", o.Description})
t.Append([]string{"Available Versions", strings.Join(o.AvailableVersions, ", ")})
t.Append([]string{"Default Version", o.DefaultVersion})
}
var (
opensearchSettings = []string{"opensearch"}
kafkaSettings = []string{
"kafka",
"kafka-rest",
"kafka-connect",
"schema-registry",
}
mysqlSettings = []string{"mysql"}
pgSettings = []string{
"pg",
"pgbouncer",
"pglookout",
}
redisSettings = []string{"redis"}
)
type dbaasTypeShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Name string `cli-arg:"#"`
ShowPlans bool `cli-flag:"plans" cli-usage:"list plans offered for the Database Service type"`
ShowSettings string `cli-flag:"settings" cli-usage:"show settings supported by the Database Service type"`
ShowBackupConfig string `cli-flag:"backup-config" cli-usage:"show backup configuration for the Database Service type and Plan"`
}
func (c *dbaasTypeShowCmd) cmdAliases() []string { return gShowAlias }
func (c *dbaasTypeShowCmd) cmdShort() string { return "Show a Database Service type details" }
func (c *dbaasTypeShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Database Service type details.
Supported Database Service type settings:
* %s
* %s
* %s
* %s
* %s
Supported output template annotations:
* When showing a Database Service: %s
* When listing Database Service plans: %s`,
strings.Join(opensearchSettings, ", "),
strings.Join(kafkaSettings, ", "),
strings.Join(mysqlSettings, ", "),
strings.Join(pgSettings, ", "),
strings.Join(redisSettings, ", "),
strings.Join(outputterTemplateAnnotations(&dbaasTypeShowOutput{}), ", "),
strings.Join(outputterTemplateAnnotations(&dbaasTypePlanListItemOutput{}), ", "))
}
func (c *dbaasTypeShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *dbaasTypeShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(
gContext,
exoapi.NewReqEndpoint(gCurrentAccount.Environment, gCurrentAccount.DefaultZone),
)
dt, err := cs.GetDatabaseServiceType(ctx, gCurrentAccount.DefaultZone, c.Name)
if err != nil {
return err
}
if c.ShowPlans {
out := make(dbaasTypePlanListOutput, len(dt.Plans))
for i := range dt.Plans {
out[i] = dbaasTypePlanListItemOutput{
Name: *dt.Plans[i].Name,
Nodes: *dt.Plans[i].Nodes,
NodeCPUs: *dt.Plans[i].NodeCPUs,
NodeMemory: *dt.Plans[i].NodeMemory,
DiskSpace: *dt.Plans[i].DiskSpace,
Authorized: *dt.Plans[i].Authorized,
}
}
return c.outputFunc(&out, nil)
}
if c.ShowSettings != "" {
var settings map[string]interface{}
switch c.Name {
case "kafka":
if !isInList(kafkaSettings, c.ShowSettings) {
return fmt.Errorf(
"invalid settings value %q, expected one of: %s",
c.ShowSettings,
strings.Join(kafkaSettings, ", "),
)
}
res, err := cs.GetDbaasSettingsKafkaWithResponse(ctx)
if err != nil {
return err
}
switch c.ShowSettings {
case "kafka":
settings = *res.JSON200.Settings.Kafka.Properties
case "kafka-connect":
settings = *res.JSON200.Settings.KafkaConnect.Properties
case "kafka-rest":
settings = *res.JSON200.Settings.KafkaRest.Properties
case "schema-registry":
settings = *res.JSON200.Settings.SchemaRegistry.Properties
}
dbaasShowSettings(settings)
case "opensearch":
if !isInList(opensearchSettings, c.ShowSettings) {
return fmt.Errorf(
"invalid settings value %q, expected one of: %s",
c.ShowSettings,
strings.Join(opensearchSettings, ", "),
)
}
res, err := cs.GetDbaasSettingsOpensearchWithResponse(ctx)
if err != nil {
return err
}
switch c.ShowSettings {
case "opensearch":
settings = *res.JSON200.Settings.Opensearch.Properties
}
dbaasShowSettings(settings)
case "mysql":
if !isInList(mysqlSettings, c.ShowSettings) {
return fmt.Errorf(
"invalid settings value %q, expected one of: %s",
c.ShowSettings,
strings.Join(mysqlSettings, ", "),
)
}
res, err := cs.GetDbaasSettingsMysqlWithResponse(ctx)
if err != nil {
return err
}
switch c.ShowSettings {
case "mysql":
settings = *res.JSON200.Settings.Mysql.Properties
}
dbaasShowSettings(settings)
case "pg":
if !isInList(pgSettings, c.ShowSettings) {
return fmt.Errorf(
"invalid settings value %q, expected one of: %s",
c.ShowSettings,
strings.Join(pgSettings, ", "),
)
}
res, err := cs.GetDbaasSettingsPgWithResponse(ctx)
if err != nil {
return err
}
switch c.ShowSettings {
case "pg":
settings = *res.JSON200.Settings.Pg.Properties
case "pgbouncer":
settings = *res.JSON200.Settings.Pgbouncer.Properties
case "pglookout":
settings = *res.JSON200.Settings.Pglookout.Properties
}
dbaasShowSettings(settings)
case "redis":
if !isInList(redisSettings, c.ShowSettings) {
return fmt.Errorf(
"invalid settings value %q, expected one of: %s",
c.ShowSettings,
strings.Join(redisSettings, ", "),
)
}
res, err := cs.GetDbaasSettingsRedisWithResponse(ctx)
if err != nil {
return err
}
switch c.ShowSettings {
case "redis":
settings = *res.JSON200.Settings.Redis.Properties
}
dbaasShowSettings(settings)
}
return nil
}
if c.ShowBackupConfig != "" {
var bc *exo.DatabaseBackupConfig
for _, plan := range dt.Plans {
if *plan.Name == c.ShowBackupConfig {
bc = plan.BackupConfig
break
}
}
if bc == nil {
return fmt.Errorf("%q is not a valid plan", c.ShowBackupConfig)
}
return c.outputFunc(&dbaasTypePlanBackupOutput{
Interval: bc.Interval,
MaxCount: bc.MaxCount,
RecoveryMode: bc.RecoveryMode,
FrequentIntervalMinutes: bc.FrequentIntervalMinutes,
FrequentOldestAgeMinutes: bc.FrequentOldestAgeMinutes,
InfrequentIntervalMinutes: bc.InfrequentIntervalMinutes,
InfrequentOldestAgeMinutes: bc.InfrequentOldestAgeMinutes,
}, nil)
}
return c.outputFunc(&dbaasTypeShowOutput{
Name: *dt.Name,
Description: defaultString(dt.Description, ""),
AvailableVersions: func() (v []string) {
if dt.AvailableVersions != nil {
v = *dt.AvailableVersions
}
return
}(),
DefaultVersion: defaultString(dt.DefaultVersion, "-"),
}, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(dbaasTypeCmd, &dbaasTypeShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}