Skip to content

Commit

Permalink
add method to client interface
Browse files Browse the repository at this point in the history
  • Loading branch information
weilong.pwl committed Aug 30, 2021
1 parent bfb55c9 commit 5792092
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 18 deletions.
20 changes: 20 additions & 0 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion client_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
34 changes: 17 additions & 17 deletions client_resource_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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)),
Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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"`
}
Expand Down
162 changes: 162 additions & 0 deletions token_auto_update_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5792092

Please sign in to comment.