-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
129 lines (108 loc) · 2.75 KB
/
client.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
package dns
import (
"context"
)
var _ = New
// Client 客户端
type Client struct {
aliyun executor
tencentCloud executor
options *options
}
// New 创建客户端
func New(opts ...option) (client *Client) {
client = new(Client)
client.options = defaultOptions()
for _, opt := range opts {
opt.apply(client.options)
}
client.aliyun = newAliyun()
client.tencentCloud = newTencentCloud()
return
}
func (c *Client) Add(ctx context.Context, domain string, subdomain string, value string, opts ...option) (err error) {
_options := c.options.clone()
for _, opt := range opts {
opt.apply(_options)
}
var _executor executor
switch _options.provider {
case providerAliyun:
_executor = c.aliyun
case providerTencentCloud:
_executor = c.tencentCloud
}
err = _executor.add(ctx, domain, subdomain, value, _options)
return
}
func (c *Client) Resolve(
ctx context.Context,
domain string, subdomain string, value string,
opts ...option,
) (original string, do bool, err error) {
_options := c.options.clone()
for _, opt := range opts {
opt.apply(_options)
}
if record, getErr := c.Get(ctx, domain, subdomain, opts...); nil != getErr {
err = getErr
} else if nil != record && string(_options.typ) == record.Type {
original = record.Value
if value != record.Value {
do = true
err = c.Update(ctx, record, value, opts...)
}
} else {
do = true
if nil != record { // 先删除记录,不然会报重复错误
_ = c.Delete(ctx, record, opts...)
}
err = c.Add(ctx, domain, subdomain, value, opts...)
}
return
}
func (c *Client) Get(ctx context.Context, domain string, subdomain string, opts ...option) (record *Record, err error) {
_options := c.options.clone()
for _, opt := range opts {
opt.apply(_options)
}
var _executor executor
switch _options.provider {
case providerAliyun:
_executor = c.aliyun
case providerTencentCloud:
_executor = c.tencentCloud
}
record, err = _executor.get(ctx, domain, subdomain, _options)
return
}
func (c *Client) Update(ctx context.Context, record *Record, value string, opts ...option) (err error) {
_options := c.options.clone()
for _, opt := range opts {
opt.apply(_options)
}
var _executor executor
switch _options.provider {
case providerAliyun:
_executor = c.aliyun
case providerTencentCloud:
_executor = c.tencentCloud
}
err = _executor.update(ctx, record, value, _options)
return
}
func (c *Client) Delete(ctx context.Context, record *Record, opts ...option) (err error) {
_options := c.options.clone()
for _, opt := range opts {
opt.apply(_options)
}
var _executor executor
switch _options.provider {
case providerAliyun:
_executor = c.aliyun
case providerTencentCloud:
_executor = c.tencentCloud
}
err = _executor.delete(ctx, record, _options)
return
}