-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas_show.go
226 lines (193 loc) · 8.39 KB
/
dbaas_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
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type dbServiceNotificationListItemOutput struct {
Level string `json:"level"`
Message string `json:"message"`
Type string `json:"type"`
}
type dbServiceNotificationListOutput []dbServiceNotificationListItemOutput
func (o *dbServiceNotificationListOutput) ToJSON() { output.JSON(o) }
func (o *dbServiceNotificationListOutput) ToText() { output.Text(o) }
func (o *dbServiceNotificationListOutput) ToTable() {
t := table.NewTable(os.Stdout)
defer t.Render()
t.SetHeader([]string{"Level", "Message"})
for _, notification := range *o {
t.Append([]string{
notification.Level,
notification.Message,
})
}
}
type dbServiceBackupListItemOutput struct {
Name string `json:"name"`
Date time.Time `json:"date"`
Size int64 `json:"size"`
}
type dbServiceBackupListOutput []dbServiceBackupListItemOutput
func (o *dbServiceBackupListOutput) ToJSON() { output.JSON(o) }
func (o *dbServiceBackupListOutput) ToText() { output.Text(o) }
func (o *dbServiceBackupListOutput) ToTable() { output.Table(o) }
type dbServiceMaintenanceShowOutput struct {
DOW string `json:"dow"`
Time string `json:"time"`
}
type dbServiceShowOutput struct {
CreationDate time.Time `json:"creation_date"`
DiskSize int64 `json:"disk_size"`
Maintenance *dbServiceMaintenanceShowOutput `json:"maintenance"`
Name string `json:"name"`
NodeCPUs int64 `json:"node_cpus"`
NodeMemory int64 `json:"node_memory"`
Nodes int64 `json:"nodes"`
Plan string `json:"plan"`
State string `json:"state"`
TerminationProtection bool `json:"termination_protection"`
Type string `json:"type"`
UpdateDate time.Time `json:"update_date"`
Zone string `json:"zone"`
Grafana *dbServiceGrafanaShowOutput `json:"grafana,omitempty"`
Kafka *dbServiceKafkaShowOutput `json:"kafka,omitempty"`
Mysql *dbServiceMysqlShowOutput `json:"mysql,omitempty"`
PG *dbServicePGShowOutput `json:"pg,omitempty"`
Redis *dbServiceRedisShowOutput `json:"redis,omitempty"`
Opensearch *dbServiceOpensearchShowOutput `json:"opensearch,omitempty"`
}
func (o *dbServiceShowOutput) ToJSON() { output.JSON(o) }
func (o *dbServiceShowOutput) ToText() { output.Text(o) }
func (o *dbServiceShowOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Database Service"})
defer t.Render()
t.Append([]string{"Zone", o.Zone})
t.Append([]string{"Name", o.Name})
t.Append([]string{"Type", o.Type})
t.Append([]string{"Plan", o.Plan})
t.Append([]string{"Disk Size", humanize.IBytes(uint64(o.DiskSize))})
t.Append([]string{"State", o.State})
t.Append([]string{"Creation Date", fmt.Sprint(o.CreationDate)})
t.Append([]string{"Update Date", fmt.Sprint(o.UpdateDate)})
t.Append([]string{"Nodes", fmt.Sprint(o.Nodes)})
t.Append([]string{"Node CPUs", fmt.Sprint(o.NodeCPUs)})
t.Append([]string{"Node Memory", humanize.IBytes(uint64(o.NodeMemory))})
t.Append([]string{"Termination Protected", fmt.Sprint(o.TerminationProtection)})
t.Append([]string{"Maintenance", func() string {
if o.Maintenance != nil {
return fmt.Sprintf("%s (%s)", o.Maintenance.DOW, o.Maintenance.Time)
}
return "n/a"
}()})
switch {
case o.Grafana != nil:
formatDatabaseServiceGrafanaTable(t, o.Grafana)
case o.Kafka != nil:
formatDatabaseServiceKafkaTable(t, o.Kafka)
case o.Opensearch != nil:
formatDatabaseServiceOpensearchTable(t, o.Opensearch)
case o.Mysql != nil:
formatDatabaseServiceMysqlTable(t, o.Mysql)
case o.PG != nil:
formatDatabaseServicePGTable(t, o.PG)
case o.Redis != nil:
formatDatabaseServiceRedisTable(t, o.Redis)
}
}
type dbaasServiceShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
Name string `cli-arg:"#"`
ShowBackups bool `cli-flag:"backups" cli-usage:"show Database Service backups"`
ShowNotifications bool `cli-flag:"notifications" cli-usage:"show Database Service notifications"`
ShowSettings string `cli-flag:"settings" cli-usage:"show Database Service settings (see \"exo dbaas type show --help\" for supported settings)"`
ShowURI bool `cli-flag:"uri" cli-usage:"show Database Service connection URI"`
Zone string `cli-short:"z" cli-usage:"Database Service zone"`
}
func (c *dbaasServiceShowCmd) cmdAliases() []string { return gShowAlias }
func (c *dbaasServiceShowCmd) cmdShort() string { return "Show a Database Service details" }
func (c *dbaasServiceShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Database Service details.
Supported output template annotations:
* When showing a Database Service: %s
- .Kafka: %s
- .Kafka.ACL[]: %s
- .Kafka.AuthenticationMethods: %s
- .Kafka.Components[]: %s
- .Kafka.ConnectionInfo: %s
- .Kafka.Users[]: %s
- .Opensearch: %s
- .Mysql: %s
- .Mysql.Components[]: %s
- .Mysql.Users[]: %s
- .PG: %s
- .PG.Components[]: %s
- .PG.ConnectionPools: %s
- .PG.Users[]: %s
- .Redis: %s
- .Redis.Components[]: %s
- .Redis.Users[]: %s
* When showing a Database Service backups: %s
* When showing a Database Service notifications: %s`,
strings.Join(output.TemplateAnnotations(&dbServiceShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceKafkaShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceKafkaACLShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceKafkaAuthenticationShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceKafkaComponentShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceKafkaConnectionInfoShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceKafkaUserShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceOpensearchShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceMysqlShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceMysqlComponentShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceMysqlUserShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServicePGShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServicePGComponentShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServicePGConnectionPool{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServicePGUserShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceRedisShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceRedisComponentShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceRedisUserShowOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceBackupListItemOutput{}), ", "),
strings.Join(output.TemplateAnnotations(&dbServiceNotificationListItemOutput{}), ", "))
}
func (c *dbaasServiceShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *dbaasServiceShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, c.Zone))
dbType, err := dbaasGetType(ctx, c.Name, c.Zone)
if err != nil {
return err
}
switch dbType {
case "grafana":
return c.outputFunc(c.showDatabaseServiceGrafana(ctx))
case "kafka":
return c.outputFunc(c.showDatabaseServiceKafka(ctx))
case "opensearch":
return c.outputFunc(c.showDatabaseServiceOpensearch(ctx))
case "mysql":
return c.outputFunc(c.showDatabaseServiceMysql(ctx))
case "pg":
return c.outputFunc(c.showDatabaseServicePG(ctx))
case "redis":
return c.outputFunc(c.showDatabaseServiceRedis(ctx))
default:
return fmt.Errorf("unsupported service type %q", dbType)
}
}
func init() {
cobra.CheckErr(registerCLICommand(dbaasCmd, &dbaasServiceShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}