-
Notifications
You must be signed in to change notification settings - Fork 578
/
pgbouncerservice.go
294 lines (255 loc) · 8.1 KB
/
pgbouncerservice.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package pgbouncerservice
/*
Copyright 2018 - 2020 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"encoding/json"
"github.com/crunchydata/postgres-operator/internal/apiserver"
msgs "github.com/crunchydata/postgres-operator/pkg/apiservermsgs"
log "github.com/sirupsen/logrus"
"net/http"
)
// CreatePgbouncerHandler ...
// pgo create pgbouncer
func CreatePgbouncerHandler(w http.ResponseWriter, r *http.Request) {
// swagger:operation POST /pgbouncer pgbouncerservice pgbouncer-post
/*```
Create a pgbouncer
*/
// ---
// produces:
// - application/json
// parameters:
// - name: "Create Pgbouncer Request"
// in: "body"
// schema:
// "$ref": "#/definitions/CreatePgbouncerRequest"
// responses:
// '200':
// description: Output
// schema:
// "$ref": "#/definitions/CreatePgbouncerResponse"
var ns string
log.Debug("pgbouncerservice.CreatePgbouncerHandler called")
username, err := apiserver.Authn(apiserver.CREATE_PGBOUNCER_PERM, w, r)
if err != nil {
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
var request msgs.CreatePgbouncerRequest
_ = json.NewDecoder(r.Body).Decode(&request)
resp := msgs.CreatePgbouncerResponse{}
resp.Status = msgs.Status{Code: msgs.Ok, Msg: ""}
if request.ClientVersion != msgs.PGO_VERSION {
resp.Status.Code = msgs.Error
resp.Status.Msg = apiserver.VERSION_MISMATCH_ERROR
json.NewEncoder(w).Encode(resp)
return
}
ns, err = apiserver.GetNamespace(apiserver.Clientset, username, request.Namespace)
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
json.NewEncoder(w).Encode(resp)
return
}
resp = CreatePgbouncer(&request, ns, username)
json.NewEncoder(w).Encode(resp)
}
/* The delete pgboucner handler is setup to be used by two different routes. To keep
the documentation consistent with the API this endpoint is documented along with the
/pgbouncer (DELETE) enpoint. This endpoint should be deprecated in future API versions.
*/
// swagger:operation DELETE /pgbouncer pgbouncerservice pgbouncer-delete
/*```
Delete a pgbouncer from a cluster
*/
// ---
// produces:
// - application/json
// parameters:
// - name: "Delete PgBouncer Request"
// in: "body"
// schema:
// "$ref": "#/definitions/DeletePgbouncerRequest"
// responses:
// '200':
// description: Output
// schema:
// "$ref": "#/definitions/DeletePgbouncerResponse"
// DeletePgbouncerHandler ...
// pgo delete pgbouncer
func DeletePgbouncerHandler(w http.ResponseWriter, r *http.Request) {
// swagger:operation DELETE /pgbouncer pgbouncerservice pgbouncer-delete
/*```
Delete a pgbouncer from a cluster
*/
// ---
// produces:
// - application/json
// parameters:
// - name: "Delete PgBouncer Request"
// in: "body"
// schema:
// "$ref": "#/definitions/DeletePgbouncerRequest"
// responses:
// '200':
// description: Output
// schema:
// "$ref": "#/definitions/DeletePgbouncerResponse"
var ns string
log.Debug("pgbouncerservice.DeletePgbouncerHandler called")
username, err := apiserver.Authn(apiserver.DELETE_PGBOUNCER_PERM, w, r)
if err != nil {
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
var request msgs.DeletePgbouncerRequest
_ = json.NewDecoder(r.Body).Decode(&request)
resp := msgs.DeletePgbouncerResponse{}
resp.Status = msgs.Status{Code: msgs.Ok, Msg: ""}
if request.ClientVersion != msgs.PGO_VERSION {
resp.Status.Code = msgs.Error
resp.Status.Msg = apiserver.VERSION_MISMATCH_ERROR
json.NewEncoder(w).Encode(resp)
return
}
ns, err = apiserver.GetNamespace(apiserver.Clientset, username, request.Namespace)
if err != nil {
resp.Status.Code = msgs.Error
resp.Status.Msg = err.Error()
json.NewEncoder(w).Encode(resp)
return
}
resp = DeletePgbouncer(&request, ns)
json.NewEncoder(w).Encode(resp)
}
// ShowPgBouncerHandler is the HTTP handler to get information about a pgBouncer
// deployment, aka `pgo show pgbouncer`
func ShowPgBouncerHandler(w http.ResponseWriter, r *http.Request) {
// swagger:operation POST /pgbouncer/show pgbouncerservice pgbouncer-post
/*```
Show information about a pgBouncer deployment
*/
// ---
// produces:
// - application/json
// parameters:
// - name: "Show PGBouncer Information"
// in: "body"
// schema:
// "$ref": "#/definitions/ShowPgBouncerRequest"
// responses:
// '200':
// description: Output
// schema:
// "$ref": "#/definitions/ShowPgBouncerResponse"
log.Debug("pgbouncerservice.ShowPgbouncerHandler called")
// first, determine if the user is authorized to access this resource
username, err := apiserver.Authn(apiserver.SHOW_PGBOUNCER_PERM, w, r)
if err != nil {
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
// get the information that is in the request
var request msgs.ShowPgBouncerRequest
_ = json.NewDecoder(r.Body).Decode(&request)
// ensure the versions align...
if request.ClientVersion != msgs.PGO_VERSION {
response := msgs.ShowPgBouncerResponse{
Status: msgs.Status{
Code: msgs.Error,
Msg: apiserver.VERSION_MISMATCH_ERROR,
},
}
json.NewEncoder(w).Encode(response)
return
}
// ensure the namespace being used exists
namespace, err := apiserver.GetNamespace(apiserver.Clientset, username, request.Namespace)
if err != nil {
response := msgs.ShowPgBouncerResponse{
Status: msgs.Status{
Code: msgs.Error,
Msg: err.Error(),
},
}
json.NewEncoder(w).Encode(response)
return
}
// get the information about a pgbouncer deployment(s)
response := ShowPgBouncer(&request, namespace)
json.NewEncoder(w).Encode(response)
}
// UpdatePgBouncerHandler is the HTTP handler to perform update tasks on a
// pgbouncer instance, such as rotating the password
func UpdatePgBouncerHandler(w http.ResponseWriter, r *http.Request) {
// swagger:operation PUT /pgbouncer pgbouncerservice pgbouncer-put
/*```
Update a pgBouncer cluster, e.g. rotate the password
*/
// ---
// produces:
// - application/json
// parameters:
// - name: "Update PGBouncer"
// in: "body"
// schema:
// "$ref": "#/definitions/UpdatePgBouncerRequest"
// responses:
// '200':
// description: Output
// schema:
// "$ref": "#/definitions/UpdatePgBouncerResponse"
log.Debug("pgbouncerservice.UpdatePgbouncerHandler called")
// first, determine if the user is authorized to access this resource
username, err := apiserver.Authn(apiserver.UPDATE_PGBOUNCER_PERM, w, r)
if err != nil {
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
// get the information that is in the request
var request msgs.UpdatePgBouncerRequest
_ = json.NewDecoder(r.Body).Decode(&request)
// ensure the versions align...
if request.ClientVersion != msgs.PGO_VERSION {
response := msgs.UpdatePgBouncerResponse{
Status: msgs.Status{
Code: msgs.Error,
Msg: apiserver.VERSION_MISMATCH_ERROR,
},
}
json.NewEncoder(w).Encode(response)
return
}
// ensure the namespace being used exists
namespace, err := apiserver.GetNamespace(apiserver.Clientset, username, request.Namespace)
if err != nil {
response := msgs.UpdatePgBouncerResponse{
Status: msgs.Status{
Code: msgs.Error,
Msg: err.Error(),
},
}
json.NewEncoder(w).Encode(response)
return
}
// get the information about a pgbouncer deployment(s)
response := UpdatePgBouncer(&request, namespace, username)
json.NewEncoder(w).Encode(response)
}