From c00cda5a75babbc7f908c34402bbb8f46e799da4 Mon Sep 17 00:00:00 2001 From: eryajf Date: Sun, 1 Sep 2024 15:39:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=B0=86=E5=85=AC=E7=94=A8=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=A7=BB=E5=8A=A8=E5=88=B0public=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cmd/root.go | 3 ++- pkg/export/cron_cache.go | 3 ++- pkg/export/export_gauge.go | 3 ++- pkg/provider/aliyun.go | 5 +++-- pkg/provider/tencent.go | 3 ++- {pkg => public}/logger/logger.go | 0 public/public.go | 3 ++- {pkg => public}/tools/time.go | 0 8 files changed, 13 insertions(+), 7 deletions(-) rename {pkg => public}/logger/logger.go (100%) rename {pkg => public}/tools/time.go (100%) diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index ee49eaa..da275ef 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -8,7 +8,8 @@ import ( "runtime" "github.com/eryajf/cloud_dns_exporter/pkg/export" - "github.com/eryajf/cloud_dns_exporter/pkg/logger" + "github.com/eryajf/cloud_dns_exporter/public/logger" + "github.com/eryajf/cloud_dns_exporter/public" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" diff --git a/pkg/export/cron_cache.go b/pkg/export/cron_cache.go index f198b91..2125a80 100644 --- a/pkg/export/cron_cache.go +++ b/pkg/export/cron_cache.go @@ -5,7 +5,8 @@ import ( "fmt" "sync" - "github.com/eryajf/cloud_dns_exporter/pkg/logger" + "github.com/eryajf/cloud_dns_exporter/public/logger" + "github.com/eryajf/cloud_dns_exporter/pkg/provider" "github.com/eryajf/cloud_dns_exporter/public" "github.com/robfig/cron/v3" diff --git a/pkg/export/export_gauge.go b/pkg/export/export_gauge.go index 6b3f816..0c29a82 100644 --- a/pkg/export/export_gauge.go +++ b/pkg/export/export_gauge.go @@ -5,7 +5,8 @@ import ( "fmt" "sync" - "github.com/eryajf/cloud_dns_exporter/pkg/logger" + "github.com/eryajf/cloud_dns_exporter/public/logger" + "github.com/eryajf/cloud_dns_exporter/pkg/provider" "github.com/eryajf/cloud_dns_exporter/public" "github.com/prometheus/client_golang/prometheus" diff --git a/pkg/provider/aliyun.go b/pkg/provider/aliyun.go index 752a742..71b75ec 100644 --- a/pkg/provider/aliyun.go +++ b/pkg/provider/aliyun.go @@ -10,8 +10,9 @@ import ( openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" "github.com/alibabacloud-go/tea/tea" - "github.com/eryajf/cloud_dns_exporter/pkg/logger" - "github.com/eryajf/cloud_dns_exporter/pkg/tools" + "github.com/eryajf/cloud_dns_exporter/public/logger" + "github.com/eryajf/cloud_dns_exporter/public/tools" + "github.com/eryajf/cloud_dns_exporter/public" ) diff --git a/pkg/provider/tencent.go b/pkg/provider/tencent.go index 0939ac2..dbdbaed 100644 --- a/pkg/provider/tencent.go +++ b/pkg/provider/tencent.go @@ -7,7 +7,8 @@ import ( "time" "github.com/alibabacloud-go/tea/tea" - "github.com/eryajf/cloud_dns_exporter/pkg/logger" + "github.com/eryajf/cloud_dns_exporter/public/logger" + "github.com/eryajf/cloud_dns_exporter/public" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" diff --git a/pkg/logger/logger.go b/public/logger/logger.go similarity index 100% rename from pkg/logger/logger.go rename to public/logger/logger.go diff --git a/public/public.go b/public/public.go index 0124699..ddc1fe0 100644 --- a/public/public.go +++ b/public/public.go @@ -7,7 +7,8 @@ import ( "time" "github.com/allegro/bigcache/v3" - "github.com/eryajf/cloud_dns_exporter/pkg/logger" + "github.com/eryajf/cloud_dns_exporter/public/logger" + "gopkg.in/yaml.v2" ) diff --git a/pkg/tools/time.go b/public/tools/time.go similarity index 100% rename from pkg/tools/time.go rename to public/tools/time.go From 673e6275985eb181244e2e5ce897cf25248cdbf7 Mon Sep 17 00:00:00 2001 From: eryajf Date: Sun, 1 Sep 2024 17:03:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8F=8A=E5=88=B0=E6=9C=9F=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +- go.mod | 5 ++- go.sum | 20 +++++++++-- pkg/cmd/root.go | 3 +- pkg/export/export_gauge.go | 5 +-- pkg/provider/aliyun.go | 73 ++++++++++++++++++++++++++++++++----- pkg/provider/provider.go | 16 +++++---- pkg/provider/tencent.go | 74 +++++++++++++++++++++++++++++++++----- public/tools/time.go | 8 ----- 9 files changed, 167 insertions(+), 40 deletions(-) delete mode 100644 public/tools/time.go diff --git a/README.md b/README.md index 073f579..30e828c 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,8 @@ domain_list{ domain_name="域名", domain_remark="域名备注", domain_status="域名状态", - create_time="域名创建时间"} 0 + create_data="域名创建日期", + create_data="域名到期日期"} 99 (此value为域名距离到期的天数) record_list{ diff --git a/go.mod b/go.mod index 5270b04..8bff2f0 100644 --- a/go.mod +++ b/go.mod @@ -7,14 +7,17 @@ toolchain go1.22.4 require ( github.com/alibabacloud-go/alidns-20150109/v4 v4.5.5 github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.9 + github.com/alibabacloud-go/domain-20180129/v4 v4.2.0 github.com/alibabacloud-go/tea v1.2.2 github.com/allegro/bigcache/v3 v3.1.0 github.com/charmbracelet/log v0.2.2 + github.com/golang-module/carbon/v2 v2.3.12 github.com/prometheus/client_golang v1.16.0 github.com/robfig/cron/v3 v3.0.1 github.com/spf13/cobra v1.8.1 - github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.989 + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.989 + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.993 ) require ( diff --git a/go.sum b/go.sum index ef54dcf..28e38a2 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.9/go.mod h1:bb+Io8Sn2RuM3/R github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68/go.mod h1:6pb/Qy8c+lqua8cFpEy7g39NRRqOWc3rOwAy8m5Y2BY= github.com/alibabacloud-go/debug v1.0.0 h1:3eIEQWfay1fB24PQIEzXAswlVJtdQok8f3EVN5VrBnA= github.com/alibabacloud-go/debug v1.0.0/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc= +github.com/alibabacloud-go/domain-20180129/v4 v4.2.0 h1:PAEt76VDoXbQODWeN9PTwYhA5NoEbJSK/yzGX2DZXrQ= +github.com/alibabacloud-go/domain-20180129/v4 v4.2.0/go.mod h1:q0n3wgGRndhuZsAXFVCFtiSR8+W0so85qYtKLzR2b18= github.com/alibabacloud-go/endpoint-util v1.1.0 h1:r/4D3VSw888XGaeNpP994zDUaxdgTSHBbVfZlzf6b5Q= github.com/alibabacloud-go/endpoint-util v1.1.0/go.mod h1:O5FuCALmCKs2Ff7JFJMudHs0I5EBgecXXxZRyswlEjE= github.com/alibabacloud-go/openapi-util v0.1.0 h1:0z75cIULkDrdEhkLWgi9tnLe+KhAFE/r5Pb3312/eAY= @@ -48,6 +50,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/golang-module/carbon/v2 v2.3.12 h1:VC1DwN1kBwJkh5MjXmTFryjs5g4CWyoM8HAHffZPX/k= +github.com/golang-module/carbon/v2 v2.3.12/go.mod h1:HNsedGzXGuNciZImYP2OMnpiwq/vhIstR/vn45ib5cI= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -119,14 +123,23 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= -github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.989 h1:jqwrVW8ZkPSAsGnpF9lLotmrkNunbC0cKrrTZRPksFY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.989/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993 h1:+iJMmF0q1MPyhLs0+J7CcJ47w/vq6ICsCxnV4gc0dKw= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.993/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.989 h1:EK4u1YQBGhgvZU0DeJEGX1oWEP/Gigq7sEwLdZBZLyA= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.989/go.mod h1:pPFLkcTX4Z+Exqd6dAQueimkvniIHzqUvvaf6o9uaa8= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.993 h1:v/L8pM1EjEhHudA6B/LduXN87QpoFZ5kgfpLcsMpLQs= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.993/go.mod h1:fRBKEOc+KFiglAiIHIozQiveI6O+unpw0qL95iunZxA= github.com/tjfoc/gmsm v1.3.2 h1:7JVkAn5bvUJ7HtU08iW6UiD+UTmJTIToHCfeFzkcCxM= github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -215,5 +228,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index da275ef..b50ab17 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -64,7 +64,8 @@ func RunServer() {

Cloud DNS Exporter

Metrics

-

By Eryajf

+

Source Repo

+

Create By Eryajf

`)) if err != nil { diff --git a/pkg/export/export_gauge.go b/pkg/export/export_gauge.go index 0c29a82..d6c3d5a 100644 --- a/pkg/export/export_gauge.go +++ b/pkg/export/export_gauge.go @@ -41,7 +41,8 @@ func NewMetrics(namespace string) *Metrics { "domain_name", "domain_remark", "domain_status", - "create_time", + "created_date", + "expiry_date", }), public.RecordList: newGlobalMetric(namespace, public.RecordList, @@ -114,7 +115,7 @@ func (c *Metrics) Collect(ch chan<- prometheus.Metric) { } for _, v := range domains { ch <- prometheus.MustNewConstMetric( - c.metrics[public.DomainList], prometheus.GaugeValue, 1, v.CloudProvider, v.CloudName, v.DomainID, v.DomainName, v.DomainRemark, v.DomainStatus, v.CreateTime) + c.metrics[public.DomainList], prometheus.GaugeValue, float64(v.DaysUntilExpiry), v.CloudProvider, v.CloudName, v.DomainID, v.DomainName, v.DomainRemark, v.DomainStatus, v.CreatedDate, v.ExpiryDate) } // get record list from cache recordListCacheKey := public.RecordList + "_" + cloudProvider + "_" + cloudName diff --git a/pkg/provider/aliyun.go b/pkg/provider/aliyun.go index 71b75ec..5f2d173 100644 --- a/pkg/provider/aliyun.go +++ b/pkg/provider/aliyun.go @@ -8,10 +8,11 @@ import ( alidns "github.com/alibabacloud-go/alidns-20150109/v4/client" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" + domain "github.com/alibabacloud-go/domain-20180129/v4/client" "github.com/alibabacloud-go/tea/tea" + "github.com/golang-module/carbon/v2" "github.com/eryajf/cloud_dns_exporter/public/logger" - "github.com/eryajf/cloud_dns_exporter/public/tools" "github.com/eryajf/cloud_dns_exporter/public" ) @@ -65,15 +66,22 @@ func (a *AliyunDNS) ListDomains() ([]Domain, error) { if err != nil { return nil, err } + domainNames, err := a.getDomainNameList() + if err != nil { + return nil, err + } for _, v := range domains { + domainCreateAndExpiryDate := a.getDomainCreateAndExpiryDate(domainNames, v) dataObj = append(dataObj, Domain{ - CloudProvider: a.account.CloudProvider, - CloudName: a.account.CloudName, - DomainID: tea.StringValue(v.DomainId), - DomainName: tea.StringValue(v.DomainName), - DomainRemark: tea.StringValue(v.Remark), - DomainStatus: "enable", - CreateTime: tea.StringValue(v.CreateTime), + CloudProvider: a.account.CloudProvider, + CloudName: a.account.CloudName, + DomainID: tea.StringValue(v.DomainId), + DomainName: tea.StringValue(v.DomainName), + DomainRemark: tea.StringValue(v.Remark), + DomainStatus: "enable", + CreatedDate: domainCreateAndExpiryDate.CreatedDate, + ExpiryDate: domainCreateAndExpiryDate.ExpiryDate, + DaysUntilExpiry: domainCreateAndExpiryDate.DaysUntilExpiry, }) } return dataObj, nil @@ -136,7 +144,7 @@ func (a *AliyunDNS) ListRecords() ([]Record, error) { RecordWeight: fmt.Sprintf("%d", tea.Int32Value(v.Weight)), RecordStatus: oneStatus(tea.StringValue(v.Status)), RecordRemark: tea.StringValue(v.Remark), - UpdateTime: tools.GetReadTimeMs(tea.Int64Value(v.UpdateTimestamp)), + UpdateTime: carbon.CreateFromTimestampMilli(tea.Int64Value(v.UpdateTimestamp)).ToDateTimeString(), FullRecord: tea.StringValue(v.RR) + "." + domain, }) } @@ -190,3 +198,50 @@ func (a *AliyunDNS) getRecordList(domain string) (rst []*alidns.DescribeDomainRe } return } + +// https://next.api.aliyun.com/document/Domain/2018-01-29/QueryDomainList +// getDomainNameList 获取域名列表 +func (a *AliyunDNS) getDomainNameList() (rst []*domain.QueryDomainListResponseBodyDataDomain, err error) { + config := openapi.Config{ + AccessKeyId: tea.String(a.account.SecretID), + AccessKeySecret: tea.String(a.account.SecretKey), + } + config.Endpoint = tea.String("domain.aliyuncs.com") + client, err := domain.NewClient(&config) + if err != nil { + return nil, err + } + var ( + pageNumber int32 = 1 + pageSize int32 = 500 + ) + for { + resp, err := client.QueryDomainList(&domain.QueryDomainListRequest{ + PageNum: tea.Int32(pageNumber), + PageSize: tea.Int32(pageSize), + }) + if err != nil { + return nil, err + } + rst = append(rst, resp.Body.Data.Domain...) + if len(resp.Body.Data.Domain) < int(pageSize) { + break + } + pageNumber++ + } + return +} + +// getDomainCreateAndExpiryDate 获取域名的创建时间与到期时间 +func (a *AliyunDNS) getDomainCreateAndExpiryDate(domainList []*domain.QueryDomainListResponseBodyDataDomain, domain *alidns.DescribeDomainsResponseBodyDomainsDomain) (d Domain) { + for _, v := range domainList { + if tea.StringValue(v.DomainName) == tea.StringValue(domain.DomainName) { + d.CreatedDate = tea.StringValue(v.RegistrationDate) + d.ExpiryDate = tea.StringValue(v.ExpirationDate) + if d.ExpiryDate != "" { + d.DaysUntilExpiry = carbon.Now().DiffInDays(carbon.Parse(d.ExpiryDate)) + } + } + } + return +} diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index e463333..1203074 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -36,13 +36,15 @@ func init() { // Doamin 域名信息 type Domain struct { - CloudProvider string `json:"cloud_provider"` - CloudName string `json:"cloud_name"` - DomainID string `json:"domain_id"` - DomainName string `json:"domain_name"` - DomainRemark string `json:"domain_remark"` - DomainStatus string `json:"domain_status"` - CreateTime string `json:"create_time"` + CloudProvider string `json:"cloud_provider"` + CloudName string `json:"cloud_name"` + DomainID string `json:"domain_id"` + DomainName string `json:"domain_name"` + DomainRemark string `json:"domain_remark"` + DomainStatus string `json:"domain_status"` + CreatedDate string `json:"created_date"` + ExpiryDate string `json:"expiry_date"` + DaysUntilExpiry int64 `json:"days_until_expiry"` } // Record 域名记录信息 diff --git a/pkg/provider/tencent.go b/pkg/provider/tencent.go index dbdbaed..915d7a6 100644 --- a/pkg/provider/tencent.go +++ b/pkg/provider/tencent.go @@ -8,12 +8,14 @@ import ( "github.com/alibabacloud-go/tea/tea" "github.com/eryajf/cloud_dns_exporter/public/logger" + "github.com/golang-module/carbon/v2" "github.com/eryajf/cloud_dns_exporter/public" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323" + domain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain/v20180808" ) type TencentCloudDNS struct { @@ -63,15 +65,22 @@ func (t *TencentCloudDNS) ListDomains() ([]Domain, error) { if err != nil { return nil, err } + domainNames, err := t.getDomainNameList() + if err != nil { + return nil, err + } for _, v := range domains { + domainCreateAndExpiryDate := t.getDomainCreateAndExpiryDate(domainNames, v) dataObj = append(dataObj, Domain{ - CloudProvider: t.account.CloudProvider, - CloudName: t.account.CloudName, - DomainID: fmt.Sprintf("%d", tea.Uint64Value(v.DomainId)), - DomainName: tea.StringValue(v.Name), - DomainRemark: tea.StringValue(v.Remark), - DomainStatus: oneStatus(tea.StringValue(v.Status)), - CreateTime: tea.StringValue(v.CreatedOn), + CloudProvider: t.account.CloudProvider, + CloudName: t.account.CloudName, + DomainID: fmt.Sprintf("%d", tea.Uint64Value(v.DomainId)), + DomainName: tea.StringValue(v.Name), + DomainRemark: tea.StringValue(v.Remark), + DomainStatus: oneStatus(tea.StringValue(v.Status)), + CreatedDate: domainCreateAndExpiryDate.CreatedDate, + ExpiryDate: domainCreateAndExpiryDate.ExpiryDate, + DaysUntilExpiry: domainCreateAndExpiryDate.DaysUntilExpiry, }) } return dataObj, nil @@ -143,7 +152,7 @@ func (t *TencentCloudDNS) ListRecords() ([]Record, error) { } // https://cloud.tencent.com/document/api/1427/56172 -// GetDomainList 获取云账号下域名列表 +// GetDomainList 获取云解析中域名列表 func (t *TencentCloudDNS) getDomainList() ([]*dnspod.DomainListItem, error) { request := dnspod.NewDescribeDomainListRequest() response, err := t.client.DescribeDomainList(request) @@ -186,3 +195,52 @@ func (t *TencentCloudDNS) getRecordList(domain string) ([]*dnspod.RecordListItem } return temp, nil } + +// https://cloud.tencent.com/document/api/242/48941 +// getDomainNameList 获取域名列表(与云解析的域名列表注意区分) +func (t *TencentCloudDNS) getDomainNameList() ([]*domain.DomainList, error) { + var ( + offset uint64 = 0 + limit uint64 = 100 + temp []*domain.DomainList + ) + cpf := profile.NewClientProfile() + cpf.HttpProfile.Endpoint = "domain.tencentcloudapi.com" + client, _ := domain.NewClient(common.NewCredential( + t.account.SecretID, + t.account.SecretKey, + ), "", cpf) + + request := domain.NewDescribeDomainNameListRequest() + for { + request.Offset = common.Uint64Ptr(offset) + request.Limit = common.Uint64Ptr(limit) + response, err := client.DescribeDomainNameList(request) + if _, ok := err.(*errors.TencentCloudSDKError); ok { + return nil, err + } + if err != nil { + return nil, err + } + temp = append(temp, response.Response.DomainSet...) + if len(response.Response.DomainSet) == 0 { + break + } + offset += limit + } + return temp, nil +} + +// getDomainCreateAndExpiryDate 获取域名的创建时间与到期时间 +func (t *TencentCloudDNS) getDomainCreateAndExpiryDate(domainList []*domain.DomainList, domain *dnspod.DomainListItem) (d Domain) { + for _, v := range domainList { + if tea.StringValue(v.DomainName) == tea.StringValue(domain.Name) { + d.CreatedDate = tea.StringValue(v.CreationDate) + d.ExpiryDate = tea.StringValue(v.ExpirationDate) + if d.ExpiryDate != "" { + d.DaysUntilExpiry = carbon.Now().DiffInDays(carbon.Parse(d.ExpiryDate)) + } + } + } + return +} diff --git a/public/tools/time.go b/public/tools/time.go deleted file mode 100644 index 15df6eb..0000000 --- a/public/tools/time.go +++ /dev/null @@ -1,8 +0,0 @@ -package tools - -import "time" - -// GetReadTimeMs 将毫秒的时间戳转换为时间 -func GetReadTimeMs(s int64) string { - return time.Unix(0, s*int64(time.Millisecond)).Format("2006-01-02 15:04:05") -}