-
Notifications
You must be signed in to change notification settings - Fork 223
/
postgres_commands.go
258 lines (208 loc) · 5.58 KB
/
postgres_commands.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
package cmd
import (
"encoding/json"
"fmt"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/pkg/agent"
)
type PostgresDatabaseListResponse struct {
Result []PostgresDatabase
}
type PostgresDatabase struct {
Name string
Users []string
}
type PostgresUserListResponse struct {
Result []PostgresUser
}
type PostgresUser struct {
Username string
Superuser bool
Databases []string
}
type PostgresRevokeAccessRequest struct {
Database string `json:"database"`
Username string `json:"username"`
}
type PostgresGrantAccessRequest struct {
Database string `json:"database"`
Username string `json:"username"`
}
type PostgresCreateUserRequest struct {
Username string `json:"username"`
Password string `json:"password"`
Superuser bool `json:"superuser"`
}
type PostgresDeleteUserRequest struct {
Username string `json:"username"`
}
type PostgresCreateDatabaseRequest struct {
Name string `json:"name"`
}
type PostgresCommandResponse struct {
Result bool `json:"result"`
Error string `json:"error"`
}
type PostgresCmd struct {
cmdCtx *cmdctx.CmdContext
app *api.App
dialer agent.Dialer
}
func NewPostgresCmd(cmdCtx *cmdctx.CmdContext, app *api.App, dialer agent.Dialer) *PostgresCmd {
return &PostgresCmd{
cmdCtx: cmdCtx,
app: app,
dialer: dialer,
}
}
func (pc *PostgresCmd) RevokeAccess(dbName, username string) (*PostgresCommandResponse, error) {
fmt.Println("Running flyadmin revoke-access")
req := &PostgresRevokeAccessRequest{
Database: dbName,
Username: username,
}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
cmd := fmt.Sprintf("flyadmin revoke-access %s", string(reqJson))
createUsrBytes, err := runSSHCommand(pc.cmdCtx, pc.app, pc.dialer, cmd)
if err != nil {
return nil, err
}
var resp PostgresCommandResponse
if err := json.Unmarshal(createUsrBytes, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (pc *PostgresCmd) GrantAccess(dbName, username string) (*PostgresCommandResponse, error) {
fmt.Println("Running flyadmin grant-access")
req := &PostgresGrantAccessRequest{
Database: dbName,
Username: username,
}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
cmd := fmt.Sprintf("flyadmin grant-access %s", string(reqJson))
createUsrBytes, err := runSSHCommand(pc.cmdCtx, pc.app, pc.dialer, cmd)
if err != nil {
return nil, err
}
var resp PostgresCommandResponse
if err := json.Unmarshal(createUsrBytes, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (pc *PostgresCmd) CreateUser(userName, pwd string) (*PostgresCommandResponse, error) {
fmt.Println("Running flyadmin user-create")
req := &PostgresCreateUserRequest{
Username: userName,
Password: pwd,
Superuser: false,
}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
cmd := fmt.Sprintf("flyadmin user-create %s", string(reqJson))
createUsrBytes, err := runSSHCommand(pc.cmdCtx, pc.app, pc.dialer, cmd)
if err != nil {
return nil, err
}
var resp PostgresCommandResponse
if err := json.Unmarshal(createUsrBytes, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (pc *PostgresCmd) DeleteUser(userName string) (*PostgresCommandResponse, error) {
fmt.Println("Running flyadmin user-delete")
req := &PostgresDeleteUserRequest{
Username: userName,
}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
cmd := fmt.Sprintf("flyadmin user-delete %s", string(reqJson))
createUsrBytes, err := runSSHCommand(pc.cmdCtx, pc.app, pc.dialer, cmd)
if err != nil {
return nil, err
}
var resp PostgresCommandResponse
if err := json.Unmarshal(createUsrBytes, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (pc *PostgresCmd) CreateDatabase(dbName string) (*PostgresCommandResponse, error) {
fmt.Println("Running flyadmin database-create")
req := &PostgresCreateDatabaseRequest{Name: dbName}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
cmd := fmt.Sprintf("flyadmin database-create %s", string(reqJson))
createDbBytes, err := runSSHCommand(pc.cmdCtx, pc.app, pc.dialer, cmd)
if err != nil {
return nil, err
}
var resp PostgresCommandResponse
if err := json.Unmarshal(createDbBytes, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (pc *PostgresCmd) ListDatabases() (*PostgresDatabaseListResponse, error) {
fmt.Println("Running flyadmin database-list")
databaseListBytes, err := runSSHCommand(pc.cmdCtx, pc.app, pc.dialer, "flyadmin database-list")
if err != nil {
return nil, err
}
var dbList PostgresDatabaseListResponse
if err := json.Unmarshal(databaseListBytes, &dbList); err != nil {
return nil, err
}
return &dbList, nil
}
func (pc *PostgresCmd) DbExists(dbName string) (bool, error) {
dbList, err := pc.ListDatabases()
if err != nil {
return false, err
}
for _, db := range dbList.Result {
if db.Name == dbName {
return true, nil
}
}
return false, nil
}
func (pc *PostgresCmd) ListUsers() (*PostgresUserListResponse, error) {
fmt.Println("Running flyadmin user-list")
userListBytes, err := runSSHCommand(pc.cmdCtx, pc.app, pc.dialer, "flyadmin user-list")
if err != nil {
return nil, err
}
var userList PostgresUserListResponse
if err := json.Unmarshal(userListBytes, &userList); err != nil {
return nil, err
}
return &userList, nil
}
func (pc *PostgresCmd) UserExists(userName string) (bool, error) {
userList, err := pc.ListUsers()
if err != nil {
return false, err
}
for _, user := range userList.Result {
if user.Username == userName {
return true, nil
}
}
return false, nil
}