DIGITALOCEAN: Use diff2 to implement digitalocean provider (#3695)

This commit is contained in:
Juho Teperi 2025-07-28 16:37:56 +03:00 committed by GitHub
parent f35018590c
commit 91c143694f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,7 +10,7 @@ import (
"time" "time"
"github.com/StackExchange/dnscontrol/v4/models" "github.com/StackExchange/dnscontrol/v4/models"
"github.com/StackExchange/dnscontrol/v4/pkg/diff" "github.com/StackExchange/dnscontrol/v4/pkg/diff2"
"github.com/StackExchange/dnscontrol/v4/providers" "github.com/StackExchange/dnscontrol/v4/providers"
"github.com/digitalocean/godo" "github.com/digitalocean/godo"
"github.com/miekg/dns/dnsutil" "github.com/miekg/dns/dnsutil"
@ -179,21 +179,20 @@ func (api *digitaloceanProvider) GetZoneRecords(domain string, meta map[string]s
func (api *digitaloceanProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, existingRecords models.Records) ([]*models.Correction, int, error) { func (api *digitaloceanProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, existingRecords models.Records) ([]*models.Correction, int, error) {
ctx := context.Background() ctx := context.Background()
toReport, toCreate, toDelete, toModify, actualChangeCount, err := diff.NewCompat(dc).IncrementalDiff(existingRecords) var corrections []*models.Correction
instructions, actualChangeCount, err := diff2.ByRecord(existingRecords, dc, nil)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
// Start corrections with the reports
corrections := diff.GenerateMessageCorrections(toReport)
// Deletes first so changing type works etc. addCorrection := func(msg string, f func() (*godo.Response, error)) {
for _, m := range toDelete { corrections = append(corrections,
id := m.Existing.Original.(*godo.DomainRecord).ID &models.Correction{
corr := &models.Correction{ Msg: msg,
Msg: fmt.Sprintf("%s, DO ID: %d", m.String(), id),
F: func() error { F: func() error {
retry: retry:
resp, err := api.client.Domains.DeleteRecord(ctx, dc.Name, id) resp, err := f()
if err != nil { if err != nil {
if pauseAndRetry(resp) { if pauseAndRetry(resp) {
goto retry goto retry
@ -201,44 +200,43 @@ func (api *digitaloceanProvider) GetZoneRecordsCorrections(dc *models.DomainConf
} }
return err return err
}, },
})
} }
corrections = append(corrections, corr)
} for _, inst := range instructions {
for _, m := range toCreate { switch inst.Type {
req := toReq(m.Desired) case diff2.REPORT:
corr := &models.Correction{ corrections = append(corrections,
Msg: m.String(), &models.Correction{
F: func() error { Msg: inst.MsgsJoined,
retry: })
continue
case diff2.CREATE:
req := toReq(inst.New[0])
addCorrection(inst.MsgsJoined, func() (*godo.Response, error) {
_, resp, err := api.client.Domains.CreateRecord(ctx, dc.Name, req) _, resp, err := api.client.Domains.CreateRecord(ctx, dc.Name, req)
if err != nil { return resp, err
if pauseAndRetry(resp) { })
goto retry
} case diff2.CHANGE:
} id := inst.Old[0].Original.(*godo.DomainRecord).ID
return err req := toReq(inst.New[0])
}, addCorrection(inst.MsgsJoined, func() (*godo.Response, error) {
}
corrections = append(corrections, corr)
}
for _, m := range toModify {
id := m.Existing.Original.(*godo.DomainRecord).ID
req := toReq(m.Desired)
corr := &models.Correction{
Msg: fmt.Sprintf("%s, DO ID: %d", m.String(), id),
F: func() error {
retry:
_, resp, err := api.client.Domains.EditRecord(ctx, dc.Name, id, req) _, resp, err := api.client.Domains.EditRecord(ctx, dc.Name, id, req)
if err != nil { return resp, err
if pauseAndRetry(resp) { })
goto retry
case diff2.DELETE:
id := inst.Old[0].Original.(*godo.DomainRecord).ID
addCorrection(inst.MsgsJoined, func() (*godo.Response, error) {
return api.client.Domains.DeleteRecord(ctx, dc.Name, id)
})
default:
panic(fmt.Sprintf("unhandled inst.Type %s", inst.Type))
} }
} }
return err
},
}
corrections = append(corrections, corr)
}
return corrections, actualChangeCount, nil return corrections, actualChangeCount, nil
} }