-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathlog_project.go
598 lines (530 loc) · 14.1 KB
/
log_project.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// LogProject defines log project
type LogProject struct {
Name string // Project name
Endpoint string // IP or hostname of SLS endpoint
AccessKeyID string
AccessKeySecret string
SecurityToken string
}
// NewLogProject creates a new SLS project.
func NewLogProject(name, endpoint, accessKeyID, accessKeySecret string) (p *LogProject, err error) {
p = &LogProject{
Name: name,
Endpoint: endpoint,
AccessKeyID: accessKeyID,
AccessKeySecret: accessKeySecret,
}
return p, nil
}
// WithToken add token parameter
func (p *LogProject) WithToken(token string) (*LogProject, error) {
p.SecurityToken = token
return p, nil
}
// ListLogStore returns all logstore names of project p.
func (p *LogProject) ListLogStore() ([]string, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores")
r, err := request(p, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, err
}
type Body struct {
Count int
LogStores []string
}
body := &Body{}
json.Unmarshal(buf, body)
storeNames := body.LogStores
return storeNames, nil
}
// GetLogStore returns logstore according by logstore name.
func (p *LogProject) GetLogStore(name string) (*LogStore, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := request(p, "GET", "/logstores/"+name, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, err
}
s := &LogStore{}
json.Unmarshal(buf, s)
s.Name = name
s.project = p
return s, nil
}
// CreateLogStore creates a new logstore in SLS,
// where name is logstore name,
// and ttl is time-to-live(in day) of logs,
// and shardCnt is the number of shards.
func (p *LogProject) CreateLogStore(name string, ttl, shardCnt int) error {
type Body struct {
Name string `json:"logstoreName"`
TTL int `json:"ttl"`
ShardCount int `json:"shardCount"`
}
store := &Body{
Name: name,
TTL: ttl,
ShardCount: shardCnt,
}
body, err := json.Marshal(store)
if err != nil {
return NewClientError(err.Error())
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
r, err := request(p, "POST", "/logstores", h, body)
if err != nil {
return NewClientError(err.Error())
}
body, _ = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// DeleteLogStore deletes a logstore according by logstore name.
func (p *LogProject) DeleteLogStore(name string) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := request(p, "DELETE", "/logstores/"+name, h, nil)
if err != nil {
return NewClientError(err.Error())
}
body, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// UpdateLogStore updates a logstore according by logstore name,
// obviously we can't modify the logstore name itself.
func (p *LogProject) UpdateLogStore(name string, ttl, shardCnt int) (err error) {
type Body struct {
Name string `json:"logstoreName"`
TTL int `json:"ttl"`
ShardCount int `json:"shardCount"`
}
store := &Body{
Name: name,
TTL: ttl,
ShardCount: shardCnt,
}
body, err := json.Marshal(store)
if err != nil {
return NewClientError(err.Error())
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
r, err := request(p, "PUT", "/logstores/"+name, h, body)
if err != nil {
return NewClientError(err.Error())
}
body, _ = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// ListMachineGroup returns machine group name list and the total number of machine groups.
// The offset starts from 0 and the size is the max number of machine groups could be returned.
func (p *LogProject) ListMachineGroup(offset, size int) (m []string, total int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
if size <= 0 {
size = 500
}
uri := fmt.Sprintf("/machinegroups?offset=%v&size=%v", offset, size)
r, err := request(p, "GET", uri, h, nil)
if err != nil {
return nil, 0, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, 0, err
}
type Body struct {
MachineGroups []string
Count int
Total int
}
body := &Body{}
json.Unmarshal(buf, body)
m = body.MachineGroups
total = body.Total
return m, total, nil
}
// CheckLogstoreExist check logstore exist or not
func (p *LogProject) CheckLogstoreExist(name string) (bool, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
_, err := request(p, "GET", "/logstores/"+name, h, nil)
if err != nil {
if _, ok := err.(*Error); ok {
slsErr := err.(*Error)
if slsErr.Code == "LogStoreNotExist" {
return false, nil
}
return false, slsErr
}
return false, err
}
return true, nil
}
// CheckMachineGroupExist check machine group exist or not
func (p *LogProject) CheckMachineGroupExist(name string) (bool, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
_, err := request(p, "GET", "/machinegroups/"+name, h, nil)
if err != nil {
if _, ok := err.(*Error); ok {
slsErr := err.(*Error)
if slsErr.Code == "MachineGroupNotExist" {
return false, nil
}
return false, slsErr
}
return false, err
}
return true, nil
}
// GetMachineGroup retruns machine group according by machine group name.
func (p *LogProject) GetMachineGroup(name string) (m *MachineGroup, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
resp, err := request(p, "GET", "/machinegroups/"+name, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, err
}
m = new(MachineGroup)
json.Unmarshal(buf, m)
m.project = p
return m, nil
}
// CreateMachineGroup creates a new machine group in SLS.
func (p *LogProject) CreateMachineGroup(m *MachineGroup) error {
body, err := json.Marshal(m)
if err != nil {
return NewClientError(err.Error())
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
resp, err := request(p, "POST", "/machinegroups", h, body)
if err != nil {
return NewClientError(err.Error())
}
body, _ = ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// UpdateMachineGroup updates a machine group.
func (p *LogProject) UpdateMachineGroup(m *MachineGroup) (err error) {
body, err := json.Marshal(m)
if err != nil {
return NewClientError(err.Error())
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
r, err := request(p, "PUT", "/machinegroups/"+m.Name, h, body)
if err != nil {
return NewClientError(err.Error())
}
body, _ = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// DeleteMachineGroup deletes machine group according machine group name.
func (p *LogProject) DeleteMachineGroup(name string) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := request(p, "DELETE", "/machinegroups/"+name, h, nil)
if err != nil {
return NewClientError(err.Error())
}
body, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// ListConfig returns config names list and the total number of configs.
// The offset starts from 0 and the size is the max number of configs could be returned.
func (p *LogProject) ListConfig(offset, size int) (cfgNames []string, total int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
if size <= 0 {
size = 100
}
uri := fmt.Sprintf("/configs?offset=%v&size=%v", offset, size)
r, err := request(p, "GET", uri, h, nil)
if err != nil {
return nil, 0, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, 0, err
}
type Body struct {
Total int
Configs []string
}
body := &Body{}
json.Unmarshal(buf, body)
cfgNames = body.Configs
total = body.Total
return cfgNames, total, nil
}
// CheckConfigExist check config exist or not
func (p *LogProject) CheckConfigExist(name string) (bool, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
_, err := request(p, "GET", "/configs/"+name, h, nil)
if err != nil {
if _, ok := err.(*Error); ok {
slsErr := err.(*Error)
if slsErr.Code == "ConfigNotExist" {
return false, nil
}
return false, slsErr
}
return false, err
}
return true, nil
}
// GetConfig returns config according by config name.
func (p *LogProject) GetConfig(name string) (c *LogConfig, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := request(p, "GET", "/configs/"+name, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, err
}
c = &LogConfig{}
json.Unmarshal(buf, c)
c.project = p
return c, nil
}
// UpdateConfig updates a config.
func (p *LogProject) UpdateConfig(c *LogConfig) (err error) {
body, err := json.Marshal(c)
if err != nil {
return NewClientError(err.Error())
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
r, err := request(p, "PUT", "/configs/"+c.Name, h, body)
if err != nil {
return NewClientError(err.Error())
}
body, _ = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// CreateConfig creates a new config in SLS.
func (p *LogProject) CreateConfig(c *LogConfig) (err error) {
body, err := json.Marshal(c)
if err != nil {
return NewClientError(err.Error())
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
r, err := request(p, "POST", "/configs", h, body)
if err != nil {
return NewClientError(err.Error())
}
body, err = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// DeleteConfig deletes a config according by config name.
func (p *LogProject) DeleteConfig(name string) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := request(p, "DELETE", "/configs/"+name, h, nil)
if err != nil {
return NewClientError(err.Error())
}
body, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// GetAppliedMachineGroups returns applied machine group names list according config name.
func (p *LogProject) GetAppliedMachineGroups(confName string) (groupNames []string, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/configs/%v/machinegroups", confName)
r, err := request(p, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, err
}
type Body struct {
Count int
Machinegroups []string
}
body := &Body{}
json.Unmarshal(buf, body)
groupNames = body.Machinegroups
return groupNames, nil
}
// GetAppliedConfigs returns applied config names list according machine group name groupName.
func (p *LogProject) GetAppliedConfigs(groupName string) (confNames []string, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/machinegroups/%v/configs", groupName)
r, err := request(p, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return nil, err
}
type Cfg struct {
Count int `json:"count"`
Configs []string `json:"configs"`
}
body := &Cfg{}
json.Unmarshal(buf, body)
confNames = body.Configs
return confNames, nil
}
// ApplyConfigToMachineGroup applies config to machine group.
func (p *LogProject) ApplyConfigToMachineGroup(confName, groupName string) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/machinegroups/%v/configs/%v", groupName, confName)
r, err := request(p, "PUT", uri, h, nil)
if err != nil {
return NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return err
}
return nil
}
// RemoveConfigFromMachineGroup removes config from machine group.
func (p *LogProject) RemoveConfigFromMachineGroup(confName, groupName string) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/machinegroups/%v/configs/%v", groupName, confName)
r, err := request(p, "DELETE", uri, h, nil)
if err != nil {
return NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(buf, err)
return err
}
return nil
}