Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Scheduled SQL #128

Merged
merged 4 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,9 @@ type ClientInterface interface {
resourceIDs []string,
tags []ResourceFilterTag,
nextToken string) (respTags []*ResourceTagResponse, respNextToken string, err error)
CreateScheduledSQL(project string, scheduledsql *ScheduledSQL) error
DeleteScheduledSQL(project string, name string) error
UpdateScheduledSQL(project string, scheduledsql *ScheduledSQL) error
GetScheduledSQL(project string, name string) (*ScheduledSQL, error)
ListScheduledSQL(project, name, displayName string, offset, size int) ([]*ScheduledSQL, int, int, error)
}
188 changes: 188 additions & 0 deletions client_scheduled_sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package sls

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
)

type ScheduledSQL struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
Status string `json:"status"`
ScheduleId string `json:"scheduleId"`
Configuration *ScheduledSQLConfiguration `json:"configuration"`
Schedule *Schedule `json:"schedule"`
CreateTime int64 `json:"createTime,omitempty"`
LastModifiedTime int64 `json:"lastModifiedTime,omitempty"`
}

type ScheduledSQLConfiguration struct {
SourceLogStore string `json:"sourceLogstore"`
DestProject string `json:"destProject"`
DestEndpoint string `json:"destEndpoint"`
DestLogStore string `json:"destLogstore"`
Script string `json:"script"`
SqlType string `json:"sqlType"`
ResourcePool string `json:"resourcePool"`
RoleArn string `json:"roleArn"`
DestRoleArn string `json:"destRoleArn"`
FromTimeExpr string `json:"fromTimeExpr"`
ToTimeExpr string `json:"toTimeExpr"`
MaxRunTimeInSeconds int32 `json:"maxRunTimeInSeconds"`
MaxRetries int32 `json:"maxRetries"`
FromTime int64 `json:"fromTime"`
ToTime int64 `json:"toTime"`
DataFormat string `json:"dataFormat"`
Parameters *ScheduledSQLParameters `json:"parameters,omitempty"`
}

func (s *ScheduledSQL) MarshalJSON() ([]byte, error) {
body := map[string]interface{}{
"name": s.Name,
"displayName": s.DisplayName,
"description": s.Description,
"status": s.Status,
"scheduleId": s.ScheduleId,
"configuration": s.Configuration,
"schedule": s.Schedule,
"createTime": s.CreateTime,
"lastModifiedTime": s.LastModifiedTime,
"type": "ScheduledSQL",
}
return json.Marshal(body)
}

func NewScheduledSQLConfiguration() *ScheduledSQLConfiguration {
return &ScheduledSQLConfiguration{
SqlType: "standard",
HuiJing-C marked this conversation as resolved.
Show resolved Hide resolved
ResourcePool: "default",
HuiJing-C marked this conversation as resolved.
Show resolved Hide resolved
FromTime: 0,
ToTime: 0,
DataFormat: "log2log",
HuiJing-C marked this conversation as resolved.
Show resolved Hide resolved
}
}

type ScheduledSQLParameters struct {
TimeKey string `json:"timeKey,omitempty"`
LabelKeys string `json:"labelKeys,omitempty"`
MetricKeys string `json:"metricKeys,omitempty"`
MetricName string `json:"metricName,omitempty"`
HashLabels string `json:"hashLabels,omitempty"`
AddLabels string `json:"addLabels,omitempty"`
}

func (c *Client) CreateScheduledSQL(project string, scheduledsql *ScheduledSQL) error {
fromTime := scheduledsql.Configuration.FromTime
toTime := scheduledsql.Configuration.ToTime
timeRange := fromTime > 1451577600 && toTime > fromTime
sustained := fromTime > 1451577600 && toTime == 0
if !timeRange && !sustained {
return fmt.Errorf("invalid fromTime: %d toTime: %d, please ensure fromTime more than 1451577600", fromTime, toTime)
}
body, err := scheduledsql.MarshalJSON()
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) DeleteScheduledSQL(project string, name string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}

uri := "/jobs/" + name
r, err := c.request(project, "DELETE", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}

func (c *Client) UpdateScheduledSQL(project string, scheduledsql *ScheduledSQL) error {
body, err := scheduledsql.MarshalJSON()
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}

uri := "/jobs/" + scheduledsql.Name
r, err := c.request(project, "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}

func (c *Client) GetScheduledSQL(project string, name string) (*ScheduledSQL, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/jobs/" + name
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
scheduledSQL := &ScheduledSQL{}
if err = json.Unmarshal(buf, scheduledSQL); err != nil {
err = NewClientError(err)
}
return scheduledSQL, err
}

func (c *Client) ListScheduledSQL(project, name, displayName string, offset, size int) (scheduledsqls []*ScheduledSQL, total, count int, error error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
v := url.Values{}
v.Add("jobName", name)
if displayName != "" {
v.Add("displayName", displayName)
}
v.Add("jobType", "ScheduledSQL")
v.Add("offset", fmt.Sprintf("%d", offset))
v.Add("size", fmt.Sprintf("%d", size))

uri := "/jobs?" + v.Encode()
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, 0, 0, err
}
defer r.Body.Close()

type ScheduledSqlList struct {
Total int `json:"total"`
Count int `json:"count"`
Results []*ScheduledSQL `json:"results"`
}
buf, _ := ioutil.ReadAll(r.Body)
scheduledSqlList := &ScheduledSqlList{}
if err = json.Unmarshal(buf, scheduledSqlList); err != nil {
err = NewClientError(err)
}
return scheduledSqlList.Results, scheduledSqlList.Total, scheduledSqlList.Count, err
}
19 changes: 19 additions & 0 deletions client_scheduled_sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sls

import (
"fmt"
"os"
"testing"
)

func TestList(t *testing.T) {
AccessKeyID := os.Getenv("ALICLOUD_ACCESS_KEY")
AccessKeySecret := os.Getenv("ALICLOUD_SECRET_KEY")
Endpoint := "cn-hangzhou.log.aliyuncs.com"
client := CreateNormalInterface(Endpoint, AccessKeyID, AccessKeySecret, "")
scheduledSQL, err := client.GetScheduledSQL("", "")
if err != nil {
fmt.Printf("%v", err)
}
fmt.Printf("%v", scheduledSQL)
}
51 changes: 51 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,3 +1179,54 @@ func (c *TokenAutoUpdateClient) ListTagResources(project string,
}
return
}

// ####################### Scheduled SQL API ######################
func (c *TokenAutoUpdateClient) CreateScheduledSQL(project string, scheduledsql *ScheduledSQL) (err error) {
for i := 0; i < c.maxTryTimes; i++ {
err = c.logClient.CreateScheduledSQL(project, scheduledsql)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) DeleteScheduledSQL(project string, name string) (err error) {
for i := 0; i < c.maxTryTimes; i++ {
err = c.logClient.DeleteScheduledSQL(project, name)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) UpdateScheduledSQL(project string, scheduledsql *ScheduledSQL) (err error) {
for i := 0; i < c.maxTryTimes; i++ {
err = c.logClient.UpdateScheduledSQL(project, scheduledsql)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) GetScheduledSQL(project string, name string) (s *ScheduledSQL, err error) {
for i := 0; i < c.maxTryTimes; i++ {
s, err = c.logClient.GetScheduledSQL(project, name)
if !c.processError(err) {
return
}
}
return
}

func (c *TokenAutoUpdateClient) ListScheduledSQL(project, name, displayName string, offset, size int) (scheduledsqls []*ScheduledSQL, total, count int, err error) {
for i := 0; i < c.maxTryTimes; i++ {
scheduledsqls, total, count, err = c.logClient.ListScheduledSQL(project, name, displayName, offset, size)
if !c.processError(err) {
return
}
}
return
}