@@ -2,125 +2,126 @@ package tencentcloud
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"strconv"
7
6
"strings"
7
+ "sync"
8
8
"time"
9
9
10
10
"github.com/libdns/libdns"
11
11
12
- tc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
13
- th "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http"
12
+ "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
14
13
tp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
14
+ dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
15
15
)
16
16
17
- func (p * Provider ) describeRecordList (ctx context.Context , zone string ) ([]libdns.Record , error ) {
18
-
19
- list := []libdns.Record {}
20
-
21
- payload := map [string ]any {
22
- "Domain" : strings .Trim (zone , "." ),
23
- }
17
+ // getClient gets the client for Tencent Cloud DNS
18
+ func (p * Provider ) getClient () (* dnspod.Client , error ) {
19
+ client := sync .OnceValues (func () (* dnspod.Client , error ) {
20
+ credential := common .NewCredential (
21
+ p .SecretId ,
22
+ p .SecretKey ,
23
+ )
24
+ cpf := tp .NewClientProfile ()
25
+ cpf .HttpProfile .Endpoint = "dnspod.tencentcloudapi.com"
26
+ client , err := dnspod .NewClient (credential , "" , cpf )
27
+ if err != nil {
28
+ return nil , err
29
+ }
30
+ return client , nil
31
+ })
32
+ return client ()
33
+ }
24
34
25
- resp , err := p .doRequest ("DescribeRecordList" , payload )
35
+ // describeRecordList describes the records for a zone
36
+ func (p * Provider ) describeRecordList (ctx context.Context , zone string ) ([]libdns.Record , error ) {
37
+ client , err := p .getClient ()
26
38
if err != nil {
27
- return list , err
28
- }
29
-
30
- data := DescribeRecordListResponse {}
31
- if err = json .Unmarshal (resp , & data ); err != nil {
32
- return list , err
39
+ return nil , err
33
40
}
34
-
35
- for _ , record := range data .Response .RecordList {
36
- list = append (list , libdns.Record {
37
- ID : strconv .Itoa (record .RecordId ),
38
- Type : record .Type ,
39
- Name : record .Name ,
40
- Value : record .Value ,
41
- TTL : time .Duration (record .TTL ) * time .Second ,
42
- })
41
+ list := []libdns.Record {}
42
+ request := dnspod .NewDescribeRecordListRequest ()
43
+ request .Domain = common .StringPtr (strings .Trim (zone , "." ))
44
+ request .Offset = common .Uint64Ptr (0 )
45
+ request .Limit = common .Uint64Ptr (3000 )
46
+
47
+ totalCount := uint64 (100 )
48
+ for * request .Offset < totalCount {
49
+ response , err := client .DescribeRecordList (request )
50
+ if err != nil {
51
+ return nil , err
52
+ }
53
+ if response .Response .RecordList != nil && len (response .Response .RecordList ) > 0 {
54
+ for _ , record := range response .Response .RecordList {
55
+ list = append (list , libdns.Record {
56
+ ID : strconv .Itoa (int (* record .RecordId )),
57
+ Type : * record .Type ,
58
+ Name : * record .Name ,
59
+ Value : * record .Value ,
60
+ TTL : time .Duration (* record .TTL ) * time .Second ,
61
+ })
62
+ }
63
+ }
64
+ totalCount = * response .Response .RecordCountInfo .TotalCount
65
+ request .Offset = common .Uint64Ptr (* request .Offset + uint64 (len (response .Response .RecordList )))
43
66
}
44
-
45
67
return list , err
46
-
47
68
}
48
69
70
+ // createRecord creates a record for a zone
49
71
func (p * Provider ) createRecord (ctx context.Context , zone string , record libdns.Record ) (string , error ) {
50
-
51
- payload := map [string ]any {
52
- "Domain" : strings .Trim (zone , "." ),
53
- "SubDomain" : record .Name ,
54
- "RecordType" : record .Type ,
55
- "RecordLine" : "默认" ,
56
- "Value" : record .Value ,
57
- }
58
-
59
- resp , err := p .doRequest ("CreateRecord" , payload )
72
+ client , err := p .getClient ()
60
73
if err != nil {
61
74
return "" , err
62
75
}
63
-
64
- data := CreateRecordResponse {}
65
- if err = json .Unmarshal (resp , & data ); err != nil {
76
+ request := dnspod .NewCreateRecordRequest ()
77
+ request .Domain = common .StringPtr (strings .Trim (zone , "." ))
78
+ request .SubDomain = common .StringPtr (record .Name )
79
+ request .RecordType = common .StringPtr (record .Type )
80
+ request .RecordLine = common .StringPtr ("默认" )
81
+ request .Value = common .StringPtr (record .Value )
82
+ response , err := client .CreateRecord (request )
83
+ if err != nil {
66
84
return "" , err
67
85
}
68
-
69
- return strconv .Itoa (data .Response .RecordId ), nil
70
-
86
+ return strconv .Itoa (int (* response .Response .RecordId )), nil
71
87
}
72
88
89
+ // modifyRecord modifies a record for a zone
73
90
func (p * Provider ) modifyRecord (ctx context.Context , zone string , record libdns.Record ) error {
74
-
91
+ client , err := p .getClient ()
92
+ if err != nil {
93
+ return err
94
+ }
75
95
recordId , _ := strconv .Atoi (record .ID )
76
-
77
- payload := map [string ]any {
78
- "Domain" : strings .Trim (zone , "." ),
79
- "SubDomain" : record .Name ,
80
- "RecordType" : record .Type ,
81
- "RecordLine" : "默认" ,
82
- "Value" : record .Value ,
83
- "RecordId" : recordId ,
96
+ request := dnspod .NewModifyRecordRequest ()
97
+ request .Domain = common .StringPtr (strings .Trim (zone , "." ))
98
+ request .SubDomain = common .StringPtr (record .Name )
99
+ request .RecordType = common .StringPtr (record .Type )
100
+ request .RecordLine = common .StringPtr ("默认" )
101
+ request .Value = common .StringPtr (record .Value )
102
+ request .RecordId = common .Uint64Ptr (uint64 (recordId ))
103
+
104
+ _ , err = client .ModifyRecord (request )
105
+ if err != nil {
106
+ return err
84
107
}
85
-
86
- _ , err := p .doRequest ("ModifyRecord" , payload )
87
-
88
- return err
89
-
108
+ return nil
90
109
}
91
110
111
+ // deleteRecord deletes a record for a zone
92
112
func (p * Provider ) deleteRecord (ctx context.Context , zone string , record libdns.Record ) error {
93
-
94
- recordId , _ := strconv .Atoi (record .ID )
95
-
96
- payload := map [string ]any {
97
- "Domain" : strings .Trim (zone , "." ),
98
- "RecordId" : recordId ,
113
+ client , err := p .getClient ()
114
+ if err != nil {
115
+ return err
99
116
}
117
+ recordId , _ := strconv .Atoi (record .ID )
118
+ request := dnspod .NewDeleteRecordRequest ()
119
+ request .Domain = common .StringPtr (strings .Trim (zone , "." ))
120
+ request .RecordId = common .Uint64Ptr (uint64 (recordId ))
100
121
101
- _ , err := p .doRequest ("DeleteRecord" , payload )
102
-
103
- return err
104
-
105
- }
106
-
107
- func (p * Provider ) doRequest (action string , payload any ) ([]byte , error ) {
108
-
109
- cpf := tp .NewClientProfile ()
110
- cpf .HttpProfile .RootDomain = "tencentcloudapi.com"
111
-
112
- cred := tc .NewCredential (p .SecretId , p .SecretKey )
113
- client := tc .NewCommonClient (cred , "" , cpf )
114
-
115
- request := th .NewCommonRequest ("dnspod" , "2021-03-23" , action )
116
- request .SetActionParameters (payload )
117
-
118
- response := th .NewCommonResponse ()
119
-
120
- if err := client .Send (request , response ); err != nil {
121
- return nil , err
122
+ _ , err = client .DeleteRecord (request )
123
+ if err != nil {
124
+ return err
122
125
}
123
-
124
- return response .GetBody (), nil
125
-
126
+ return nil
126
127
}
0 commit comments