-
Notifications
You must be signed in to change notification settings - Fork 4
/
record.go
192 lines (164 loc) · 3.92 KB
/
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package cns
import (
"net/url"
"strconv"
)
//“域名”“解析记录”的数据结构
type Record struct {
Id int
Ttl int
Value string
Enabled int
Status string
UpdatedOn string `json:"updated_on"`
QProjectId int `json:"q_project_id"`
Name string
Line string
LineId string `json:"line_id"`
Type string
Remark string
Mx int
Hold string
}
//“域名”“解析记录”列表中的“域名”数据结构,和“域名列表”中的略有不同
type DomainInRecordList struct {
Domain
Ttl int
Id int `json:",string"`
DnspodNs []string `json:"dnspod_ns"`
}
//获取指定“域名”的“解析记录”列表
func (cli *Client) RecordList(domain string) ([]Record, error) {
var respInfo struct {
BaseResponse
Data struct {
Domain DomainInRecordList
Records []Record
Info struct {
SubDomains int `json:"sub_domains,string"`
RecordTotal int `json:"record_total,string"`
}
}
}
param := url.Values{
"domain": {domain},
}
err := cli.requestGET("RecordList", param, &respInfo)
if err != nil {
return nil, err
}
return respInfo.Data.Records, nil
}
//添加指定“域名”的“解析记录”
func (cli *Client) RecordCreate(domain string, record Record) (int, error) {
if record.Line == "" {
record.Line = "默认"
}
//必选参数
param := url.Values{
"domain": {domain},
"subDomain": {record.Name},
"recordType": {record.Type},
"recordLine": {record.Line},
"value": {record.Value},
}
//可选TTL参数,缺省为600
if record.Ttl > 0 {
param.Set("ttl", strconv.Itoa(record.Ttl))
}
//MX记录必须的额外参数
if record.Type == "MX" {
param.Set("mx", strconv.Itoa(record.Mx))
}
var respInfo struct {
BaseResponse
Data struct {
Record struct {
Id int `json:",string"`
Name string
Status string
Weight interface{}
}
}
}
err := cli.requestGET("RecordCreate", param, &respInfo)
if err != nil {
return 0, err
}
return respInfo.Data.Record.Id, nil
}
//设置指定“域名”的“解析记录”状态
func (cli *Client) RecordStatus(domain string, recordId int, enable bool) error {
param := url.Values{
"domain": {domain},
"recordId": {strconv.Itoa(recordId)},
}
if enable {
param.Set("status", "enable")
} else {
param.Set("status", "disable")
}
var respInfo BaseResponse
err := cli.requestGET("RecordStatus", param, &respInfo)
if err != nil {
return err
}
return nil
}
//添加指定“域名”的“解析记录”
func (cli *Client) RecordModify(domain string, record Record) error {
if record.Line == "" {
record.Line = "默认"
}
//必选参数
param := url.Values{
"domain": {domain},
"recordId": {strconv.Itoa(record.Id)},
"subDomain": {record.Name},
"recordType": {record.Type},
"recordLine": {record.Line},
"value": {record.Value},
}
//可选TTL参数,缺省为600
if record.Ttl > 0 {
param.Set("ttl", strconv.Itoa(record.Ttl))
}
//MX记录必须的额外参数
if record.Type == "MX" {
param.Set("mx", strconv.Itoa(record.Mx))
}
var respInfo struct {
BaseResponse
//FIXME: 腾讯文档上描述的是如下的返回结构
//Data struct {
// Record struct {
// Id int
// Name string
// Value string
// Status string
// Weight interface{}
// }
//}
//事实上返回的可能是个空白的数组[],不知道腾讯想要搞什么
//这里就只有用interface{}了
Data interface{}
}
err := cli.requestGET("RecordModify", param, &respInfo)
if err != nil {
return err
}
return nil
}
//删除指定“域名”的“解析记录”
func (cli *Client) RecordDelete(domain string, recordId int) error {
param := url.Values{
"domain": {domain},
"recordId": {strconv.Itoa(recordId)},
}
var respInfo BaseResponse
err := cli.requestGET("RecordDelete", param, &respInfo)
if err != nil {
return err
}
return nil
}