@@ -2,6 +2,7 @@ package sls
2
2
3
3
import (
4
4
"encoding/json"
5
+ "errors"
5
6
"fmt"
6
7
"io/ioutil"
7
8
"net/url"
@@ -12,6 +13,7 @@ type ResourcePool string
12
13
type DataFormat string
13
14
type JobType string
14
15
type Status string
16
+ type ScheduledSQLState string
15
17
16
18
const (
17
19
STANDARD SqlType = "standard"
@@ -42,6 +44,12 @@ const (
42
44
DISABLED Status = "Disabled"
43
45
)
44
46
47
+ const (
48
+ ScheduledSQL_RUNNING ScheduledSQLState = "RUNNING"
49
+ ScheduledSQL_FAILED ScheduledSQLState = "FAILED"
50
+ ScheduledSQL_SUCCEEDED ScheduledSQLState = "SUCCEEDED"
51
+ )
52
+
45
53
type ScheduledSQL struct {
46
54
Name string `json:"name"`
47
55
DisplayName string `json:"displayName"`
@@ -206,3 +214,98 @@ func (c *Client) ListScheduledSQL(project, name, displayName string, offset, siz
206
214
}
207
215
return scheduledSqlList .Results , scheduledSqlList .Total , scheduledSqlList .Count , err
208
216
}
217
+
218
+ type ScheduledSQLJobInstance struct {
219
+ InstanceId string `json:"instanceId"`
220
+ JobName string `json:"jobName,omitempty"`
221
+ DisplayName string `json:"displayName,omitempty"`
222
+ Description string `json:"description,omitempty"`
223
+ JobScheduleId string `json:"jobScheduleId,omitempty"`
224
+ CreateTimeInMillis int64 `json:"createTimeInMillis"`
225
+ ScheduleTimeInMillis int64 `json:"scheduleTimeInMillis"`
226
+ UpdateTimeInMillis int64 `json:"updateTimeInMillis"`
227
+ State ScheduledSQLState `json:"state"`
228
+ ErrorCode string `json:"errorCode"`
229
+ ErrorMessage string `json:"errorMessage"`
230
+ Summary string `json:"summary,omitempty"`
231
+ }
232
+
233
+ func (c * Client ) GetScheduledSQLJobInstance (projectName , jobName , instanceId string , result bool ) (* ScheduledSQLJobInstance , error ) {
234
+ h := map [string ]string {
235
+ "x-log-bodyrawsize" : "0" ,
236
+ "Content-Type" : "application/json" ,
237
+ }
238
+ uri := fmt .Sprintf ("/jobs/%s/jobinstances/%s?result=%t" , jobName , instanceId , result )
239
+ r , err := c .request (projectName , "GET" , uri , h , nil )
240
+ if err != nil {
241
+ return nil , err
242
+ }
243
+ defer r .Body .Close ()
244
+ buf , _ := ioutil .ReadAll (r .Body )
245
+ instance := & ScheduledSQLJobInstance {}
246
+ if err = json .Unmarshal (buf , instance ); err != nil {
247
+ err = NewClientError (err )
248
+ }
249
+ return instance , err
250
+ }
251
+
252
+ func (c * Client ) ModifyScheduledSQLJobInstanceState (projectName , jobName , instanceId string , state ScheduledSQLState ) error {
253
+ if ScheduledSQL_RUNNING != state {
254
+ return NewClientError (errors .New (fmt .Sprintf ("Invalid state: %s, state must be RUNNING." , state )))
255
+ }
256
+ h := map [string ]string {
257
+ "x-log-bodyrawsize" : "0" ,
258
+ "Content-Type" : "application/json" ,
259
+ }
260
+
261
+ uri := fmt .Sprintf ("/jobs/%s/jobinstances/%s?state=%s" , jobName , instanceId , state )
262
+ r , err := c .request (projectName , "PUT" , uri , h , nil )
263
+ if err != nil {
264
+ return err
265
+ }
266
+ r .Body .Close ()
267
+ return nil
268
+ }
269
+
270
+ type InstanceStatus struct {
271
+ FromTime int64
272
+ ToTime int64
273
+ Offset int64
274
+ Size int64
275
+ State ScheduledSQLState
276
+ }
277
+
278
+ func (c * Client ) ListScheduledSQLJobInstances (projectName , jobName string , status * InstanceStatus ) (instances []* ScheduledSQLJobInstance , total , count int64 , err error ) {
279
+ h := map [string ]string {
280
+ "x-log-bodyrawsize" : "0" ,
281
+ "Content-Type" : "application/json" ,
282
+ }
283
+ v := url.Values {}
284
+ v .Add ("jobType" , "ScheduledSQL" )
285
+ v .Add ("start" , fmt .Sprintf ("%d" , status .FromTime ))
286
+ v .Add ("end" , fmt .Sprintf ("%d" , status .ToTime ))
287
+ v .Add ("offset" , fmt .Sprintf ("%d" , status .Offset ))
288
+ v .Add ("size" , fmt .Sprintf ("%d" , status .Size ))
289
+ if status .State != "" {
290
+ v .Add ("state" , string (status .State ))
291
+ }
292
+
293
+ uri := fmt .Sprintf ("/jobs/%s/jobinstances?%s" , jobName , v .Encode ())
294
+ r , err := c .request (projectName , "GET" , uri , h , nil )
295
+ if err != nil {
296
+ return nil , 0 , 0 , err
297
+ }
298
+ defer r .Body .Close ()
299
+
300
+ type ScheduledSqlJobInstances struct {
301
+ Total int64 `json:"total"`
302
+ Count int64 `json:"count"`
303
+ Results []* ScheduledSQLJobInstance `json:"results"`
304
+ }
305
+ buf , _ := ioutil .ReadAll (r .Body )
306
+ jobInstances := & ScheduledSqlJobInstances {}
307
+ if err = json .Unmarshal (buf , jobInstances ); err != nil {
308
+ err = NewClientError (err )
309
+ }
310
+ return jobInstances .Results , jobInstances .Total , jobInstances .Count , err
311
+ }
0 commit comments