From bfb55c9239b292614169f683820bc935fb86db22 Mon Sep 17 00:00:00 2001 From: "weilong.pwl" Date: Mon, 30 Aug 2021 14:48:49 +0800 Subject: [PATCH 1/2] add alert and resource api --- .gitignore | 1 + client_alert.go | 133 +++++++++++++++++++++-- client_alert_resource.go | 120 +++++++++++++++++++++ client_alert_resource_test.go | 129 ++++++++++++++++++++++ client_alert_test.go | 185 +++++++++++++++++++++++++++++-- client_request.go | 2 +- client_resource.go | 191 +++++++++++++++++++++++++++++++++ client_resource_record.go | 166 ++++++++++++++++++++++++++++ client_resource_record_test.go | 141 ++++++++++++++++++++++++ client_resource_test.go | 137 +++++++++++++++++++++++ 10 files changed, 1186 insertions(+), 19 deletions(-) create mode 100644 .gitignore create mode 100644 client_alert_resource.go create mode 100644 client_alert_resource_test.go create mode 100644 client_resource.go create mode 100644 client_resource_record.go create mode 100644 client_resource_record_test.go create mode 100644 client_resource_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..485dee64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/client_alert.go b/client_alert.go index 16a7f8c3..989e7982 100644 --- a/client_alert.go +++ b/client_alert.go @@ -30,6 +30,38 @@ const ( NotificationTypeMessageCenter = "MessageCenter" ) +const ( + CountConditionKey = "__count__" +) + +type Severity int + +const ( + Report Severity = 2 + Low Severity = 4 + Medium Severity = 6 + High Severity = 8 + Critical Severity = 10 +) + +const ( + JoinTypeCross = "cross_join" + JoinTypeInner = "inner_join" + JoinTypeLeft = "left_join" + JoinTypeRight = "right_join" + JoinTypeFull = "full_join" + JoinTypeLeftExclude = "left_exclude" + JoinTypeRightExclude = "right_exclude" + JoinTypeConcat = "concat" + JoinTypeNo = "no_join" +) + +const ( + GroupTypeNoGroup = "no_group" + GroupTypeLabelsAuto = "labels_auto" + GroupTypeCustom = "custom" +) + const ( ScheduleTypeFixedRate = "FixedRate" ScheduleTypeHourly = "Hourly" @@ -40,6 +72,63 @@ const ( ScheduleTypeResident = "Resident" ) +const ( + StoreTypeLog = "log" + StoreTypeMetric = "metric" + StoreTypeMeta = "meta" +) + +// SeverityConfiguration severity config by group +type SeverityConfiguration struct { + Severity Severity `json:"severity"` + EvalCondition ConditionConfiguration `json:"evalCondition"` +} + +type ConditionConfiguration struct { + Condition string `json:"condition"` + CountCondition string `json:"countCondition"` +} + +type JoinConfiguration struct { + Type string `json:"type"` + Condition string `json:"condition"` +} + +type GroupConfiguration struct { + Type string `json:"type"` + Fields []string `json:"fields"` +} + +type Tag struct { + Key string `json:"key"` + Value string `json:"value"` +} + +type Token struct { + Name string `json:"name"` + DisplayName string `json:"display_name"` + Required bool `json:"required"` + Type string `json:"type"` + Default string `json:"default"` + Hide bool `json:"hide"` +} + +type TemplateConfiguration struct { + Id string `json:"id"` + Type string `json:"type"` + Version string `json:"version"` + Lang string `json:"lang"` + Tokens map[string]string `json:"tokens"` + Annotations map[string]string `json:"annotations"` +} + +type PolicyConfiguration struct { + UseDefault bool `json:"useDefault"` + RepeatInterval string `json:"repeatInterval"` + AlertPolicyId string `json:"alertPolicyId"` + ActionPolicyId string `json:"actionPolicyId"` +} + type Alert struct { Name string `json:"name"` DisplayName string `json:"displayName"` @@ -66,16 +155,6 @@ func (alert *Alert) MarshalJSON() ([]byte, error) { return json.Marshal(body) } -type AlertConfiguration struct { - Condition string `json:"condition"` - Dashboard string `json:"dashboard"` - QueryList []*AlertQuery `json:"queryList"` - MuteUntil int64 `json:"muteUntil"` - NotificationList []*Notification `json:"notificationList"` - NotifyThreshold int32 `json:"notifyThreshold"` - Throttling string `json:"throttling"` -} - type AlertQuery struct { ChartTitle string `json:"chartTitle"` LogStore string `json:"logStore"` @@ -83,6 +162,13 @@ type AlertQuery struct { TimeSpanType string `json:"timeSpanType"` Start string `json:"start"` End string `json:"end"` + + StoreType string `json:"storeType"` + Project string `json:"project"` + Store string `json:"store"` + Region string `json:"region"` + RoleArn string `json:"roleArn"` + DashboardId string `json:"dashboardId"` } type Notification struct { @@ -104,6 +190,33 @@ type Schedule struct { Hour int32 `json:"hour"` } +type AlertConfiguration struct { + Condition string `json:"condition"` + MuteUntil int64 `json:"muteUntil"` + NotificationList []*Notification `json:"notificationList"` + NotifyThreshold int32 `json:"notifyThreshold"` + Throttling string `json:"throttling"` + + Version string `json:"version"` + Type string `json:"type"` + TemplateConfiguration *TemplateConfiguration `json:"templateConfiguration"` + + Dashboard string `json:"dashboard"` + Threshold int `json:"threshold"` + NoDataFire bool `json:"noDataFire"` + NoDataSeverity Severity `json:"noDataSeverity"` + SendResolved bool `json:"sendResolved"` + QueryList []*AlertQuery `json:"queryList"` + Annotations []*Tag `json:"annotations"` + Labels []*Tag `json:"labels"` + SeverityConfigurations []*SeverityConfiguration `json:"severityConfigurations"` + + JoinConfigurations []*JoinConfiguration `json:"joinConfigurations"` + GroupConfiguration GroupConfiguration `json:"groupConfiguration"` + + PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"` +} + func (c *Client) CreateSavedSearch(project string, savedSearch *SavedSearch) error { body, err := json.Marshal(savedSearch) if err != nil { diff --git a/client_alert_resource.go b/client_alert_resource.go new file mode 100644 index 00000000..80612aec --- /dev/null +++ b/client_alert_resource.go @@ -0,0 +1,120 @@ +package sls + +import "encoding/json" + +const ( + ResourceNameAlertPolicy = "sls.alert.alert_policy" + ResourceNameActionPolicy = "sls.alert.action_policy" + ResourceNameUser = "sls.common.user" + ResourceNameUserGroup = "sls.common.user_group" + ResourceNameContentTemplate = "sls.alert.content_template" + ResourceNameGlobalConfig = "sls.alert.global_config" + ResourceNameWebhookIntegration = "sls.alert.webhook_application" +) + +type ( + // Notified users. + ResourceUser struct { + UserId string `json:"user_id"` + UserName string `json:"user_name"` + Enabled bool `json:"enabled"` + CountryCode string `json:"country_code"` + Phone string `json:"phone"` + Email []string `json:"email"` + SmsEnabled bool `json:"sms_enabled"` + VoiceEnabled bool `json:"voice_enabled"` + } + + // ResourceUserGroup is a collection of users. + ResourceUserGroup struct { + Id string `json:"user_group_id"` + Name string `json:"user_group_name"` + Enabled bool `json:"enabled"` + Members []string `json:"members"` + } + + // ResourceAlertPolicy defines how alerts should be grouped, inhibited and silenced. + ResourceAlertPolicy struct { + PolicyId string `json:"policy_id"` + PolicyName string `json:"policy_name"` + Parent string `json:"parent_id"` + IsDefault bool `json:"is_default"` + GroupPolicy string `json:"group_script"` + InhibitPolicy string `json:"inhibit_script"` + SilencePolicy string `json:"silence_script"` + } + + // ResourceActionPolicy defines how to send alert notifications. + ResourceActionPolicy struct { + ActionPolicyId string `json:"action_policy_id"` + ActionPolicyName string `json:"action_policy_name"` + IsDefault bool `json:"is_default"` + PrimaryPolicyScript string `json:"primary_policy_script"` + SecondaryPolicyScript string `json:"secondary_policy_script"` + EscalationStartEnabled bool `json:"escalation_start_enabled"` + EscalationStartTimeout string `json:"escalation_start_timeout"` + EscalationInprogressEnabled bool `json:"escalation_inprogress_enabled"` + EscalationInprogressTimeout string `json:"escalation_inprogress_timeout"` + EscalationEnabled bool `json:"escalation_enabled"` + EscalationTimeout string `json:"escalation_timeout"` + Labels map[string]string `json:"labels"` + } + + // ContentTemplate + ResourceTemplate struct { + Content string `json:"content"` + Locale string `json:"locale"` + Title string `json:"title"` + Subject string `json:"subject"` + SendType string `json:"send_type"` + Limit int `json:"limit"` + } + + ResourceTemplates struct { + Sms ResourceTemplate `json:"sms"` + Voice ResourceTemplate `json:"voice"` + Email ResourceTemplate `json:"email"` + Dingtalk ResourceTemplate `json:"dingtalk"` + Webhook ResourceTemplate `json:"webhook"` + MessageCenter ResourceTemplate `json:"message_center"` + Wechat ResourceTemplate `json:"wechat"` + Lark ResourceTemplate `json:"lark"` + Slack ResourceTemplate `json:"slack"` + } + ResourceContentTemplate struct { + TemplateId string `json:"template_id"` + TemplateName string `json:"template_name"` + IsDefault bool `json:"is_default"` + Templates ResourceTemplates `json:"templates"` + } + + // WebhookIntegration is a wrap of webhook notification config. + ResourceWebhookHeader struct { + Key string `json:"key"` + Value string `json:"value"` + } + WebhookIntegration struct { + Id string `json:"id"` + Name string `json:"name"` + Method string `json:"method"` + Url string `json:"url"` + Type string `json:"type"` + Headers []*ResourceWebhookHeader `json:"headers"` + } + + // GlobalConfig is the global configuration for alerts. + GlobalConfig struct { + ConfigId string `json:"config_id"` + ConfigName string `json:"config_name"` + ConfigDetail struct { + AlertCenterLog struct { + Region string `json:"region"` + } `json:"alert_center_log"` + } `json:"config_detail"` + } +) + +func JsonMarshal(v interface{}) string { + vBytes, _ := json.Marshal(v) + return string(vBytes) +} diff --git a/client_alert_resource_test.go b/client_alert_resource_test.go new file mode 100644 index 00000000..faf6e434 --- /dev/null +++ b/client_alert_resource_test.go @@ -0,0 +1,129 @@ +package sls + +import ( + "encoding/json" + "os" + "testing" + + "github.com/stretchr/testify/suite" +) + +func TestUser(t *testing.T) { + suite.Run(t, new(UserTestSuite)) +} + +type UserTestSuite struct { + suite.Suite + endpoint string + projectName string + logstoreName string + accessKeyID string + accessKeySecret string + client Client + userId string + userName string + resourceName string +} + +func (s *UserTestSuite) SetupSuite() { + s.endpoint = os.Getenv("LOG_TEST_ENDPOINT") + s.projectName = os.Getenv("LOG_TEST_PROJECT") + s.logstoreName = os.Getenv("LOG_TEST_LOGSTORE") + s.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID") + s.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET") + s.client.AccessKeyID = s.accessKeyID + s.client.AccessKeySecret = s.accessKeySecret + s.client.Endpoint = s.endpoint + s.resourceName = ResourceNameUser + s.userId = "user_id_1" + s.userName = "user name 1" + + _ = s.client.DeleteResourceRecord(s.resourceName, s.userId) +} + +func (s *UserTestSuite) TearDownSuite() { + _ = s.client.DeleteResourceRecord(s.resourceName, s.userId) +} + +func (s *UserTestSuite) createUser() error { + customUser := new(ResourceRecord) + user := new(ResourceUser) + user.UserId = s.userId + user.UserName = s.userName + user.Phone = "13888888888" + user.CountryCode = "86" + user.Enabled = true + user.VoiceEnabled = true + user.SmsEnabled = true + customUser.Id = s.userId + customUser.Tag = s.userName + customUser.Value = JsonMarshal(user) + return s.client.CreateResourceRecord(s.resourceName, customUser) +} + +func (s *UserTestSuite) TestClient_CreateUser() { + err := s.createUser() + s.Require().Nil(err) + err = s.client.DeleteResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) +} + +func (s *UserTestSuite) TestClient_UpdateUser() { + err := s.createUser() + s.Require().Nil(err) + resourceRecord, err := s.client.GetResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) + user := new(ResourceUser) + err = json.Unmarshal([]byte(resourceRecord.Value), user) + s.Require().Nil(err) + user.UserName = "new name" + resourceRecord.Value = JsonMarshal(user) + err = s.client.UpdateResourceRecord(s.resourceName, resourceRecord) + s.Require().Nil(err) + resourceRecord, err = s.client.GetResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) + nUser := new(ResourceUser) + err = json.Unmarshal([]byte(resourceRecord.Value), nUser) + s.Require().Nil(err) + s.Require().Equal(`new name`, nUser.UserName, "update resourceRecord failed") + err = s.client.DeleteResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) +} + +func (s *UserTestSuite) TestClient_DeleteUser() { + err := s.createUser() + s.Require().Nil(err) + _, err = s.client.GetResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) + err = s.client.DeleteResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) + _, err = s.client.GetResourceRecord(s.resourceName, s.userId) + s.Require().NotNil(err) +} + +func (s *UserTestSuite) TestClient_GetUser() { + err := s.createUser() + s.Require().Nil(err) + getUserRecord, err := s.client.GetResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) + user := new(ResourceUser) + err = json.Unmarshal([]byte(getUserRecord.Value), user) + s.Require().Nil(err) + s.Require().Equal(user.UserId, s.userId) + + err = s.client.DeleteResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) +} + +func (s *UserTestSuite) TestClient_ListUser() { + err := s.createUser() + s.Require().Nil(err) + resourceRecords, total, count, err := s.client.ListResourceRecord(s.resourceName, 0, 100) + s.Require().Nil(err) + if total < 1 || count < 1 { + s.Require().Fail("list resourceRecord failed") + } + s.Require().Equal(count, len(resourceRecords), "there should be more than one resourceRecord") + err = s.client.DeleteResourceRecord(s.resourceName, s.userId) + s.Require().Nil(err) +} diff --git a/client_alert_test.go b/client_alert_test.go index ad6a069c..fb9075cd 100644 --- a/client_alert_test.go +++ b/client_alert_test.go @@ -46,13 +46,6 @@ func (s *AlertTestSuite) SetupTest() { } } -func (s *AlertTestSuite) TestClient_CreateAlert() { - err := s.createAlert() - s.Require().Nil(err) - err = s.client.DeleteAlert(s.projectName, s.alertName) - s.Require().Nil(err) -} - func (s *AlertTestSuite) createAlert() error { alerts, _, _, err := s.client.ListAlert(s.projectName, "", "", 0, 100) s.Require().Nil(err) @@ -76,7 +69,7 @@ func (s *AlertTestSuite) createAlert() error { alert := &Alert{ Name: s.alertName, State: "Enabled", - DisplayName: "AlertTest", + DisplayName: "AlertTest By GO SDK", Description: "Description for alert", Schedule: &Schedule{ Type: ScheduleTypeFixedRate, @@ -122,6 +115,111 @@ func (s *AlertTestSuite) createAlert() error { return s.client.CreateAlert(s.projectName, alert) } +func (s *AlertTestSuite) createAlert2() error { + alerts, _, _, err := s.client.ListAlert(s.projectName, "", "", 0, 100) + s.Require().Nil(err) + for _, x := range alerts { + err = s.client.DeleteAlert(s.projectName, x.Name) + s.Require().Nil(err) + } + alert := &Alert{ + Name: s.alertName, + State: "Enabled", + DisplayName: "AlertTest By GO SDK ", + Description: "Description for alert by go sdk", + Schedule: &Schedule{ + Type: ScheduleTypeFixedRate, + Interval: "1m", + }, + Configuration: &AlertConfiguration{ + GroupConfiguration: GroupConfiguration{ + Type: GroupTypeNoGroup, + }, + QueryList: []*AlertQuery{ + { + Query: "* | select count(1) as count", + Start: "-120s", + End: "now", + TimeSpanType: "Custom", + StoreType: StoreTypeLog, + Store: "test-alert", + Region: "cn-hangzhou", + Project: s.projectName, + }, + }, + Dashboard: s.dashboardName, + MuteUntil: time.Now().Unix(), + Version: "2.0", + Type: "default", + Threshold: 1, + NoDataFire: true, + NoDataSeverity: Medium, + SendResolved: true, + Annotations: []*Tag{ + &Tag{ + Key: "title", + Value: "this is title", + }, + &Tag{ + Key: "desc", + Value: "this is desc, count is ${count}", + }, + }, + Labels: []*Tag{ + &Tag{ + Key: "env", + Value: "test", + }, + }, + SeverityConfigurations: []*SeverityConfiguration{ + &SeverityConfiguration{ + Severity: Critical, + EvalCondition: ConditionConfiguration{ + Condition: "count > 99", + }, + }, + &SeverityConfiguration{ + Severity: High, + EvalCondition: ConditionConfiguration{ + Condition: "count > 80", + }, + }, + &SeverityConfiguration{ + Severity: Medium, + EvalCondition: ConditionConfiguration{ + Condition: "count > 20", + }, + }, + &SeverityConfiguration{ + Severity: Report, + EvalCondition: ConditionConfiguration{ + }, + }, + }, + PolicyConfiguration: PolicyConfiguration{ + AlertPolicyId: "sls.builtin.dynamic", + ActionPolicyId: "huolang.test", + RepeatInterval: "5m", + }, + }, + } + return s.client.CreateAlert(s.projectName, alert) +} + +func (s *AlertTestSuite) TestClient_CreateAlert() { + err := s.createAlert() + s.Require().Nil(err) + err = s.client.DeleteAlert(s.projectName, s.alertName) + s.Require().Nil(err) +} + +func (s *AlertTestSuite) TestClient_CreateAlert2() { + err := s.createAlert2() + s.Require().Nil(err) + err = s.client.DeleteAlert(s.projectName, s.alertName) + s.Require().Nil(err) +} + func (s *AlertTestSuite) TestClient_UpdateAlert() { err := s.createAlert() s.Require().Nil(err) @@ -139,6 +237,23 @@ func (s *AlertTestSuite) TestClient_UpdateAlert() { s.Require().Nil(err) } +func (s *AlertTestSuite) TestClient_UpdateAlert2() { + err := s.createAlert2() + s.Require().Nil(err) + alert, err := s.client.GetAlert(s.projectName, s.alertName) + s.Require().Nil(err) + alert.DisplayName = "new display name" + alert.CreateTime = 0 + alert.LastModifiedTime = 0 + err = s.client.UpdateAlert(s.projectName, alert) + s.Require().Nil(err) + alert, err = s.client.GetAlert(s.projectName, s.alertName) + s.Require().Nil(err) + s.Require().Equal("new display name", alert.DisplayName, "update alert failed") + err = s.client.DeleteAlert(s.projectName, s.alertName) + s.Require().Nil(err) +} + func (s *AlertTestSuite) TestClient_DeleteAlert() { err := s.createAlert() s.Require().Nil(err) @@ -150,6 +265,17 @@ func (s *AlertTestSuite) TestClient_DeleteAlert() { s.Require().NotNil(err) } +func (s *AlertTestSuite) TestClient_DeleteAlert2() { + err := s.createAlert2() + s.Require().Nil(err) + _, err = s.client.GetAlert(s.projectName, s.alertName) + s.Require().Nil(err) + err = s.client.DeleteAlert(s.projectName, s.alertName) + s.Require().Nil(err) + _, err = s.client.GetAlert(s.projectName, s.alertName) + s.Require().NotNil(err) +} + func (s *AlertTestSuite) TestClient_DisableAndEnableAlert() { err := s.createAlert() s.Require().Nil(err) @@ -167,6 +293,23 @@ func (s *AlertTestSuite) TestClient_DisableAndEnableAlert() { s.Require().Nil(err) } +func (s *AlertTestSuite) TestClient_DisableAndEnableAlert2() { + err := s.createAlert2() + s.Require().Nil(err) + err = s.client.DisableAlert(s.projectName, s.alertName) + s.Require().Nil(err) + alert, err := s.client.GetAlert(s.projectName, s.alertName) + s.Require().Nil(err) + s.Require().Equal("Disabled", alert.State, "disable alert failed") + err = s.client.EnableAlert(s.projectName, s.alertName) + s.Require().Nil(err) + alert, err = s.client.GetAlert(s.projectName, s.alertName) + s.Require().Nil(err) + s.Require().Equal("Enabled", alert.State, "enable alert failed") + err = s.client.DeleteAlert(s.projectName, s.alertName) + s.Require().Nil(err) +} + func (s *AlertTestSuite) TestClient_GetAlert() { err := s.createAlert() s.Require().Nil(err) @@ -185,6 +328,17 @@ func (s *AlertTestSuite) TestClient_GetAlert() { s.Require().Nil(err) } +func (s *AlertTestSuite) TestClient_GetAlert2() { + err := s.createAlert2() + s.Require().Nil(err) + getAlert, err := s.client.GetAlert(s.projectName, s.alertName) + s.Require().Nil(err) + s.Require().Equal(getAlert.Name, s.alertName) + s.Require().Equal(getAlert.Configuration.GroupConfiguration.Type, GroupTypeNoGroup) + err = s.client.DeleteAlert(s.projectName, s.alertName) + s.Require().Nil(err) +} + func (s *AlertTestSuite) TestClient_ListAlert() { err := s.createAlert() s.Require().Nil(err) @@ -199,3 +353,18 @@ func (s *AlertTestSuite) TestClient_ListAlert() { err = s.client.DeleteAlert(s.projectName, s.alertName) s.Require().Nil(err) } + +func (s *AlertTestSuite) TestClient_ListAlert2() { + err := s.createAlert2() + s.Require().Nil(err) + alerts, total, count, err := s.client.ListAlert(s.projectName, "", "", 0, 100) + s.Require().Nil(err) + if total != 1 || count != 1 { + s.Require().Fail("list alert failed") + } + s.Require().Equal(1, len(alerts), "there should be only one alert") + alert := alerts[0] + s.Require().Equal(s.alertName, alert.Name, "list alert failed") + err = s.client.DeleteAlert(s.projectName, s.alertName) + s.Require().Nil(err) +} diff --git a/client_request.go b/client_request.go index b5ec325a..6eb45c48 100644 --- a/client_request.go +++ b/client_request.go @@ -37,7 +37,7 @@ func (c *Client) request(project, method, uri string, headers map[string]string, // SLS public request headers var hostStr string if len(project) == 0 { - hostStr = project + hostStr = endpoint } else { hostStr = project + "." + endpoint } diff --git a/client_resource.go b/client_resource.go new file mode 100644 index 00000000..400c8190 --- /dev/null +++ b/client_resource.go @@ -0,0 +1,191 @@ +package sls + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "strconv" +) + +const ResourceTypeUserDefine = "userdefine" + +type Resource struct { + Name string `json:"name"` + Type string `json:"type"` + Schema string `json:"schema"` + Description string `json:"description"` + ExtInfo string `json:"extInfo"` + CreateTime int64 `json:"createTime"` + LastModifyTime int64 `json:"lastModifyTime"` +} + +type ResourceSchema struct { + Schema []*ResourceSchemaItem `json:"schema"` +} + +type ResourceSchemaItem struct { + Column string `json:"column"` + Desc string `json:"desc"` + ExtInfo interface{} `json:"ext_info"` + Required bool `json:"required"` + Type string `json:"type"` +} + +func (rs *ResourceSchema) ToString() string { + rsBytes, _ := json.Marshal(rs) + return string(rsBytes) +} + +func (rs *ResourceSchema) FromJsonString(schema string) error { + return json.Unmarshal([]byte(schema), rs) +} + +func (c *Client) CreateResourceString(resourceStr string) error { + body := []byte(resourceStr) + + h := map[string]string{ + "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), + "Content-Type": "application/json", + } + + uri := "/resources" + r, err := c.request("", "POST", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) CreateResource(resource *Resource) error { + body, err := json.Marshal(resource) + if err != nil { + return NewClientError(err) + } + + h := map[string]string{ + "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), + "Content-Type": "application/json", + } + + uri := "/resources" + r, err := c.request("", "POST", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) UpdateResource(resource *Resource) error { + body, err := json.Marshal(resource) + if err != nil { + return NewClientError(err) + } + + h := map[string]string{ + "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), + "Content-Type": "application/json", + } + + uri := "/resources/" + resource.Name + r, err := c.request("", "PUT", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) UpdateResourceString(resourceName, resourceStr string) error { + body := []byte(resourceStr) + + h := map[string]string{ + "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), + "Content-Type": "application/json", + } + + uri := "/resources/" + resourceName + r, err := c.request("", "PUT", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) DeleteResource(name string) error { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + } + + uri := "/resources/" + name + r, err := c.request("", "DELETE", uri, h, nil) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) GetResource(name string) (resource *Resource, err error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + } + uri := "/resources/" + name + r, err := c.request("", "GET", uri, h, nil) + if err != nil { + return nil, err + } + defer r.Body.Close() + buf, _ := ioutil.ReadAll(r.Body) + resource = &Resource{} + if err = json.Unmarshal(buf, resource); err != nil { + err = NewClientError(err) + } + return resource, err +} + +func (c *Client) GetResourceString(name string) (resource string, err error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + } + uri := "/resources/" + name + r, err := c.request("", "GET", uri, h, nil) + if err != nil { + return "", err + } + defer r.Body.Close() + buf, err := ioutil.ReadAll(r.Body) + return string(buf), err +} + +func (c *Client) ListResource(resourceType string, resourceName string, offset, size int) (resourceList []*Resource, count, total int, err error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + "offset": strconv.Itoa(offset), + "size": strconv.Itoa(size), + } + uri := fmt.Sprintf("/resources?type=%s&names=%s", resourceType, resourceName) + r, err := c.request("", "GET", uri, h, nil) + if err != nil { + return nil, 0, 0, err + } + defer r.Body.Close() + type ListResourceResponse struct { + ResourceList []*Resource `json:"Items"` + Total int `json:"total"` + Count int `json:"count"` + } + + buf, _ := ioutil.ReadAll(r.Body) + resources := &ListResourceResponse{} + if err = json.Unmarshal(buf, resources); err != nil { + err = NewClientError(err) + } + return resources.ResourceList, resources.Count, resources.Total, err +} diff --git a/client_resource_record.go b/client_resource_record.go new file mode 100644 index 00000000..42c1a532 --- /dev/null +++ b/client_resource_record.go @@ -0,0 +1,166 @@ +package sls + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "strconv" +) + +type ResourceRecord struct { + Id string `json:"id"` + Tag string `json:"tag"` + Value string `json:"value"` + CreateTime int64 `json:"createTime"` + LastModifyTime int64 `json:"lastModifyTime"` +} + +func (c *Client) CreateResourceRecordString(resourceName, resourceStr string) error { + body := []byte(resourceStr) + + h := map[string]string{ + "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), + "Content-Type": "application/json", + } + + uri := fmt.Sprintf("/resources/%s/records", resourceName) + r, err := c.request("", "POST", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) CreateResourceRecord(resourceName string, resource *ResourceRecord) error { + body, err := json.Marshal(resource) + 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("/resources/%s/records", resourceName) + r, err := c.request("", "POST", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) UpdateResourceRecord(resourceName string, resource *ResourceRecord) error { + body, err := json.Marshal(resource) + 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("/resources/%s/records/%s", resourceName, resource.Id) + r, err := c.request("", "PUT", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) UpdateResourceRecordString(resourceName, resourceStr string) error { + body := []byte(resourceStr) + + h := map[string]string{ + "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), + "Content-Type": "application/json", + } + + uri := fmt.Sprintf("/resources/%s/records", resourceName) + r, err := c.request("", "PUT", uri, h, body) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) DeleteResourceRecord(resourceName, recordId string) error { + + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + } + + uri := fmt.Sprintf("/resources/%s/records?ids=%s", resourceName, recordId) + r, err := c.request("", "DELETE", uri, h, nil) + if err != nil { + return err + } + r.Body.Close() + return nil +} + +func (c *Client) GetResourceRecord(resourceName, recordId string) (resource *ResourceRecord, err error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + } + uri := fmt.Sprintf("/resources/%s/records/%s", resourceName, recordId) + r, err := c.request("", "GET", uri, h, nil) + if err != nil { + return nil, err + } + defer r.Body.Close() + buf, _ := ioutil.ReadAll(r.Body) + resource = &ResourceRecord{} + if err = json.Unmarshal(buf, resource); err != nil { + err = NewClientError(err) + } + return resource, err +} + +func (c *Client) GetResourceRecordString(resourceName, name string) (resource string, err error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + } + uri := fmt.Sprintf("/resources/%s/records", resourceName) + r, err := c.request("", "GET", uri, h, nil) + if err != nil { + return "", err + } + defer r.Body.Close() + buf, err := ioutil.ReadAll(r.Body) + return string(buf), err +} + +func (c *Client) ListResourceRecord(resourceName string, offset, size int) (resourceList []*ResourceRecord, count, total int, err error) { + h := map[string]string{ + "x-log-bodyrawsize": "0", + "Content-Type": "application/json", + "offset": strconv.Itoa(offset), + "size": strconv.Itoa(size), + } + uri := fmt.Sprintf("/resources/%s/records", resourceName) + r, err := c.request("", "GET", uri, h, nil) + if err != nil { + return nil, 0, 0, err + } + defer r.Body.Close() + type ListResourceRecordResponse struct { + ResourceRecordList []*ResourceRecord `json:"Items"` + Total int `json:"total"` + Count int `json:"count"` + } + + buf, _ := ioutil.ReadAll(r.Body) + resources := &ListResourceRecordResponse{} + if err = json.Unmarshal(buf, resources); err != nil { + err = NewClientError(err) + } + return resources.ResourceRecordList, resources.Count, resources.Total, err +} diff --git a/client_resource_record_test.go b/client_resource_record_test.go new file mode 100644 index 00000000..643d45cb --- /dev/null +++ b/client_resource_record_test.go @@ -0,0 +1,141 @@ +package sls + +import ( + "os" + "testing" + + "github.com/stretchr/testify/suite" +) + +func TestResourceRecord(t *testing.T) { + suite.Run(t, new(ResourceRecordTestSuite)) +} + +type ResourceRecordTestSuite struct { + suite.Suite + endpoint string + projectName string + logstoreName string + accessKeyID string + accessKeySecret string + client Client + recordId string + tagName string + resourceName string +} + +func (s *ResourceRecordTestSuite) SetupSuite() { + s.endpoint = os.Getenv("LOG_TEST_ENDPOINT") + s.projectName = os.Getenv("LOG_TEST_PROJECT") + s.logstoreName = os.Getenv("LOG_TEST_LOGSTORE") + s.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID") + s.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET") + s.client.AccessKeyID = s.accessKeyID + s.client.AccessKeySecret = s.accessKeySecret + s.client.Endpoint = s.endpoint + s.resourceName = "user.test_resource_1" + s.recordId = "test_record_1" + s.tagName = "test record" + + _ = s.client.DeleteResource(s.resourceName) + _ = s.createResource() +} + +func (s *ResourceRecordTestSuite) createResource() error { + rs := &ResourceSchema{ + Schema: []*ResourceSchemaItem{ + &ResourceSchemaItem{ + Column: "col1", + Desc: "col1 desc", + ExtInfo: map[string]string{}, + Required: true, + Type: "string", + }, + &ResourceSchemaItem{ + Column: "col2", + Desc: "col2 desc", + ExtInfo: "optional", + Required: true, + Type: "string", + }, + }, + } + customResource := new(Resource) + customResource.Type = ResourceTypeUserDefine + customResource.Name = s.resourceName + customResource.Schema = rs.ToString() + customResource.Description = "user test resource 1 descc" + return s.client.CreateResource(customResource) +} + +func (s *ResourceRecordTestSuite) TearDownSuite() { + err := s.client.DeleteResource(s.resourceName) + s.Require().Nil(err) +} + +func (s *ResourceRecordTestSuite) createResourceRecord() error { + customResourceRecord := new(ResourceRecord) + customResourceRecord.Id = s.recordId + customResourceRecord.Tag = s.tagName + customResourceRecord.Value = `{"col1": "sls", "col2": "tag"}` + return s.client.CreateResourceRecord(s.resourceName, customResourceRecord) +} + +func (s *ResourceRecordTestSuite) TestClient_CreateResourceRecord() { + err := s.createResourceRecord() + s.Require().Nil(err) + err = s.client.DeleteResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) +} + +func (s *ResourceRecordTestSuite) TestClient_UpdateResourceRecord() { + err := s.createResourceRecord() + s.Require().Nil(err) + resourceRecord, err := s.client.GetResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) + resourceRecord.Value = `{"col1": "new sls", "col2": "new tag"}` + err = s.client.UpdateResourceRecord(s.resourceName, resourceRecord) + s.Require().Nil(err) + resourceRecord, err = s.client.GetResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) + s.Require().Equal(`{"col1": "new sls", "col2": "new tag"}`, resourceRecord.Value, "update resourceRecord failed") + err = s.client.DeleteResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) +} + +func (s *ResourceRecordTestSuite) TestClient_DeleteResourceRecord() { + err := s.createResourceRecord() + s.Require().Nil(err) + _, err = s.client.GetResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) + err = s.client.DeleteResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) + _, err = s.client.GetResourceRecord(s.resourceName, s.recordId) + s.Require().NotNil(err) +} + +func (s *ResourceRecordTestSuite) TestClient_GetResourceRecord() { + err := s.createResourceRecord() + s.Require().Nil(err) + getResourceRecord, err := s.client.GetResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) + s.Require().Equal(getResourceRecord.Id, s.recordId) + + err = s.client.DeleteResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) +} + +func (s *ResourceRecordTestSuite) TestClient_ListResourceRecord() { + err := s.createResourceRecord() + s.Require().Nil(err) + resourceRecords, total, count, err := s.client.ListResourceRecord(s.resourceName, 0, 100) + s.Require().Nil(err) + if total != 1 || count != 1 { + s.Require().Fail("list resourceRecord failed") + } + s.Require().Equal(1, len(resourceRecords), "there should be only one resourceRecord") + resourceRecord := resourceRecords[0] + s.Require().Equal(s.recordId, resourceRecord.Id, "list resourceRecord failed") + err = s.client.DeleteResourceRecord(s.resourceName, s.recordId) + s.Require().Nil(err) +} diff --git a/client_resource_test.go b/client_resource_test.go new file mode 100644 index 00000000..1e57ceb4 --- /dev/null +++ b/client_resource_test.go @@ -0,0 +1,137 @@ +package sls + +import ( + "os" + "testing" + + "github.com/stretchr/testify/suite" +) + +func TestResource(t *testing.T) { + suite.Run(t, new(ResourceTestSuite)) +} + +type ResourceTestSuite struct { + suite.Suite + endpoint string + projectName string + logstoreName string + accessKeyID string + accessKeySecret string + client Client + resourceName string +} + +func (s *ResourceTestSuite) SetupSuite() { + s.endpoint = os.Getenv("LOG_TEST_ENDPOINT") + s.projectName = os.Getenv("LOG_TEST_PROJECT") + s.logstoreName = os.Getenv("LOG_TEST_LOGSTORE") + s.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID") + s.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET") + s.client.AccessKeyID = s.accessKeyID + s.client.AccessKeySecret = s.accessKeySecret + s.client.Endpoint = s.endpoint + s.resourceName = "user.test_resource_1" +} + +func (s *ResourceTestSuite) TearDownSuite() { +} + +func (s *ResourceTestSuite) createResource() error { + rs := &ResourceSchema{ + Schema: []*ResourceSchemaItem{ + &ResourceSchemaItem{ + Column: "col1", + Desc: "col1 desc", + ExtInfo: map[string]string{}, + Required: true, + Type: "string", + }, + &ResourceSchemaItem{ + Column: "col2", + Desc: "col2 desc", + ExtInfo: "optional", + Required: true, + Type: "string", + }, + }, + } + customResource := new(Resource) + customResource.Type = ResourceTypeUserDefine + customResource.Name = s.resourceName + customResource.Schema = rs.ToString() + customResource.Description = "user test resource 1 descc" + return s.client.CreateResource(customResource) +} + +func (s *ResourceTestSuite) TestClient_CreateResource() { + err := s.createResource() + s.Require().Nil(err) + err = s.client.DeleteResource(s.resourceName) + s.Require().Nil(err) +} + +func (s *ResourceTestSuite) TestClient_UpdateResource() { + err := s.createResource() + s.Require().Nil(err) + resource, err := s.client.GetResource(s.resourceName) + s.Require().Nil(err) + rs := new(ResourceSchema) + err = rs.FromJsonString(resource.Schema) + s.Require().Nil(err) + rs.Schema[0].Desc = "new desc" + resource.Schema = rs.ToString() + err = s.client.UpdateResource(resource) + s.Require().Nil(err) + resource, err = s.client.GetResource(s.resourceName) + s.Require().Nil(err) + nrs := new(ResourceSchema) + err = nrs.FromJsonString(resource.Schema) + s.Require().Nil(err) + s.Require().Equal("new desc", rs.Schema[0].Desc, "update resource failed") + err = s.client.DeleteResource(s.resourceName) + s.Require().Nil(err) +} + +func (s *ResourceTestSuite) TestClient_DeleteResource() { + err := s.createResource() + s.Require().Nil(err) + _, err = s.client.GetResource(s.resourceName) + s.Require().Nil(err) + err = s.client.DeleteResource(s.resourceName) + s.Require().Nil(err) + _, err = s.client.GetResource(s.resourceName) + s.Require().NotNil(err) +} + +func (s *ResourceTestSuite) TestClient_GetResource() { + err := s.createResource() + s.Require().Nil(err) + getResource, err := s.client.GetResource(s.resourceName) + s.Require().Nil(err) + s.Require().Equal(getResource.Name, s.resourceName) + rs := new(ResourceSchema) + err = rs.FromJsonString(getResource.Schema) + s.Require().Nil(err) + + s.Require().Equal(len(rs.Schema), 2) + s.Require().Equal(rs.Schema[0].Desc, "col1 desc") + + err = s.client.DeleteResource(s.resourceName) + s.Require().Nil(err) +} + +func (s *ResourceTestSuite) TestClient_ListResource() { + err := s.createResource() + s.Require().Nil(err) + resources, total, count, err := s.client.ListResource(ResourceTypeUserDefine, s.resourceName, 0, 100) + s.Require().Nil(err) + if total != 1 || count != 1 { + s.Require().Fail("list resource failed") + } + s.Require().Equal(1, len(resources), "there should be only one resource") + resource := resources[0] + s.Require().Equal(s.resourceName, resource.Name, "list resource failed") + err = s.client.DeleteResource(s.resourceName) + s.Require().Nil(err) +} From 57920923049926bcb0ad1cc5857290132385dd35 Mon Sep 17 00:00:00 2001 From: "weilong.pwl" Date: Mon, 30 Aug 2021 16:27:20 +0800 Subject: [PATCH 2/2] add method to client interface --- client_interface.go | 20 +++++ client_resource.go | 2 +- client_resource_record.go | 34 ++++---- token_auto_update_client.go | 162 ++++++++++++++++++++++++++++++++++++ 4 files changed, 200 insertions(+), 18 deletions(-) diff --git a/client_interface.go b/client_interface.go index eb46211c..9f6a710b 100644 --- a/client_interface.go +++ b/client_interface.go @@ -266,4 +266,24 @@ type ClientInterface interface { GetScheduledSQLJobInstance(projectName, jobName, instanceId string, result bool) (instance *ScheduledSQLJobInstance, err error) ModifyScheduledSQLJobInstanceState(projectName, jobName, instanceId string, state ScheduledSQLState) error ListScheduledSQLJobInstances(projectName, jobName string, status *InstanceStatus) (instances []*ScheduledSQLJobInstance, total, count int64, err error) + + // #################### Resource Operations ##################### + ListResource(resourceType string, resourceName string, offset, size int) (resourceList []*Resource, count, total int, err error) + GetResource(name string) (resource *Resource, err error) + GetResourceString(name string) (resource string, err error) + DeleteResource(name string) error + UpdateResource(resource *Resource) error + UpdateResourceString(resourceName, resourceStr string) error + CreateResource(resource *Resource) error + CreateResourceString(resourceStr string) error + + // #################### Resource Record Operations ##################### + ListResourceRecord(resourceName string, offset, size int) (recordList []*ResourceRecord, count, total int, err error) + GetResourceRecord(resourceName, recordId string) (record *ResourceRecord, err error) + GetResourceRecordString(resourceName, name string) (record string, err error) + DeleteResourceRecord(resourceName, recordId string) error + UpdateResourceRecord(resourceName string, record *ResourceRecord) error + UpdateResourceRecordString(resourceName, recordStr string) error + CreateResourceRecord(resourceName string, record *ResourceRecord) error + CreateResourceRecordString(resourceName, recordStr string) error } diff --git a/client_resource.go b/client_resource.go index 400c8190..ed5c1ce1 100644 --- a/client_resource.go +++ b/client_resource.go @@ -177,7 +177,7 @@ func (c *Client) ListResource(resourceType string, resourceName string, offset, } defer r.Body.Close() type ListResourceResponse struct { - ResourceList []*Resource `json:"Items"` + ResourceList []*Resource `json:"items"` Total int `json:"total"` Count int `json:"count"` } diff --git a/client_resource_record.go b/client_resource_record.go index 42c1a532..8239baed 100644 --- a/client_resource_record.go +++ b/client_resource_record.go @@ -15,8 +15,8 @@ type ResourceRecord struct { LastModifyTime int64 `json:"lastModifyTime"` } -func (c *Client) CreateResourceRecordString(resourceName, resourceStr string) error { - body := []byte(resourceStr) +func (c *Client) CreateResourceRecordString(resourceName, recordStr string) error { + body := []byte(recordStr) h := map[string]string{ "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), @@ -32,8 +32,8 @@ func (c *Client) CreateResourceRecordString(resourceName, resourceStr string) er return nil } -func (c *Client) CreateResourceRecord(resourceName string, resource *ResourceRecord) error { - body, err := json.Marshal(resource) +func (c *Client) CreateResourceRecord(resourceName string, record *ResourceRecord) error { + body, err := json.Marshal(record) if err != nil { return NewClientError(err) } @@ -51,8 +51,8 @@ func (c *Client) CreateResourceRecord(resourceName string, resource *ResourceRec return nil } -func (c *Client) UpdateResourceRecord(resourceName string, resource *ResourceRecord) error { - body, err := json.Marshal(resource) +func (c *Client) UpdateResourceRecord(resourceName string, record *ResourceRecord) error { + body, err := json.Marshal(record) if err != nil { return NewClientError(err) } @@ -62,7 +62,7 @@ func (c *Client) UpdateResourceRecord(resourceName string, resource *ResourceRec "Content-Type": "application/json", } - uri := fmt.Sprintf("/resources/%s/records/%s", resourceName, resource.Id) + uri := fmt.Sprintf("/resources/%s/records/%s", resourceName, record.Id) r, err := c.request("", "PUT", uri, h, body) if err != nil { return err @@ -71,8 +71,8 @@ func (c *Client) UpdateResourceRecord(resourceName string, resource *ResourceRec return nil } -func (c *Client) UpdateResourceRecordString(resourceName, resourceStr string) error { - body := []byte(resourceStr) +func (c *Client) UpdateResourceRecordString(resourceName, recordStr string) error { + body := []byte(recordStr) h := map[string]string{ "x-log-bodyrawsize": fmt.Sprintf("%v", len(body)), @@ -104,7 +104,7 @@ func (c *Client) DeleteResourceRecord(resourceName, recordId string) error { return nil } -func (c *Client) GetResourceRecord(resourceName, recordId string) (resource *ResourceRecord, err error) { +func (c *Client) GetResourceRecord(resourceName, recordId string) (record *ResourceRecord, err error) { h := map[string]string{ "x-log-bodyrawsize": "0", "Content-Type": "application/json", @@ -116,19 +116,19 @@ func (c *Client) GetResourceRecord(resourceName, recordId string) (resource *Res } defer r.Body.Close() buf, _ := ioutil.ReadAll(r.Body) - resource = &ResourceRecord{} - if err = json.Unmarshal(buf, resource); err != nil { + record = &ResourceRecord{} + if err = json.Unmarshal(buf, record); err != nil { err = NewClientError(err) } - return resource, err + return record, err } -func (c *Client) GetResourceRecordString(resourceName, name string) (resource string, err error) { +func (c *Client) GetResourceRecordString(resourceName, recordId string) (recordStr string, err error) { h := map[string]string{ "x-log-bodyrawsize": "0", "Content-Type": "application/json", } - uri := fmt.Sprintf("/resources/%s/records", resourceName) + uri := fmt.Sprintf("/resources/%s/records/%s", resourceName, recordId) r, err := c.request("", "GET", uri, h, nil) if err != nil { return "", err @@ -138,7 +138,7 @@ func (c *Client) GetResourceRecordString(resourceName, name string) (resource st return string(buf), err } -func (c *Client) ListResourceRecord(resourceName string, offset, size int) (resourceList []*ResourceRecord, count, total int, err error) { +func (c *Client) ListResourceRecord(resourceName string, offset, size int) (recordList []*ResourceRecord, count, total int, err error) { h := map[string]string{ "x-log-bodyrawsize": "0", "Content-Type": "application/json", @@ -152,7 +152,7 @@ func (c *Client) ListResourceRecord(resourceName string, offset, size int) (reso } defer r.Body.Close() type ListResourceRecordResponse struct { - ResourceRecordList []*ResourceRecord `json:"Items"` + ResourceRecordList []*ResourceRecord `json:"items"` Total int `json:"total"` Count int `json:"count"` } diff --git a/token_auto_update_client.go b/token_auto_update_client.go index c65e569e..9dc5eaad 100644 --- a/token_auto_update_client.go +++ b/token_auto_update_client.go @@ -1260,3 +1260,165 @@ func (c *TokenAutoUpdateClient) ListScheduledSQLJobInstances(projectName, jobNam } return instances, total, count, err } + +// ####################### Resource API ###################### +func (c *TokenAutoUpdateClient) ListResource(resourceType string, resourceName string, offset, size int) (resourceList []*Resource, count, total int, err error) { + for i := 0; i < c.maxTryTimes; i++ { + resourceList, total, count, err = c.logClient.ListResource(resourceType, resourceName, offset, size) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) GetResource(name string) (resource *Resource, err error) { + for i := 0; i < c.maxTryTimes; i++ { + resource, err = c.logClient.GetResource(name) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) GetResourceString(name string) (resource string, err error) { + for i := 0; i < c.maxTryTimes; i++ { + resource, err = c.logClient.GetResourceString(name) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) DeleteResource(name string) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.DeleteResource(name) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) UpdateResource(resource *Resource) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.UpdateResource(resource) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) UpdateResourceString(resourceName, resourceStr string) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.UpdateResourceString(resourceName, resourceStr) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) CreateResource(resource *Resource) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.CreateResource(resource) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) CreateResourceString(resourceStr string) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.CreateResourceString(resourceStr) + if !c.processError(err) { + return + } + } + return +} + +// ####################### Resource Record API ###################### +func (c *TokenAutoUpdateClient) ListResourceRecord(resourceName string, offset, size int) (recordList []*ResourceRecord, count, total int, err error) { + for i := 0; i < c.maxTryTimes; i++ { + recordList, total, count, err = c.logClient.ListResourceRecord(resourceName, offset, size) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) GetResourceRecord(resourceName, recordId string) (record *ResourceRecord, err error) { + for i := 0; i < c.maxTryTimes; i++ { + record, err = c.logClient.GetResourceRecord(resourceName, recordId) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) GetResourceRecordString(resourceName, name string) (record string, err error) { + for i := 0; i < c.maxTryTimes; i++ { + record, err = c.logClient.GetResourceRecordString(resourceName, name) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) DeleteResourceRecord(resourceName, recordId string) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.DeleteResourceRecord(resourceName, recordId) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) UpdateResourceRecord(resourceName string, record *ResourceRecord) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.UpdateResourceRecord(resourceName, record) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) UpdateResourceRecordString(resourceName, recordStr string) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.UpdateResourceString(resourceName, recordStr) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) CreateResourceRecord(resourceName string, record *ResourceRecord) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.CreateResourceRecord(resourceName, record) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) CreateResourceRecordString(resourceName, recordStr string) (err error) { + for i := 0; i < c.maxTryTimes; i++ { + err = c.logClient.CreateResourceRecordString(resourceName, recordStr) + if !c.processError(err) { + return + } + } + return +}