-
Notifications
You must be signed in to change notification settings - Fork 488
/
handler.go
558 lines (476 loc) · 16.7 KB
/
handler.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package distcc
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"build-booster/common/blog"
"build-booster/common/codec"
commonMySQL "build-booster/common/mysql"
commonTypes "build-booster/common/types"
"build-booster/gateway/pkg/api"
"build-booster/server/pkg/engine"
"build-booster/server/pkg/engine/distcc"
"github.com/emicklei/go-restful"
)
// ListTask handle the http request for listing task with conditions.
func ListTask(req *restful.Request, resp *restful.Response) {
opts, err := getListOptions(req, "TASK")
if err != nil {
blog.Errorf("list task get options failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
taskList, length, err := defaultMySQL.ListTask(opts)
if err != nil {
blog.Errorf("list task failed opts(%v): %v", opts, err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrListTaskFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp, Data: taskList, Extra: map[string]interface{}{"length": length}})
}
// ListProject handle the http request for listing project with conditions.
func ListProject(req *restful.Request, resp *restful.Response) {
opts, err := getListOptions(req, "PROJECT")
if err != nil {
blog.Errorf("list project get options failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
projectList, length, err := defaultMySQL.ListProject(opts)
if err != nil {
blog.Errorf("list project failed opts(%v): %v", opts, err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrListProjectFailed, Message: err.Error()})
return
}
var result []map[string]interface{}
for _, project := range projectList {
item := make(map[string]interface{}, 1000)
for k, v := range wrapMap(project.TableProjectSetting) {
item[k] = v
}
for k, v := range wrapMap(project.TableProjectInfo) {
item[k] = v
}
result = append(result, item)
}
api.ReturnRest(&api.RestResponse{Resp: resp, Data: result, Extra: map[string]interface{}{"length": length}})
}
// UpdateProject handle the http request for updating project with some fields.
func UpdateProject(req *restful.Request, resp *restful.Response) {
var projectType UpdateProjectType
rawBody, err := ioutil.ReadAll(req.Request.Body)
if err != nil {
blog.Errorf("update project read data from request body failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
if err = projectType.Load(rawBody); err != nil {
blog.Errorf("update project load data failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
if projectType.Operator == "" {
blog.Errorf("update project failed: operator not specific")
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrOperatorNoSpecific, Message: "operator no specific"})
return
}
projectID := req.PathParameter(api.QueryProjectIDKey)
projectType.Data.ProjectID = projectID
projectType.RawData["project_id"] = projectID
if err := projectType.CheckData(); err != nil {
blog.Errorf("update project failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
var record []byte
_ = codec.EncJSON(projectType.RawData, &record)
blog.Infof("receive a project update: ID(%s) Operator(%s) Data: %s", projectID, projectType.Operator, string(record))
if err := defaultMySQL.CreateOrUpdateProjectSetting(&projectType.Data, projectType.RawData); err != nil {
blog.Errorf("update project failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrUpdateProjectFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp})
}
// DeleteProject handle the http request for deleting project.
func DeleteProject(req *restful.Request, resp *restful.Response) {
var projectType DeleteProjectType
if err := codec.DecJSONReader(req.Request.Body, &projectType); err != nil {
blog.Errorf("delete project get data failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
if projectType.Operator == "" {
blog.Errorf("delete project failed: operator not specific")
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrOperatorNoSpecific, Message: "operator no specific"})
return
}
projectID := req.PathParameter(api.QueryProjectIDKey)
blog.Infof("receive a project delete: ID(%s) Operator(%s)", projectID, projectType.Operator)
if err := defaultMySQL.DeleteProjectSetting(projectID); err != nil {
blog.Errorf("delete project failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrDeleteProjectFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp})
}
// ListWhitelist handle the http request for listing whitelist with conditions.
func ListWhitelist(req *restful.Request, resp *restful.Response) {
opts, err := getListOptions(req, "WHITELIST")
if err != nil {
blog.Errorf("list whitelist get options failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
whitelistList, _, err := defaultMySQL.ListWhitelist(opts)
if err != nil {
blog.Errorf("list whitelist failed opts(%v): %v", opts, err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrListWhiteListFailed, Message: err.Error()})
return
}
for _, wl := range whitelistList {
if wl.ProjectID == engine.WhiteListAllProjectID {
wl.ProjectID = ""
}
}
api.ReturnRest(&api.RestResponse{Resp: resp, Data: whitelistList})
}
// UpdateWhitelist handle the http request for updating whitelist with full fields.
func UpdateWhitelist(req *restful.Request, resp *restful.Response) {
var whitelistType UpdateWhitelistType
if err := codec.DecJSONReader(req.Request.Body, &whitelistType); err != nil {
blog.Errorf("update whitelist get data failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
if whitelistType.Operator == "" {
blog.Errorf("update whitelist failed: operator not specific")
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrOperatorNoSpecific, Message: "operator no specific"})
return
}
whiteList := whitelistType.Data
for _, wl := range whiteList {
if err := wl.CheckData(); err != nil {
blog.Errorf("update whitelist check data failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
}
var record []byte
_ = codec.EncJSON(whiteList, &record)
blog.Infof("receive a whitelist update: Operator(%s) Data: %s", whitelistType.Operator, record)
if err := defaultMySQL.PutWhitelist(whiteList); err != nil {
blog.Errorf("update whitelist failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrUpdateWhiteListFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp})
}
// DeleteWhitelist handle the http request for deleting whitelist.
func DeleteWhitelist(req *restful.Request, resp *restful.Response) {
var whitelistType DeleteWhitelistType
if err := codec.DecJSONReader(req.Request.Body, &whitelistType); err != nil {
blog.Errorf("delete whitelist get data failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
if whitelistType.Operator == "" {
blog.Errorf("delete whitelist failed: operator not specific")
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrOperatorNoSpecific, Message: "operator no specific"})
return
}
keys := whitelistType.Data
for _, key := range keys {
if key.ProjectID == "" {
key.ProjectID = engine.WhiteListAllProjectID
}
}
var record []byte
_ = codec.EncJSON(keys, &record)
blog.Infof("receive a whitelist delete: Operator(%s) Data: %s", whitelistType.Operator, record)
if err := defaultMySQL.DeleteWhitelist(keys); err != nil {
blog.Errorf("delete whitelist failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrDeleteWhiteListFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp})
}
// ListGcc handle the http request for listing gcc with conditions.
func ListGcc(req *restful.Request, resp *restful.Response) {
opts, err := getListOptions(req, "GCC")
if err != nil {
blog.Errorf("list gcc get options failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
gccList, _, err := defaultMySQL.ListGcc(opts)
if err != nil {
blog.Errorf("list gcc failed opts(%v): %v", opts, err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrListGccFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp, Data: gccList})
}
// UpdateGcc handle the http request for updating gcc with full fields.
func UpdateGcc(req *restful.Request, resp *restful.Response) {
var gccType UpdateGccType
if err := codec.DecJSONReader(req.Request.Body, &gccType); err != nil {
blog.Errorf("update gcc get data failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
if gccType.Operator == "" {
blog.Errorf("update gcc failed: operator not specific")
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrOperatorNoSpecific, Message: "operator no specific"})
return
}
gcc := gccType.Data
gcc.GccVersion = req.PathParameter(queryGccVersionKey)
if err := gcc.CheckData(); err != nil {
blog.Errorf("update gcc failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
blog.Infof("receive a gcc update: Operator(%s) gccVersion(%s)", gccType.Operator, gcc.GccVersion)
if err := defaultMySQL.PutGcc(&gcc); err != nil {
blog.Errorf("update gcc failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrUpdateGccFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp})
}
// DeleteGcc handle the http request for deleting gcc.
func DeleteGcc(req *restful.Request, resp *restful.Response) {
var gccType DeleteGccType
if err := codec.DecJSONReader(req.Request.Body, &gccType); err != nil {
blog.Errorf("delete gcc get data failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrInvalidParam, Message: err.Error()})
return
}
if gccType.Operator == "" {
blog.Errorf("delete gcc failed: operator not specific")
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrOperatorNoSpecific, Message: "operator no specific"})
return
}
gccVersion := req.PathParameter(queryGccVersionKey)
blog.Infof("receive a gcc delete: gccVersion(%s) Operator(%s)", gccVersion, gccType.Operator)
if err := defaultMySQL.DeleteGcc(gccVersion); err != nil {
blog.Errorf("delete gcc failed: %v", err)
api.ReturnRest(&api.RestResponse{Resp: resp, ErrCode: commonTypes.ServerErrDeleteGccFailed, Message: err.Error()})
return
}
api.ReturnRest(&api.RestResponse{Resp: resp})
}
func getListOptions(req *restful.Request, resource string) (commonMySQL.ListOptions, error) {
opts := commonMySQL.NewListOptions()
value := req.Request.URL.Query()
// set offset of list options, default is 0
offset, _ := strconv.Atoi(value.Get(api.QueryOffsetKey))
opts.Offset(offset)
// set limit of list options, default is 1000
limit, _ := strconv.Atoi(value.Get(api.QueryLimitKey))
if limit == 0 {
limit = 1000
}
opts.Limit(limit)
// set selector of list options
selector := value.Get(api.QuerySelectorKey)
if selector != "" {
opts.Select(strings.Split(selector, api.MultiSeparator))
}
// set order of list options
order := value.Get(api.QueryOrderKey)
if order != "" {
opts.Order(strings.Split(order, api.MultiSeparator))
}
// set other query conditions
for k, vl := range value {
if len(vl) == 0 {
continue
}
rv := vl[0]
switch strings.ToUpper(resource) {
case "TASK":
if _, ok := listTaskInKey[k]; ok {
v, err := parseType(k, rv, true)
if err != nil {
return opts, err
}
opts.In(k, v)
continue
}
if _, ok := listTaskGtKey[k]; ok {
v, err := parseType(k, rv, false)
if err != nil {
return opts, err
}
if _, ok = api.OriginKey[k]; !ok {
continue
}
opts.Gt(api.OriginKey[k], v)
continue
}
if _, ok := listTaskLtKey[k]; ok {
v, err := parseType(k, rv, false)
if err != nil {
return opts, err
}
if _, ok = api.OriginKey[k]; !ok {
continue
}
opts.Lt(api.OriginKey[k], v)
continue
}
case "PROJECT":
if _, ok := listProjectInKey[k]; ok {
v, err := parseType(k, rv, true)
if err != nil {
return opts, err
}
opts.In(k, v)
continue
}
case "WHITELIST":
if _, ok := listWhitelistInKey[k]; ok {
v, err := parseType(k, rv, true)
if err != nil {
return opts, err
}
opts.In(k, v)
continue
}
case "GCC":
break
default:
break
}
}
return opts, nil
}
func parseType(k string, v string, isList bool) (interface{}, error) {
if _, ok := intKey[k]; ok {
if !isList {
return strconv.Atoi(v)
}
r := make([]int, 0, 100)
for _, vi := range strings.Split(v, api.MultiSeparator) {
item, err := strconv.Atoi(vi)
if err != nil {
return nil, err
}
r = append(r, item)
}
return r, nil
}
if _, ok := int64Key[k]; ok {
if !isList {
return strconv.ParseInt(v, 10, 64)
}
r := make([]int64, 0, 100)
for _, vi := range strings.Split(v, api.MultiSeparator) {
item, err := strconv.ParseInt(vi, 10, 64)
if err != nil {
return nil, err
}
r = append(r, item)
}
return r, nil
}
if _, ok := boolKey[k]; ok {
return v == "true", nil
}
if _, ok := float64Key[k]; ok {
if !isList {
return strconv.ParseFloat(v, 64)
}
r := make([]float64, 0, 100)
for _, vi := range strings.Split(v, api.MultiSeparator) {
item, err := strconv.ParseFloat(vi, 64)
if err != nil {
return nil, err
}
r = append(r, item)
}
return r, nil
}
if !isList {
return v, nil
}
return strings.Split(v, api.MultiSeparator), nil
}
// OperatorType describe the http request's operator.
type OperatorType struct {
Operator string `json:"operator"`
}
// UpdateProjectType describe the param of http request to update project.
type UpdateProjectType struct {
OperatorType
Data distcc.TableProjectSetting `json:"data"`
RawData map[string]interface{} `json:"-"`
}
// Load load fields data from byte.
func (upt *UpdateProjectType) Load(rawBody []byte) error {
if err := codec.DecJSON(rawBody, upt); err != nil {
return err
}
var rawMap map[string]interface{}
_ = codec.DecJSON(rawBody, &rawMap)
data := rawMap["data"]
if data == nil {
return fmt.Errorf("no data specified, nothing to do")
}
var tmp []byte
_ = codec.EncJSON(data, &tmp)
_ = codec.DecJSON(tmp, &upt.RawData)
return nil
}
// CheckData check if the data is valid.
func (upt *UpdateProjectType) CheckData() error {
// check project_id can not be empty
if upt.RawData["project_id"] == "" {
return fmt.Errorf("projectID empty")
}
// make sure that queue_name == city
upt.Data.EngineName = distcc.EngineName
if upt.Data.City != "" {
upt.Data.QueueName = upt.Data.City
}
upt.RawData["engine_name"] = distcc.EngineName
city, ok := upt.RawData["city"]
queueName, _ := upt.RawData["queue_name"]
if ok && queueName == nil {
upt.RawData["queue_name"] = city
}
return nil
}
// DeleteProjectType describe the param of http request to delete project.
type DeleteProjectType struct {
OperatorType
}
// UpdateWhitelistType describe the param of http request to update whitelist.
type UpdateWhitelistType struct {
OperatorType
Data []*distcc.TableWhitelist `json:"data"`
}
// DeleteWhitelistType describe the param of http request to delete whitelist.
type DeleteWhitelistType struct {
OperatorType
Data []*engine.WhiteListKey `json:"data"`
}
// UpdateGccType describe the param of http request to update gcc.
type UpdateGccType struct {
OperatorType
Data distcc.TableGcc `json:"data"`
}
// DeleteGccType describe the param of http request to delete gcc.
type DeleteGccType struct {
OperatorType
}
func wrapMap(source interface{}) map[string]interface{} {
var tmp []byte
_ = codec.EncJSON(source, &tmp)
data := make(map[string]interface{}, 1000)
_ = codec.DecJSON(tmp, &data)
return data
}