-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
234 lines (181 loc) · 6.33 KB
/
database.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
package directadmin
import (
"bytes"
"fmt"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
)
const (
DatabaseFormatGz = DatabaseFormat("gz")
DatabaseFormatSql = DatabaseFormat("sql")
)
type (
DatabaseFormat string
Database struct {
Name string `json:"database"`
DefaultCharset string `json:"defaultCharset"`
DefaultCollation string `json:"defaultCollation"`
DefinerIssues int `json:"definerIssues"`
EventCount int `json:"eventCount"`
RoutineCount int `json:"routineCount"`
SizeBytes int `json:"sizeBytes"`
TableCount int `json:"tableCount"`
TriggerCount int `json:"triggerCount"`
UserCount int `json:"userCount"`
ViewCount int `json:"viewCount"`
}
DatabaseProcess struct {
Command string `json:"command"`
Database string `json:"database"`
Host string `json:"host"`
Id int `json:"id"`
Info string `json:"info"`
State string `json:"state"`
Time int `json:"time"`
User string `json:"user"`
}
DatabaseUser struct {
HostPatterns []string `json:"hostPatterns"`
Password string `json:"password"`
User string `json:"dbuser"`
}
DatabaseWithUser struct {
Database
Password string `json:"password"`
User string `json:"dbuser"`
}
)
func (c *UserContext) CreateDatabase(database *Database) error {
database.Name = c.addUsernamePrefix(database.Name)
if _, err := c.makeRequestNew(http.MethodPost, "db-manage/create-db", database, nil); err != nil {
return err
}
return nil
}
func (c *UserContext) CreateDatabaseWithUser(database *DatabaseWithUser) error {
database.Name = c.addUsernamePrefix(database.Name)
database.User = c.addUsernamePrefix(database.User)
if _, err := c.makeRequestNew(http.MethodPost, "db-manage/create-db-with-user", database, nil); err != nil {
return err
}
return nil
}
func (c *UserContext) CreateDatabaseUser(databaseUser *DatabaseUser) error {
databaseUser.User = c.addUsernamePrefix(databaseUser.User)
if _, err := c.makeRequestNew(http.MethodPost, "db-manage/create-user", databaseUser, nil); err != nil {
return err
}
return nil
}
func (c *UserContext) DeleteDatabase(databaseName string) error {
databaseName = c.addUsernamePrefix(databaseName)
if _, err := c.makeRequestNew(http.MethodDelete, "db-manage/databases/"+databaseName, nil, nil); err != nil {
return err
}
return nil
}
func (c *UserContext) DownloadDatabase(name string, format DatabaseFormat, filePath string) error {
name = name + "." + string(format)
if !strings.Contains(name, c.GetMyUsername()+"_") {
name = c.GetMyUsername() + "_" + name
}
switch format {
case DatabaseFormatGz, DatabaseFormatSql:
break
default:
return fmt.Errorf("invalid database format: %v", format)
}
var file *os.File
if _, err := os.Stat(filePath); os.IsNotExist(err) {
file, err = os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create file: %v", err)
}
}
resp, err := c.makeRequestOld(http.MethodPost, "DB/"+name, nil, nil)
if err != nil {
return fmt.Errorf("failed to download database: %v", err)
}
if _, err = file.Write(resp); err != nil {
return fmt.Errorf("error writing to file: %w", err)
}
return nil
}
// ExportDatabase (user) returns an export of the given database
func (c *UserContext) ExportDatabase(databaseName string, gzip bool) ([]byte, error) {
databaseName = c.addUsernamePrefix(databaseName)
export, err := c.makeRequestNew(http.MethodGet, "db-manage/databases/"+databaseName+"/export?gzip="+strconv.FormatBool(gzip), nil, nil)
if err != nil {
return nil, err
}
return export, nil
}
// GetDatabase (user) returns the given database
func (c *UserContext) GetDatabase(databaseName string) (*Database, error) {
databaseName = c.addUsernamePrefix(databaseName)
var database Database
if _, err := c.makeRequestNew(http.MethodGet, "db-show/databases/"+databaseName, nil, &database); err != nil {
return nil, err
}
return &database, nil
}
// GetDatabases (user) returns an array of the session user's databases
func (c *UserContext) GetDatabases() ([]*Database, error) {
var databases []*Database
if _, err := c.makeRequestNew(http.MethodGet, "db-show/databases", nil, &databases); err != nil {
return nil, err
}
return databases, nil
}
// GetDatabaseProcesses (admin) returns an array of current database processes
func (c *UserContext) GetDatabaseProcesses() ([]*DatabaseProcess, error) {
var databaseProcesses []*DatabaseProcess
if _, err := c.makeRequestNew(http.MethodGet, "db-monitor/processes", nil, &databaseProcesses); err != nil {
return nil, err
}
return databaseProcesses, nil
}
// ImportDatabase (user) imports the given database export into the given database
func (c *UserContext) ImportDatabase(databaseName string, emptyExistingDatabase bool, sql []byte) error {
databaseName = c.addUsernamePrefix(databaseName)
var byteBuffer bytes.Buffer
multipartWriter := multipart.NewWriter(&byteBuffer)
formFile, err := multipartWriter.CreateFormFile("sqlfile", "filename")
if err != nil {
return fmt.Errorf("failed to create form file: %w", err)
}
if _, err = formFile.Write(sql); err != nil {
return fmt.Errorf("failed to write to form file: %w", err)
}
if err = multipartWriter.Close(); err != nil {
return fmt.Errorf("failed to close multipart writer: %w", err)
}
if _, err = c.uploadFile(http.MethodPost, "/api/db-manage/databases/"+databaseName+"/import?clean="+strconv.FormatBool(emptyExistingDatabase), byteBuffer.Bytes(), nil, multipartWriter.FormDataContentType()); err != nil {
return err
}
return nil
}
// UpdateDatabaseUserHosts (user) updates the given database user's hosts
func (c *UserContext) UpdateDatabaseUserHosts(username string, hosts []string) error {
username = c.addUsernamePrefix(username)
if _, err := c.makeRequestNew(http.MethodPost, "db-manage/users/"+username+"/change-hosts", hosts, nil); err != nil {
return err
}
return nil
}
// UpdateDatabaseUserPassword (user) updates the given database user's password
func (c *UserContext) UpdateDatabaseUserPassword(username string, password string) error {
username = c.addUsernamePrefix(username)
newPassword := struct {
NewPassword string `json:"newPassword"`
}{
password,
}
if _, err := c.makeRequestNew(http.MethodPost, "db-manage/users/"+username+"/change-password", newPassword, nil); err != nil {
return err
}
return nil
}