@@ -8,12 +8,14 @@ import (
88
99 "github.com/alibabacloud-go/tea/tea"
1010 "github.com/eryajf/cloud_dns_exporter/public/logger"
11+ "github.com/golang-module/carbon/v2"
1112
1213 "github.com/eryajf/cloud_dns_exporter/public"
1314 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
1415 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1516 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
1617 dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
18+ domain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain/v20180808"
1719)
1820
1921type TencentCloudDNS struct {
@@ -63,15 +65,22 @@ func (t *TencentCloudDNS) ListDomains() ([]Domain, error) {
6365 if err != nil {
6466 return nil , err
6567 }
68+ domainNames , err := t .getDomainNameList ()
69+ if err != nil {
70+ return nil , err
71+ }
6672 for _ , v := range domains {
73+ domainCreateAndExpiryDate := t .getDomainCreateAndExpiryDate (domainNames , v )
6774 dataObj = append (dataObj , Domain {
68- CloudProvider : t .account .CloudProvider ,
69- CloudName : t .account .CloudName ,
70- DomainID : fmt .Sprintf ("%d" , tea .Uint64Value (v .DomainId )),
71- DomainName : tea .StringValue (v .Name ),
72- DomainRemark : tea .StringValue (v .Remark ),
73- DomainStatus : oneStatus (tea .StringValue (v .Status )),
74- CreateTime : tea .StringValue (v .CreatedOn ),
75+ CloudProvider : t .account .CloudProvider ,
76+ CloudName : t .account .CloudName ,
77+ DomainID : fmt .Sprintf ("%d" , tea .Uint64Value (v .DomainId )),
78+ DomainName : tea .StringValue (v .Name ),
79+ DomainRemark : tea .StringValue (v .Remark ),
80+ DomainStatus : oneStatus (tea .StringValue (v .Status )),
81+ CreatedDate : domainCreateAndExpiryDate .CreatedDate ,
82+ ExpiryDate : domainCreateAndExpiryDate .ExpiryDate ,
83+ DaysUntilExpiry : domainCreateAndExpiryDate .DaysUntilExpiry ,
7584 })
7685 }
7786 return dataObj , nil
@@ -143,7 +152,7 @@ func (t *TencentCloudDNS) ListRecords() ([]Record, error) {
143152}
144153
145154// https://cloud.tencent.com/document/api/1427/56172
146- // GetDomainList 获取云账号下域名列表
155+ // GetDomainList 获取云解析中域名列表
147156func (t * TencentCloudDNS ) getDomainList () ([]* dnspod.DomainListItem , error ) {
148157 request := dnspod .NewDescribeDomainListRequest ()
149158 response , err := t .client .DescribeDomainList (request )
@@ -186,3 +195,52 @@ func (t *TencentCloudDNS) getRecordList(domain string) ([]*dnspod.RecordListItem
186195 }
187196 return temp , nil
188197}
198+
199+ // https://cloud.tencent.com/document/api/242/48941
200+ // getDomainNameList 获取域名列表(与云解析的域名列表注意区分)
201+ func (t * TencentCloudDNS ) getDomainNameList () ([]* domain.DomainList , error ) {
202+ var (
203+ offset uint64 = 0
204+ limit uint64 = 100
205+ temp []* domain.DomainList
206+ )
207+ cpf := profile .NewClientProfile ()
208+ cpf .HttpProfile .Endpoint = "domain.tencentcloudapi.com"
209+ client , _ := domain .NewClient (common .NewCredential (
210+ t .account .SecretID ,
211+ t .account .SecretKey ,
212+ ), "" , cpf )
213+
214+ request := domain .NewDescribeDomainNameListRequest ()
215+ for {
216+ request .Offset = common .Uint64Ptr (offset )
217+ request .Limit = common .Uint64Ptr (limit )
218+ response , err := client .DescribeDomainNameList (request )
219+ if _ , ok := err .(* errors.TencentCloudSDKError ); ok {
220+ return nil , err
221+ }
222+ if err != nil {
223+ return nil , err
224+ }
225+ temp = append (temp , response .Response .DomainSet ... )
226+ if len (response .Response .DomainSet ) == 0 {
227+ break
228+ }
229+ offset += limit
230+ }
231+ return temp , nil
232+ }
233+
234+ // getDomainCreateAndExpiryDate 获取域名的创建时间与到期时间
235+ func (t * TencentCloudDNS ) getDomainCreateAndExpiryDate (domainList []* domain.DomainList , domain * dnspod.DomainListItem ) (d Domain ) {
236+ for _ , v := range domainList {
237+ if tea .StringValue (v .DomainName ) == tea .StringValue (domain .Name ) {
238+ d .CreatedDate = tea .StringValue (v .CreationDate )
239+ d .ExpiryDate = tea .StringValue (v .ExpirationDate )
240+ if d .ExpiryDate != "" {
241+ d .DaysUntilExpiry = carbon .Now ().DiffInDays (carbon .Parse (d .ExpiryDate ))
242+ }
243+ }
244+ }
245+ return
246+ }
0 commit comments