-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas_show_redis.go
216 lines (191 loc) · 5.81 KB
/
dbaas_show_redis.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
package cmd
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/mitchellh/go-wordwrap"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
)
type dbServiceRedisComponentShowOutput struct {
Component string `json:"component"`
Host string `json:"host"`
Port int64 `json:"port"`
Route string `json:"route"`
Usage string `json:"usage"`
}
type dbServiceRedisUserShowOutput struct {
Password string `json:"password,omitempty"`
Type string `json:"type,omitempty"`
Username string `json:"username,omitempty"`
}
type dbServiceRedisShowOutput struct {
Components []dbServiceRedisComponentShowOutput `json:"components"`
IPFilter []string `json:"ip_filter"`
URI string `json:"uri"`
URIParams map[string]interface{} `json:"uri_params"`
Users []dbServiceRedisUserShowOutput `json:"users"`
}
func formatDatabaseServiceRedisTable(t *table.Table, o *dbServiceRedisShowOutput) {
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()
}()})
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) showDatabaseServiceRedis(ctx context.Context) (output.Outputter, error) {
res, err := globalstate.EgoscaleClient.GetDbaasServiceRedisWithResponse(ctx, oapi.DbaasServiceName(c.Name))
if err != nil {
if errors.Is(err, exoapi.ErrNotFound) {
return nil, fmt.Errorf("resource not found in zone %q", c.Zone)
}
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 "redis":
serviceSettings = databaseService.RedisSettings
default:
return nil, fmt.Errorf(
"invalid settings value %q, expected one of: %s",
c.ShowSettings,
strings.Join(redisSettings, ", "),
)
}
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(utils.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
}(),
Redis: &dbServiceRedisShowOutput{
Components: func() (v []dbServiceRedisComponentShowOutput) {
if databaseService.Components != nil {
for _, c := range *databaseService.Components {
v = append(v, dbServiceRedisComponentShowOutput{
Component: c.Component,
Host: c.Host,
Port: c.Port,
Route: string(c.Route),
Usage: string(c.Usage),
})
}
}
return
}(),
IPFilter: func() (v []string) {
if databaseService.IpFilter != nil {
v = *databaseService.IpFilter
}
return
}(),
URI: *databaseService.Uri,
URIParams: *databaseService.UriParams,
Users: func() (v []dbServiceRedisUserShowOutput) {
if databaseService.Users != nil {
for _, u := range *databaseService.Users {
v = append(v, dbServiceRedisUserShowOutput{
Password: utils.DefaultString(u.Password, ""),
Type: utils.DefaultString(u.Type, ""),
Username: utils.DefaultString(u.Username, ""),
})
}
}
return
}(),
},
}
return &out, nil
}