forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 114
/
etl_job_project.go
109 lines (103 loc) · 2.38 KB
/
etl_job_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
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// CreateETLJob creates a new ETL job in SLS.
func (p *LogProject) CreateETLJob(j *ETLJob) error {
body, err := json.Marshal(j)
if err != nil {
return NewClientError(err)
}
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", "/etljobs", h, body)
if err != nil {
return err
}
defer r.Body.Close()
return nil
}
// GetETLJob returns ETL job according to job name.
func (p *LogProject) GetETLJob(name string) (*ETLJob, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := request(p, "GET", "/etljobs/"+name, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
job := &ETLJob{}
err = json.Unmarshal(buf, job)
if err != nil {
return nil, err
}
return job, nil
}
// UpdateETLJob updates an ETL job according to job name,
// Not all fields of ETLJob can be updated
func (p *LogProject) UpdateETLJob(name string, job *ETLJob) error {
body, err := json.Marshal(job)
if err != nil {
return NewClientError(err)
}
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", "/etljobs/"+name, h, body)
if err != nil {
return err
}
defer r.Body.Close()
return nil
}
// DeleteETLJob deletes a job according to job name.
func (p *LogProject) DeleteETLJob(name string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := request(p, "DELETE", "/etljobs/"+name, h, nil)
if err != nil {
return err
}
defer r.Body.Close()
return nil
}
// ListETLJobs returns all job names of project.
func (p *LogProject) ListETLJobs() ([]string, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/etljobs")
r, err := request(p, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
type Body struct {
Count int
ETLJobs []string `json:"etlJobNameList"`
Total int
}
body := &Body{}
err = json.Unmarshal(buf, body)
if err != nil {
return nil, err
}
return body.ETLJobs, nil
}