forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathclient_etl.go
227 lines (207 loc) · 5.8 KB
/
client_etl.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
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
)
type ETL struct {
Configuration ETLConfiguration `json:"configuration"`
Description string `json:"description,omitempty"`
DisplayName string `json:"displayName"`
Name string `json:"name"`
Schedule ETLSchedule `json:"schedule"`
Type string `json:"type"`
Status string `json:"status"`
CreateTime int32 `json:"createTime,omitempty"`
LastModifiedTime int32 `json:"lastModifiedTime,omitempty"`
}
type ETLConfiguration struct {
AccessKeyId string `json:"accessKeyId"`
AccessKeySecret string `json:"accessKeySecret"`
FromTime int64 `json:"fromTime,omitempty"`
Logstore string `json:"logstore"`
Parameters map[string]string `json:"parameters,omitempty"`
RoleArn string `json:"roleArn,omitempty"`
Script string `json:"script"`
ToTime int32 `json:"toTime,omitempty"`
Version int8 `json:"version"`
ETLSinks []ETLSink `json:"sinks"`
}
type ETLSchedule struct {
Type string `json:"type"`
}
type ETLSink struct {
AccessKeyId string `json:"accessKeyId"`
AccessKeySecret string `json:"accessKeySecret"`
Endpoint string `json:"endpoint"`
Logstore string `json:"logstore"`
Name string `json:"name"`
Project string `json:"project"`
RoleArn string `json:"roleArn,omitempty"`
Type string `json:"type,omitempty"`
}
type ListETLResponse struct {
Total int `json:"total"`
Count int `json:"count"`
Results []*ETL `json:"results"`
}
func NewETL(endpoint, accessKeyId, accessKeySecret, logstore, name, project string) ETL {
sink := ETLSink{
AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret,
Endpoint: endpoint,
Logstore: logstore,
Name: name,
Project: project,
Type: ETLSinksType,
}
config := ETLConfiguration{
AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret,
FromTime: time.Now().Unix(),
Script: "e_set('new','aliyun')",
Version: ETLVersion,
Logstore: logstore,
ETLSinks: []ETLSink{sink},
Parameters: map[string]string{},
}
schedule := ETLSchedule{
Type: "Resident",
}
etljob := ETL{
Configuration: config,
DisplayName: "displayname",
Description: "go sdk case",
Name: name,
Schedule: schedule,
Type: ETLType,
}
return etljob
}
func (c *Client) CreateETL(project string, etljob ETL) error {
body, err := json.Marshal(etljob)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/jobs"
r, err := c.request(project, "POST", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) GetETL(project string, etlName string) (ETLJob *ETL, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/jobs/" + etlName
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
etlJob := &ETL{}
if err = json.Unmarshal(buf, etlJob); err != nil {
err = NewClientError(err)
}
return etlJob, nil
}
func (c *Client) UpdateETL(project string, etljob ETL) error {
body, err := json.Marshal(etljob)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/jobs/" + etljob.Name
r, err := c.request(project, "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) DeleteETL(project string, etlName string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/jobs/" + etlName
r, err := c.request(project, "DELETE", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) ListETL(project string, offset int, size int) (*ListETLResponse, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/jobs?offset=%d&size=%d&jobType=ETL", offset, size)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
listETLResponse := &ListETLResponse{}
if err = json.Unmarshal(buf, listETLResponse); err != nil {
err = NewClientError(err)
}
return listETLResponse, err
}
func (c *Client) StartETL(project, name string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/jobs/%s?action=START", name)
r, err := c.request(project, "PUT", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) StopETL(project, name string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/jobs/%s?action=STOP", name)
r, err := c.request(project, "PUT", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) RestartETL(project string, etljob ETL) error {
body, err := json.Marshal(etljob)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/jobs/%s?action=RESTART", etljob.Name)
r, err := c.request(project, "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}