可以设置集群的DNS记录TTL

This commit is contained in:
刘祥超
2021-09-20 20:01:21 +08:00
parent d121bc86a0
commit d4d7b1fff7
14 changed files with 113 additions and 16 deletions

View File

@@ -15,4 +15,5 @@ type Record struct {
Type RecordType `json:"type"`
Value string `json:"value"`
Route string `json:"route"`
TTL int32 `json:"ttl"`
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"strings"
)
@@ -87,6 +88,7 @@ func (this *AliDNSProvider) GetRecords(domain string) (records []*dnstypes.Recor
Type: record.Type,
Value: record.Value,
Route: record.Line,
TTL: types.Int32(record.TTL),
})
}
@@ -141,6 +143,10 @@ func (this *AliDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record)
req.DomainName = domain
req.Line = newRecord.Route
if newRecord.TTL > 0 {
req.TTL = requests.NewInteger(types.Int(newRecord.TTL))
}
resp := alidns.CreateAddDomainRecordResponse()
err := this.doAPI(req, resp)
if err != nil {
@@ -162,6 +168,10 @@ func (this *AliDNSProvider) UpdateRecord(domain string, record *dnstypes.Record,
req.Value = newRecord.Value
req.Line = newRecord.Route
if newRecord.TTL > 0 {
req.TTL = requests.NewInteger(types.Int(newRecord.TTL))
}
resp := alidns.CreateUpdateDomainRecordResponse()
err := this.doAPI(req, resp)
return err

View File

@@ -10,6 +10,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"io"
"io/ioutil"
"net/http"
@@ -158,6 +159,7 @@ func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordTy
Name: record.Name,
Type: record.Type,
Value: record.Content,
TTL: types.Int32(record.Ttl),
Route: CloudFlareDefaultRoute,
}, nil
}
@@ -170,11 +172,17 @@ func (this *CloudFlareProvider) AddRecord(domain string, newRecord *dnstypes.Rec
}
resp := new(cloudflare.CreateDNSRecordResponse)
var ttl = newRecord.TTL
if ttl <= 0 {
ttl = 1 // 自动默认
}
err = this.doAPI(http.MethodPost, "zones/"+zoneId+"/dns_records", nil, maps.Map{
"type": newRecord.Type,
"name": newRecord.Name + "." + domain,
"content": newRecord.Value,
"ttl": 1,
"ttl": ttl,
}, resp)
if err != nil {
return err
@@ -189,12 +197,17 @@ func (this *CloudFlareProvider) UpdateRecord(domain string, record *dnstypes.Rec
return err
}
var ttl = newRecord.TTL
if ttl <= 0 {
ttl = 1 // 自动默认
}
resp := new(cloudflare.UpdateDNSRecordResponse)
return this.doAPI(http.MethodPut, "zones/"+zoneId+"/dns_records/"+record.Id, nil, maps.Map{
"type": newRecord.Type,
"name": newRecord.Name + "." + domain,
"content": newRecord.Value,
"ttl": 1,
"ttl": ttl,
}, resp)
}

View File

@@ -98,6 +98,7 @@ func TestCloudFlareProvider_AddRecord(t *testing.T) {
Type: dnstypes.RecordTypeA,
Value: "182.92.212.46",
Route: "",
TTL: 300,
})
if err != nil {
t.Fatal(err)

View File

@@ -13,6 +13,10 @@ import (
"strings"
)
const (
DNSPodMaxTTL int32 = 604800
)
// DNSPodProvider DNSPod服务商
type DNSPodProvider struct {
BaseProvider
@@ -94,6 +98,7 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Recor
Type: recordMap.GetString("type"),
Value: recordMap.GetString("value"),
Route: recordMap.GetString("line"),
TTL: recordMap.GetInt32("ttl"),
})
}
@@ -165,13 +170,18 @@ func (this *DNSPodProvider) AddRecord(domain string, newRecord *dnstypes.Record)
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
newRecord.Value += "."
}
_, err := this.post("/Record.Create", map[string]string{
var args = map[string]string{
"domain": domain,
"sub_domain": newRecord.Name,
"record_type": newRecord.Type,
"value": newRecord.Value,
"record_line": newRecord.Route,
})
}
if newRecord.TTL > 0 && newRecord.TTL <= DNSPodMaxTTL {
args["ttl"] = types.String(newRecord.TTL)
}
_, err := this.post("/Record.Create", args)
return err
}
@@ -188,14 +198,19 @@ func (this *DNSPodProvider) UpdateRecord(domain string, record *dnstypes.Record,
if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
newRecord.Value += "."
}
_, err := this.post("/Record.Modify", map[string]string{
var args = map[string]string{
"domain": domain,
"record_id": record.Id,
"sub_domain": newRecord.Name,
"record_type": newRecord.Type,
"value": newRecord.Value,
"record_line": newRecord.Route,
})
}
if newRecord.TTL > 0 && newRecord.TTL <= DNSPodMaxTTL {
args["ttl"] = types.String(newRecord.TTL)
}
_, err := this.post("/Record.Modify", args)
return err
}

View File

@@ -58,6 +58,7 @@ func TestDNSPodProvider_AddRecord(t *testing.T) {
Name: "hello-forward",
Value: "hello.yun4s.cn",
Route: "联通",
TTL: 300,
})
if err != nil {
t.Fatal(err)

View File

@@ -35,6 +35,8 @@ var huaweiDNSHTTPClient = &http.Client{
},
}
// HuaweiDNSProvider 华为云DNS
// 相关文档链接https://support.huaweicloud.com/api-dns/dns_api_62001.html
type HuaweiDNSProvider struct {
BaseProvider
@@ -100,6 +102,7 @@ func (this *HuaweiDNSProvider) GetRecords(domain string) (records []*dnstypes.Re
Type: recordSet.Type,
Value: value,
Route: recordSet.Line,
TTL: types.Int32(recordSet.Ttl),
})
}
}
@@ -1320,6 +1323,7 @@ func (this *HuaweiDNSProvider) QueryRecord(domain string, name string, recordTyp
Type: recordType,
Value: recordSet.Records[0],
Route: recordSet.Line,
TTL: types.Int32(recordSet.Ttl),
}, nil
}
@@ -1331,12 +1335,17 @@ func (this *HuaweiDNSProvider) AddRecord(domain string, newRecord *dnstypes.Reco
}
var resp = new(huaweidns.ZonesCreateRecordSetResponse)
var ttl = newRecord.TTL
if ttl <= 0 {
ttl = 300
}
err = this.doAPI(http.MethodPost, "/v2.1/zones/"+zoneId+"/recordsets", map[string]string{}, maps.Map{
"name": newRecord.Name + "." + domain + ".",
"description": "CDN系统自动创建",
"type": newRecord.Type,
"records": []string{newRecord.Value},
"line": newRecord.Route,
"ttl": ttl,
}, resp)
if err != nil {
return err
@@ -1362,6 +1371,11 @@ func (this *HuaweiDNSProvider) UpdateRecord(domain string, record *dnstypes.Reco
recordId = record.Id
}
var ttl = newRecord.TTL
if ttl <= 0 {
ttl = 300
}
var resp = new(huaweidns.ZonesUpdateRecordSetResponse)
err = this.doAPI(http.MethodPut, "/v2.1/zones/"+zoneId+"/recordsets/"+recordId, map[string]string{}, maps.Map{
"name": newRecord.Name + "." + domain + ".",
@@ -1369,6 +1383,7 @@ func (this *HuaweiDNSProvider) UpdateRecord(domain string, record *dnstypes.Reco
"type": newRecord.Type,
"records": []string{newRecord.Value},
"line": newRecord.Route, // TODO 华为云此API无法修改线路API地址https://support.huaweicloud.com/api-dns/dns_api_65006.html
"ttl": ttl,
}, resp)
if err != nil {
return err

View File

@@ -71,6 +71,7 @@ func TestHuaweiDNSProvider_AddRecord(t *testing.T) {
Type: "A",
Value: "192.168.2.40",
Route: "Beijing",
TTL: 120,
}
err = provider.AddRecord("yun4s.cn", record)
if err != nil {

View File

@@ -83,6 +83,7 @@ func (this *LocalEdgeDNSProvider) GetRecords(domain string) (records []*dnstypes
Type: record.Type,
Value: record.Value,
Route: routeIds[0],
TTL: types.Int32(record.Ttl),
})
}
@@ -183,6 +184,7 @@ func (this *LocalEdgeDNSProvider) QueryRecord(domain string, name string, record
Type: record.Type,
Value: record.Value,
Route: routeIdString,
TTL: types.Int32(record.Ttl),
}, nil
}
@@ -202,7 +204,10 @@ func (this *LocalEdgeDNSProvider) AddRecord(domain string, newRecord *dnstypes.R
routeIds = append(routeIds, newRecord.Route)
}
_, err = nameservers.SharedNSRecordDAO.CreateRecord(tx, domainId, "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds)
if newRecord.TTL <= 0 {
newRecord.TTL = this.ttl
}
_, err = nameservers.SharedNSRecordDAO.CreateRecord(tx, domainId, "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds)
if err != nil {
return err
}
@@ -226,8 +231,12 @@ func (this *LocalEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.R
routeIds = append(routeIds, newRecord.Route)
}
if newRecord.TTL <= 0 {
newRecord.TTL = this.ttl
}
if len(record.Id) > 0 {
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(record.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds, true)
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(record.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds, true)
if err != nil {
return err
}
@@ -237,7 +246,7 @@ func (this *LocalEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.R
return err
}
if realRecord != nil {
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(realRecord.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds, true)
err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(realRecord.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds, true)
if err != nil {
return err
}

View File

@@ -101,6 +101,7 @@ func TestLocalEdgeDNSProvider_AddRecord(t *testing.T) {
Type: dnstypes.RecordTypeA,
Value: "10.0.0.1",
Route: "id:7",
TTL: 300,
})
if err != nil {
t.Fatal(err)