Skip to content

Commit a60f31b

Browse files
committed
fix: fix find record bug
1 parent c569614 commit a60f31b

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

pkg/api/api.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package api
22

33
import (
4-
"ddns/pkg/utils"
5-
64
alidns20150109 "github.com/alibabacloud-go/alidns-20150109/v2/client"
75
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
86
"github.com/alibabacloud-go/tea/tea"
@@ -24,32 +22,28 @@ func CreateClient(accessKeyId string, accessKeySecret string) (_result *alidns20
2422
return _result, _err
2523
}
2624

27-
func DescribeDomainRecords(client *alidns20150109.Client, domainName string) (
28-
[]*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {
25+
func FindRecordByRR(client *alidns20150109.Client, domainName, rr string) (
26+
*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {
2927

3028
describeDomainRecordsRequest := &alidns20150109.DescribeDomainRecordsRequest{
3129
DomainName: tea.String(domainName),
30+
KeyWord: tea.String(rr),
31+
SearchMode: tea.String("EXACT"),
3232
}
3333
resp, err := client.DescribeDomainRecords(describeDomainRecordsRequest)
3434
if err != nil {
3535
return nil, err
3636
}
3737
records := resp.Body.DomainRecords.Record
38-
39-
log.Debug(utils.FUNCTION()+"success", zap.Reflect("records", records))
40-
return records, nil
41-
}
42-
43-
func FindRecordByRR(
44-
records []*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord,
45-
rr string) *alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord {
38+
log.Debug("describe domain records success", zap.Reflect("records", records))
4639

4740
for _, rec := range records {
4841
if *rec.RR == rr {
49-
return rec
42+
return rec, nil
5043
}
5144
}
52-
return nil
45+
46+
return nil, nil
5347
}
5448

5549
func UpdateRecord(client *alidns20150109.Client, id, rr, typ, value string) error {

pkg/service/service.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,29 @@ func UpdateDns() {
2121
return
2222
}
2323

24-
records, err := api.DescribeDomainRecords(client, config.Cfg.Domain)
25-
if err != nil {
26-
log.Error("list record failed", zap.Error(err))
27-
return
28-
}
29-
if len(records) == 0 {
30-
log.Error("record not found")
31-
return
32-
}
33-
log.Info("describe domain records", zap.Reflect("records", records))
34-
3524
for _, ddns := range config.Cfg.DDNSs {
3625
if !ddns.Enable {
37-
continue
26+
return
3827
}
3928
logFields := []zap.Field{zap.Reflect("ddns", ddns)}
4029

30+
// get ip
4131
ip, err := utils.GetIpWithPrefix(ddns.Interface, ddns.Prefix)
4232
if err != nil {
4333
log.Error("get interface ip failed", append(logFields, zap.Error(err))...)
4434
continue
4535
}
4636
logFields = append(logFields, zap.String("ip", ip))
4737

48-
record := api.FindRecordByRR(records, ddns.RR)
38+
// find record
39+
record, err := api.FindRecordByRR(client, config.Cfg.Domain, ddns.RR)
40+
if err != nil {
41+
log.Error("find record by rr failed", append(logFields, zap.Error(err))...)
42+
continue
43+
}
44+
logFields = append(logFields, zap.Reflect("record", record))
45+
46+
// create or update
4947
if record == nil { // create
5048
err = api.CreateRecord(client, config.Cfg.Domain, ddns.RR, ddns.Type, ip)
5149
if err != nil {

0 commit comments

Comments
 (0)