forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathclient_resource_record.go
163 lines (143 loc) · 4.2 KB
/
client_resource_record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
)
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, recordStr string) error {
body := []byte(recordStr)
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, record *ResourceRecord) error {
body, err := json.Marshal(record)
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, record *ResourceRecord) error {
body, err := json.Marshal(record)
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, record.Id)
r, err := c.request("", "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) UpdateResourceRecordString(resourceName, recordStr string) error {
body := []byte(recordStr)
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) (record *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)
record = &ResourceRecord{}
if err = json.Unmarshal(buf, record); err != nil {
err = NewClientError(err)
}
return record, err
}
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/%s", resourceName, recordId)
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) (recordList []*ResourceRecord, count, total int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/resources/%s/records?offset=%d&size=%d", resourceName, offset, size)
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
}