2020-12-29 05:07:33 +08:00
|
|
|
package msdns
|
|
|
|
|
|
|
|
import (
|
2023-02-03 05:53:37 +08:00
|
|
|
"fmt"
|
|
|
|
|
2023-05-21 01:21:45 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v4/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/v4/pkg/diff2"
|
|
|
|
"github.com/StackExchange/dnscontrol/v4/pkg/txtutil"
|
2020-12-29 05:07:33 +08:00
|
|
|
)
|
|
|
|
|
2023-04-15 03:22:23 +08:00
|
|
|
// GetZoneRecordsCorrections returns a list of corrections that will turn existing records into dc.Records.
|
|
|
|
func (client *msdnsProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, foundRecords models.Records) ([]*models.Correction, error) {
|
2023-10-23 01:56:13 +08:00
|
|
|
var corrections []*models.Correction
|
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
|
|
|
|
2023-02-03 05:53:37 +08:00
|
|
|
changes, err := diff2.ByRecord(foundRecords, dc, nil)
|
2023-01-02 23:17:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-12-29 05:07:33 +08:00
|
|
|
}
|
|
|
|
|
2023-02-03 05:53:37 +08:00
|
|
|
var corr *models.Correction
|
|
|
|
for _, change := range changes {
|
|
|
|
msgsJoined := change.MsgsJoined
|
|
|
|
switch change.Type {
|
|
|
|
case diff2.REPORT:
|
|
|
|
corr = &models.Correction{Msg: msgsJoined}
|
|
|
|
case diff2.CREATE:
|
|
|
|
newrec := change.New[0]
|
|
|
|
corr = &models.Correction{
|
|
|
|
Msg: msgsJoined,
|
|
|
|
F: func() error {
|
|
|
|
return client.createOneRecord(client.dnsserver, dc.Name, newrec)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case diff2.CHANGE:
|
|
|
|
oldrec := change.Old[0]
|
|
|
|
newrec := change.New[0]
|
2023-03-31 21:08:26 +08:00
|
|
|
var f func(dnsserver string, zonename string, oldrec *models.RecordConfig, newrec *models.RecordConfig) error
|
|
|
|
if change.HintOnlyTTL && change.HintRecordSetLen1 {
|
|
|
|
// If we're only changing the TTL, and there is exactly one
|
|
|
|
// record of type oldrec.Type at this label, then we can do the
|
|
|
|
// TTL change in one command instead of deleting and re-creating
|
|
|
|
// the record.
|
|
|
|
f = client.modifyRecordTTL
|
|
|
|
} else {
|
|
|
|
f = client.modifyOneRecord
|
|
|
|
}
|
2023-02-03 05:53:37 +08:00
|
|
|
corr = &models.Correction{
|
|
|
|
Msg: msgsJoined,
|
|
|
|
F: func() error {
|
2023-03-31 21:08:26 +08:00
|
|
|
return f(client.dnsserver, dc.Name, oldrec, newrec)
|
2023-02-03 05:53:37 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case diff2.DELETE:
|
|
|
|
oldrec := change.Old[0]
|
|
|
|
corr = &models.Correction{
|
|
|
|
Msg: msgsJoined,
|
|
|
|
F: func() error {
|
|
|
|
return client.deleteOneRecord(client.dnsserver, dc.Name, oldrec)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unhandled change.Type %s", change.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
corrections = append(corrections, corr)
|
2023-01-02 23:17:44 +08:00
|
|
|
}
|
2023-02-03 05:53:37 +08:00
|
|
|
|
2022-12-12 04:02:58 +08:00
|
|
|
return corrections, nil
|
2023-02-03 05:53:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *msdnsProvider) deleteOneRecord(dnsserver, zonename string, oldrec *models.RecordConfig) error {
|
|
|
|
return client.shell.RecordDelete(dnsserver, zonename, oldrec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *msdnsProvider) createOneRecord(dnsserver, zonename string, newrec *models.RecordConfig) error {
|
|
|
|
return client.shell.RecordCreate(dnsserver, zonename, newrec)
|
|
|
|
}
|
2023-01-02 23:17:44 +08:00
|
|
|
|
2023-02-03 05:53:37 +08:00
|
|
|
func (client *msdnsProvider) modifyOneRecord(dnsserver, zonename string, oldrec, newrec *models.RecordConfig) error {
|
|
|
|
return client.shell.RecordModify(dnsserver, zonename, oldrec, newrec)
|
2020-12-29 05:07:33 +08:00
|
|
|
}
|
|
|
|
|
2023-03-31 21:08:26 +08:00
|
|
|
func (client *msdnsProvider) modifyRecordTTL(dnsserver, zonename string, oldrec, newrec *models.RecordConfig) error {
|
|
|
|
return client.shell.RecordModifyTTL(dnsserver, zonename, oldrec, newrec.TTL)
|
|
|
|
}
|