forked from libdns/tencentcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
132 lines (116 loc) · 3.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
130
131
132
package tencentcloud
import (
"context"
"strconv"
"strings"
"sync"
"time"
"github.com/libdns/libdns"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
tp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/libdns/tencentcloud/dnspod"
)
// getClient gets the client for Tencent Cloud DNS
func (p *Provider) getClient() (*dnspod.Client, error) {
client := sync.OnceValues(func() (*dnspod.Client, error) {
credential := common.NewCredential(
p.SecretId,
p.SecretKey,
)
cpf := tp.NewClientProfile()
cpf.HttpProfile.Endpoint = "dnspod.tencentcloudapi.com"
client, err := dnspod.NewClient(credential, "", cpf)
if err != nil {
return nil, err
}
return client, nil
})
return client()
}
// describeRecordList describes the records for a zone
func (p *Provider) describeRecordList(ctx context.Context, zone string) ([]libdns.Record, error) {
client, err := p.getClient()
if err != nil {
return nil, err
}
var list []libdns.Record
request := dnspod.NewDescribeRecordListRequest()
request.Domain = common.StringPtr(strings.Trim(zone, "."))
request.Offset = common.Uint64Ptr(0)
request.Limit = common.Uint64Ptr(3000)
totalCount := uint64(100)
for *request.Offset < totalCount {
response, err := client.DescribeRecordListWithContext(ctx, request)
if err != nil {
return nil, err
}
if response.Response.RecordList != nil && len(response.Response.RecordList) > 0 {
for _, record := range response.Response.RecordList {
list = append(list, libdns.Record{
ID: strconv.Itoa(int(*record.RecordId)),
Type: *record.Type,
Name: *record.Name,
Value: *record.Value,
TTL: time.Duration(*record.TTL) * time.Second,
})
}
}
totalCount = *response.Response.RecordCountInfo.TotalCount
request.Offset = common.Uint64Ptr(*request.Offset + uint64(len(response.Response.RecordList)))
}
return list, err
}
// createRecord creates a record for a zone
func (p *Provider) createRecord(ctx context.Context, zone string, record libdns.Record) (string, error) {
client, err := p.getClient()
if err != nil {
return "", err
}
request := dnspod.NewCreateRecordRequest()
request.Domain = common.StringPtr(strings.Trim(zone, "."))
request.SubDomain = common.StringPtr(record.Name)
request.RecordType = common.StringPtr(record.Type)
request.RecordLine = common.StringPtr("默认")
request.Value = common.StringPtr(record.Value)
response, err := client.CreateRecordWithContext(ctx, request)
if err != nil {
return "", err
}
return strconv.Itoa(int(*response.Response.RecordId)), nil
}
// modifyRecord modifies a record for a zone
func (p *Provider) modifyRecord(ctx context.Context, zone string, record libdns.Record) error {
client, err := p.getClient()
if err != nil {
return err
}
recordId, _ := strconv.Atoi(record.ID)
request := dnspod.NewModifyRecordRequest()
request.Domain = common.StringPtr(strings.Trim(zone, "."))
request.SubDomain = common.StringPtr(record.Name)
request.RecordType = common.StringPtr(record.Type)
request.RecordLine = common.StringPtr("默认")
request.Value = common.StringPtr(record.Value)
request.RecordId = common.Uint64Ptr(uint64(recordId))
_, err = client.ModifyRecordWithContext(ctx, request)
if err != nil {
return err
}
return nil
}
// deleteRecord deletes a record for a zone
func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns.Record) error {
client, err := p.getClient()
if err != nil {
return err
}
recordId, _ := strconv.Atoi(record.ID)
request := dnspod.NewDeleteRecordRequest()
request.Domain = common.StringPtr(strings.Trim(zone, "."))
request.RecordId = common.Uint64Ptr(uint64(recordId))
_, err = client.DeleteRecordWithContext(ctx, request)
if err != nil {
return err
}
return nil
}