@@ -7,12 +7,15 @@ import (
77 "time"
88
99 "github.com/alibabacloud-go/tea/tea"
10- "github.com/eryajf/cloud_dns_exporter/pkg/logger"
10+ "github.com/eryajf/cloud_dns_exporter/public/logger"
11+ "github.com/golang-module/carbon/v2"
12+
1113 "github.com/eryajf/cloud_dns_exporter/public"
1214 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
1315 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1416 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
1517 dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
18+ domain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain/v20180808"
1619)
1720
1821type TencentCloudDNS struct {
@@ -62,15 +65,22 @@ func (t *TencentCloudDNS) ListDomains() ([]Domain, error) {
6265 if err != nil {
6366 return nil , err
6467 }
68+ domainNames , err := t .getDomainNameList ()
69+ if err != nil {
70+ return nil , err
71+ }
6572 for _ , v := range domains {
73+ domainCreateAndExpiryDate := t .getDomainCreateAndExpiryDate (domainNames , v )
6674 dataObj = append (dataObj , Domain {
67- CloudProvider : t .account .CloudProvider ,
68- CloudName : t .account .CloudName ,
69- DomainID : fmt .Sprintf ("%d" , tea .Uint64Value (v .DomainId )),
70- DomainName : tea .StringValue (v .Name ),
71- DomainRemark : tea .StringValue (v .Remark ),
72- DomainStatus : oneStatus (tea .StringValue (v .Status )),
73- 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 ,
7484 })
7585 }
7686 return dataObj , nil
@@ -142,7 +152,7 @@ func (t *TencentCloudDNS) ListRecords() ([]Record, error) {
142152}
143153
144154// https://cloud.tencent.com/document/api/1427/56172
145- // GetDomainList 获取云账号下域名列表
155+ // GetDomainList 获取云解析中域名列表
146156func (t * TencentCloudDNS ) getDomainList () ([]* dnspod.DomainListItem , error ) {
147157 request := dnspod .NewDescribeDomainListRequest ()
148158 response , err := t .client .DescribeDomainList (request )
@@ -185,3 +195,52 @@ func (t *TencentCloudDNS) getRecordList(domain string) ([]*dnspod.RecordListItem
185195 }
186196 return temp , nil
187197}
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