-
Notifications
You must be signed in to change notification settings - Fork 240
/
types.go
155 lines (124 loc) · 2.81 KB
/
types.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
package flypg
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
type DatabaseListResponse struct {
Result []PostgresDatabase
}
type PostgresDatabase struct {
Name string
Users []string
}
type UserListResponse struct {
Result []PostgresUser
}
type PostgresUser struct {
Username string
Superuser bool
Databases []string
}
type RevokeAccessRequest struct {
Database string `json:"database"`
Username string `json:"username"`
}
type GrantAccessRequest struct {
Database string `json:"database"`
Username string `json:"username"`
}
type CreateUserRequest struct {
Username string `json:"username"`
Password string `json:"password"`
Superuser bool `json:"superuser"`
}
type DeleteUserRequest struct {
Username string `json:"username"`
}
type CreateDatabaseRequest struct {
Name string `json:"name"`
}
type DeleteDatabaseRequest struct {
Name string `json:"name"`
}
type CommandResponse struct {
Result bool `json:"result"`
Error string `json:"error"`
}
type FindDatabaseResponse struct {
Result PostgresDatabase
}
type FindUserResponse struct {
Result PostgresUser
}
type ReplicationStat struct {
Name string `json:"name"`
Diff int `json:"diff"`
}
type ReplicationStatsResponse struct {
Result []ReplicationStat
}
type StolonDBUidResponse struct {
Result string
}
type ReadonlyResponse struct {
Result bool
}
type RestartResponse struct {
Result string
}
type NodeRoleResponse struct {
Result string
}
type PGSettings struct {
Settings []PGSetting `json:"settings,omitempty"`
}
type PGSetting struct {
Name string `json:"name,omitempty"`
Setting string `json:"setting,omitempty"`
VarType string `json:"vartype,omitempty"`
MinVal string `json:"min_val,omitempty"`
MaxVal string `json:"max_val,omitempty"`
EnumVals []string `json:"enumvals,omitempty"`
Context string `json:"context,omitempty"`
Unit string `json:"unit,omitempty"`
Desc string `json:"short_desc,omitempty"`
PendingChange string `json:"pending_change,omitempty"`
PendingRestart bool `json:"pending_restart,omitempty"`
}
type SettingsViewResponse struct {
Result PGSettings
}
type Error struct {
StatusCode int
Err string `json:"error"`
}
func (e *Error) Error() string {
return fmt.Sprintf("%d: %s", e.StatusCode, e.Err)
}
func ErrorStatus(err error) int {
var e *Error
if errors.As(err, &e) {
return e.StatusCode
}
return http.StatusInternalServerError
}
func newError(status int, res *http.Response) error {
e := new(Error)
e.StatusCode = status
switch res.Header.Get("Content-Type") {
case "application/json":
if err := json.NewDecoder(res.Body).Decode(e); err != nil {
return err
}
default:
b, err := io.ReadAll(res.Body)
if err != nil {
return err
}
e.Err = string(b)
}
return e
}