-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas_show_pg.go
280 lines (250 loc) · 7.63 KB
/
dbaas_show_pg.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
package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale/v2/oapi"
"github.com/mitchellh/go-wordwrap"
)
type dbServicePGConnectionPool struct {
ConnectionURI string `json:"connection_uri"`
Database string `json:"database"`
Mode string `json:"mode"`
Name string `json:"name"`
Size int64 `json:"size"`
Username string `json:"username"`
}
type dbServicePGComponentShowOutput struct {
Component string `json:"component"`
Host string `json:"host"`
Port int64 `json:"port"`
Route string `json:"route"`
Usage string `json:"usage"`
}
type dbServicePGUserShowOutput struct {
AllowReplication bool `json:"allow_replication,omitempty"`
Password string `json:"password,omitempty"`
Type string `json:"type,omitempty"`
Username string `json:"username,omitempty"`
}
type dbServicePGShowOutput struct {
BackupSchedule string `json:"backup_schedule"`
Components []dbServicePGComponentShowOutput `json:"components"`
ConnectionPools []dbServicePGConnectionPool `json:"connection_pools"`
IPFilter []string `json:"ip_filter"`
URI string `json:"uri"`
URIParams map[string]interface{} `json:"uri_params"`
Users []dbServicePGUserShowOutput `json:"users"`
Version string `json:"version"`
}
func formatDatabaseServicePGTable(t *table.Table, o *dbServicePGShowOutput) {
t.Append([]string{"Version", o.Version})
t.Append([]string{"Backup Schedule", o.BackupSchedule})
t.Append([]string{"URI", redactDatabaseServiceURI(o.URI)})
t.Append([]string{"IP Filter", strings.Join(o.IPFilter, ", ")})
t.Append([]string{"Components", func() string {
buf := bytes.NewBuffer(nil)
ct := table.NewEmbeddedTable(buf)
ct.SetHeader([]string{" "})
for _, c := range o.Components {
ct.Append([]string{
c.Component,
fmt.Sprintf("%s:%d", c.Host, c.Port),
"route:" + c.Route,
"usage:" + c.Usage,
})
}
ct.Render()
return buf.String()
}()})
if len(o.ConnectionPools) > 0 {
t.Append([]string{"Connection Pools", func() string {
buf := bytes.NewBuffer(nil)
pt := table.NewEmbeddedTable(buf)
pt.SetHeader([]string{" "})
for _, pool := range o.ConnectionPools {
pt.Append([]string{
pool.Name,
"database:" + pool.Database,
"size:" + fmt.Sprint(pool.Size),
"mode:" + pool.Mode,
})
}
pt.Render()
return buf.String()
}()})
}
t.Append([]string{"Users", func() string {
if len(o.Users) > 0 {
return strings.Join(
func() []string {
users := make([]string, len(o.Users))
for i := range o.Users {
users[i] = fmt.Sprintf("%s (%s)", o.Users[i].Username, o.Users[i].Type)
}
return users
}(),
"\n")
}
return "n/a"
}()})
}
func (c *dbaasServiceShowCmd) showDatabaseServicePG(ctx context.Context) (outputter, error) {
res, err := cs.GetDbaasServicePgWithResponse(ctx, oapi.DbaasServiceName(c.Name))
if err != nil {
return nil, err
}
if res.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("API request error: unexpected status %s", res.Status())
}
databaseService := res.JSON200
switch {
case c.ShowBackups:
out := make(dbServiceBackupListOutput, 0)
if databaseService.Backups != nil {
for _, b := range *databaseService.Backups {
out = append(out, dbServiceBackupListItemOutput{
Date: b.BackupTime,
Name: b.BackupName,
Size: b.DataSize,
})
}
}
return &out, nil
case c.ShowNotifications:
out := make(dbServiceNotificationListOutput, 0)
if databaseService.Notifications != nil {
for _, n := range *databaseService.Notifications {
out = append(out, dbServiceNotificationListItemOutput{
Level: string(n.Level),
Message: wordwrap.WrapString(n.Message, 50),
Type: string(n.Type),
})
}
}
return &out, nil
case c.ShowSettings != "":
var serviceSettings *map[string]interface{}
switch c.ShowSettings {
case "pg":
serviceSettings = databaseService.PgSettings
case "pgbouncer":
serviceSettings = databaseService.PgbouncerSettings
case "pglookout":
serviceSettings = databaseService.PglookoutSettings
default:
return nil, fmt.Errorf(
"invalid settings value %q, expected one of: %s",
c.ShowSettings,
strings.Join(pgSettings, ", "),
)
}
if serviceSettings != nil {
out, err := json.MarshalIndent(serviceSettings, "", " ")
if err != nil {
return nil, fmt.Errorf("unable to marshal JSON: %w", err)
}
fmt.Println(string(out))
}
return nil, nil
case c.ShowURI:
fmt.Println(defaultString(databaseService.Uri, ""))
return nil, nil
}
out := dbServiceShowOutput{
Zone: c.Zone,
Name: string(databaseService.Name),
Type: string(databaseService.Type),
Plan: databaseService.Plan,
CreationDate: *databaseService.CreatedAt,
Nodes: *databaseService.NodeCount,
NodeCPUs: *databaseService.NodeCpuCount,
NodeMemory: *databaseService.NodeMemory,
UpdateDate: *databaseService.UpdatedAt,
DiskSize: *databaseService.DiskSize,
State: string(*databaseService.State),
TerminationProtection: *databaseService.TerminationProtection,
Maintenance: func() (v *dbServiceMaintenanceShowOutput) {
if databaseService.Maintenance != nil {
v = &dbServiceMaintenanceShowOutput{
DOW: string(databaseService.Maintenance.Dow),
Time: databaseService.Maintenance.Time,
}
}
return
}(),
PG: &dbServicePGShowOutput{
BackupSchedule: func() (v string) {
if databaseService.BackupSchedule != nil {
v = fmt.Sprintf(
"%02d:%02d",
*databaseService.BackupSchedule.BackupHour,
*databaseService.BackupSchedule.BackupMinute,
)
}
return
}(),
Components: func() (v []dbServicePGComponentShowOutput) {
if databaseService.Components != nil {
for _, c := range *databaseService.Components {
v = append(v, dbServicePGComponentShowOutput{
Component: c.Component,
Host: c.Host,
Port: c.Port,
Route: string(c.Route),
Usage: string(c.Usage),
})
}
}
return
}(),
ConnectionPools: func() (v []dbServicePGConnectionPool) {
if databaseService.ConnectionPools != nil {
for _, pool := range *databaseService.ConnectionPools {
v = append(v, dbServicePGConnectionPool{
ConnectionURI: pool.ConnectionUri,
Database: pool.Database,
Mode: string(pool.Mode),
Name: pool.Name,
Size: pool.Size,
Username: pool.Username,
})
}
}
return
}(),
IPFilter: func() (v []string) {
if databaseService.IpFilter != nil {
v = *databaseService.IpFilter
}
return
}(),
URI: *databaseService.Uri,
URIParams: *databaseService.UriParams,
Users: func() (v []dbServicePGUserShowOutput) {
if databaseService.Users != nil {
for _, u := range *databaseService.Users {
v = append(v, dbServicePGUserShowOutput{
AllowReplication: func() (v bool) {
if u.AccessControl != nil {
v = defaultBool(u.AccessControl.AllowReplication, false)
}
return
}(),
Password: defaultString(u.Password, ""),
Type: defaultString(u.Type, ""),
Username: defaultString(u.Username, ""),
})
}
}
return
}(),
Version: defaultString(databaseService.Version, ""),
},
}
return &out, nil
}