2020-12-29 05:07:33 +08:00
|
|
|
package msdns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/diff"
|
2021-03-08 02:19:22 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/txtutil"
|
2020-12-29 05:07:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetDomainCorrections gets existing records, diffs them against existing, and returns corrections.
|
2022-08-12 03:40:13 +08:00
|
|
|
func (client *msdnsProvider) GenerateDomainCorrections(dc *models.DomainConfig, foundRecords models.Records) ([]*models.Correction, error) {
|
2020-12-29 05:07:33 +08:00
|
|
|
|
|
|
|
// Normalize
|
|
|
|
models.PostProcessRecords(foundRecords)
|
2021-03-08 02:19:22 +08:00
|
|
|
txtutil.SplitSingleLongTxt(dc.Records) // Autosplit long TXT records
|
2020-12-29 05:07:33 +08:00
|
|
|
|
|
|
|
differ := diff.New(dc)
|
|
|
|
_, creates, dels, modifications, err := differ.IncrementalDiff(foundRecords)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate changes.
|
|
|
|
corrections := []*models.Correction{}
|
|
|
|
for _, del := range dels {
|
2022-02-12 03:30:45 +08:00
|
|
|
corrections = append(corrections, client.deleteRec(client.dnsserver, dc.Name, del))
|
2020-12-29 05:07:33 +08:00
|
|
|
}
|
|
|
|
for _, cre := range creates {
|
2022-02-12 03:30:45 +08:00
|
|
|
corrections = append(corrections, client.createRec(client.dnsserver, dc.Name, cre)...)
|
2020-12-29 05:07:33 +08:00
|
|
|
}
|
|
|
|
for _, m := range modifications {
|
2022-02-12 03:30:45 +08:00
|
|
|
corrections = append(corrections, client.modifyRec(client.dnsserver, dc.Name, m))
|
2020-12-29 05:07:33 +08:00
|
|
|
}
|
|
|
|
return corrections, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:30:45 +08:00
|
|
|
func (client *msdnsProvider) deleteRec(dnsserver, domainname string, cor diff.Correlation) *models.Correction {
|
2020-12-29 05:07:33 +08:00
|
|
|
rec := cor.Existing
|
|
|
|
return &models.Correction{
|
|
|
|
Msg: cor.String(),
|
|
|
|
F: func() error {
|
2022-02-12 03:30:45 +08:00
|
|
|
return client.shell.RecordDelete(dnsserver, domainname, rec)
|
2020-12-29 05:07:33 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:30:45 +08:00
|
|
|
func (client *msdnsProvider) createRec(dnsserver, domainname string, cre diff.Correlation) []*models.Correction {
|
2020-12-29 05:07:33 +08:00
|
|
|
rec := cre.Desired
|
|
|
|
arr := []*models.Correction{{
|
|
|
|
Msg: cre.String(),
|
|
|
|
F: func() error {
|
2022-02-12 03:30:45 +08:00
|
|
|
return client.shell.RecordCreate(dnsserver, domainname, rec)
|
2020-12-29 05:07:33 +08:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:30:45 +08:00
|
|
|
func (client *msdnsProvider) modifyRec(dnsserver, domainname string, m diff.Correlation) *models.Correction {
|
2020-12-29 05:07:33 +08:00
|
|
|
old, rec := m.Existing, m.Desired
|
|
|
|
return &models.Correction{
|
|
|
|
Msg: m.String(),
|
|
|
|
F: func() error {
|
2022-02-12 03:30:45 +08:00
|
|
|
return client.shell.RecordModify(dnsserver, domainname, old, rec)
|
2020-12-29 05:07:33 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|