Skip to content

Commit

Permalink
fix: fix find record bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsang committed Jan 14, 2023
1 parent c569614 commit a60f31b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
22 changes: 8 additions & 14 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package api

import (
"ddns/pkg/utils"

alidns20150109 "github.com/alibabacloud-go/alidns-20150109/v2/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
"github.com/alibabacloud-go/tea/tea"
Expand All @@ -24,32 +22,28 @@ func CreateClient(accessKeyId string, accessKeySecret string) (_result *alidns20
return _result, _err
}

func DescribeDomainRecords(client *alidns20150109.Client, domainName string) (
[]*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {
func FindRecordByRR(client *alidns20150109.Client, domainName, rr string) (
*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {

describeDomainRecordsRequest := &alidns20150109.DescribeDomainRecordsRequest{
DomainName: tea.String(domainName),
KeyWord: tea.String(rr),
SearchMode: tea.String("EXACT"),
}
resp, err := client.DescribeDomainRecords(describeDomainRecordsRequest)
if err != nil {
return nil, err
}
records := resp.Body.DomainRecords.Record

log.Debug(utils.FUNCTION()+"success", zap.Reflect("records", records))
return records, nil
}

func FindRecordByRR(
records []*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord,
rr string) *alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord {
log.Debug("describe domain records success", zap.Reflect("records", records))

for _, rec := range records {
if *rec.RR == rr {
return rec
return rec, nil
}
}
return nil

return nil, nil
}

func UpdateRecord(client *alidns20150109.Client, id, rr, typ, value string) error {
Expand Down
24 changes: 11 additions & 13 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,29 @@ func UpdateDns() {
return
}

records, err := api.DescribeDomainRecords(client, config.Cfg.Domain)
if err != nil {
log.Error("list record failed", zap.Error(err))
return
}
if len(records) == 0 {
log.Error("record not found")
return
}
log.Info("describe domain records", zap.Reflect("records", records))

for _, ddns := range config.Cfg.DDNSs {
if !ddns.Enable {
continue
return
}
logFields := []zap.Field{zap.Reflect("ddns", ddns)}

// get ip
ip, err := utils.GetIpWithPrefix(ddns.Interface, ddns.Prefix)
if err != nil {
log.Error("get interface ip failed", append(logFields, zap.Error(err))...)
continue
}
logFields = append(logFields, zap.String("ip", ip))

record := api.FindRecordByRR(records, ddns.RR)
// find record
record, err := api.FindRecordByRR(client, config.Cfg.Domain, ddns.RR)
if err != nil {
log.Error("find record by rr failed", append(logFields, zap.Error(err))...)
continue
}
logFields = append(logFields, zap.Reflect("record", record))

// create or update
if record == nil { // create
err = api.CreateRecord(client, config.Cfg.Domain, ddns.RR, ddns.Type, ip)
if err != nil {
Expand Down

0 comments on commit a60f31b

Please sign in to comment.