From 57920923049926bcb0ad1cc5857290132385dd35 Mon Sep 17 00:00:00 2001 From: "weilong.pwl" Date: Mon, 30 Aug 2021 16:27:20 +0800 Subject: [PATCH] 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 +}