mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
DESEC: Adopt diff2 in compatibility mode (#1876)
This commit is contained in:
@ -166,88 +166,88 @@ func PrepDesiredRecords(dc *models.DomainConfig, minTTL uint32) {
|
|||||||
func (c *desecProvider) GenerateDomainCorrections(dc *models.DomainConfig, existing models.Records) ([]*models.Correction, error) {
|
func (c *desecProvider) GenerateDomainCorrections(dc *models.DomainConfig, existing models.Records) ([]*models.Correction, error) {
|
||||||
|
|
||||||
var corrections []*models.Correction
|
var corrections []*models.Correction
|
||||||
if !diff2.EnableDiff2 || true { // Remove "|| true" when diff2 version arrives
|
var err error
|
||||||
|
var keysToUpdate map[models.RecordKey][]string
|
||||||
|
if !diff2.EnableDiff2 {
|
||||||
// diff existing vs. current.
|
// diff existing vs. current.
|
||||||
differ := diff.New(dc)
|
differ := diff.New(dc)
|
||||||
keysToUpdate, err := differ.ChangedGroups(existing)
|
keysToUpdate, err = differ.ChangedGroups(existing)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, err
|
differ := diff.NewCompat(dc)
|
||||||
}
|
keysToUpdate, err = differ.ChangedGroups(existing)
|
||||||
if len(keysToUpdate) == 0 {
|
}
|
||||||
return nil, nil
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
desiredRecords := dc.Records.GroupedByKey()
|
if len(keysToUpdate) == 0 {
|
||||||
var rrs []resourceRecord
|
return nil, nil
|
||||||
buf := &bytes.Buffer{}
|
}
|
||||||
// For any key with an update, delete or replace those records.
|
|
||||||
for label := range keysToUpdate {
|
desiredRecords := dc.Records.GroupedByKey()
|
||||||
if _, ok := desiredRecords[label]; !ok {
|
var rrs []resourceRecord
|
||||||
//we could not find this RecordKey in the desiredRecords
|
buf := &bytes.Buffer{}
|
||||||
//this means it must be deleted
|
// For any key with an update, delete or replace those records.
|
||||||
for i, msg := range keysToUpdate[label] {
|
for label := range keysToUpdate {
|
||||||
if i == 0 {
|
if _, ok := desiredRecords[label]; !ok {
|
||||||
rc := resourceRecord{}
|
//we could not find this RecordKey in the desiredRecords
|
||||||
rc.Type = label.Type
|
//this means it must be deleted
|
||||||
rc.Records = make([]string, 0) // empty array of records should delete this rrset
|
for i, msg := range keysToUpdate[label] {
|
||||||
rc.TTL = 3600
|
if i == 0 {
|
||||||
shortname := dnsutil.TrimDomainName(label.NameFQDN, dc.Name)
|
rc := resourceRecord{}
|
||||||
if shortname == "@" {
|
rc.Type = label.Type
|
||||||
shortname = ""
|
rc.Records = make([]string, 0) // empty array of records should delete this rrset
|
||||||
}
|
rc.TTL = 3600
|
||||||
rc.Subname = shortname
|
shortname := dnsutil.TrimDomainName(label.NameFQDN, dc.Name)
|
||||||
fmt.Fprintln(buf, msg)
|
if shortname == "@" {
|
||||||
rrs = append(rrs, rc)
|
shortname = ""
|
||||||
} else {
|
|
||||||
//just add the message
|
|
||||||
fmt.Fprintln(buf, msg)
|
|
||||||
}
|
}
|
||||||
|
rc.Subname = shortname
|
||||||
|
fmt.Fprintln(buf, msg)
|
||||||
|
rrs = append(rrs, rc)
|
||||||
|
} else {
|
||||||
|
//just add the message
|
||||||
|
fmt.Fprintln(buf, msg)
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
//it must be an update or create, both can be done with the same api call.
|
} else {
|
||||||
ns := recordsToNative(desiredRecords[label], dc.Name)
|
//it must be an update or create, both can be done with the same api call.
|
||||||
if len(ns) > 1 {
|
ns := recordsToNative(desiredRecords[label], dc.Name)
|
||||||
panic("we got more than one resource record to create / modify")
|
if len(ns) > 1 {
|
||||||
}
|
panic("we got more than one resource record to create / modify")
|
||||||
for i, msg := range keysToUpdate[label] {
|
}
|
||||||
if i == 0 {
|
for i, msg := range keysToUpdate[label] {
|
||||||
rrs = append(rrs, ns[0])
|
if i == 0 {
|
||||||
fmt.Fprintln(buf, msg)
|
rrs = append(rrs, ns[0])
|
||||||
} else {
|
fmt.Fprintln(buf, msg)
|
||||||
//noop just for printing the additional messages
|
} else {
|
||||||
fmt.Fprintln(buf, msg)
|
//noop just for printing the additional messages
|
||||||
}
|
fmt.Fprintln(buf, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
msg := fmt.Sprintf("Changes:\n%s", buf)
|
|
||||||
corrections = append(corrections,
|
|
||||||
&models.Correction{
|
|
||||||
Msg: msg,
|
|
||||||
F: func() error {
|
|
||||||
rc := rrs
|
|
||||||
err := c.upsertRR(rc, dc.Name)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// NB(tlim): This sort is just to make updates look pretty. It is
|
|
||||||
// cosmetic. The risk here is that there may be some updates that
|
|
||||||
// require a specific order (for example a delete before an add).
|
|
||||||
// However the code doesn't seem to have such situation. All tests
|
|
||||||
// pass. That said, if this breaks anything, the easiest fix might
|
|
||||||
// be to just remove the sort.
|
|
||||||
sort.Slice(corrections, func(i, j int) bool { return diff.CorrectionLess(corrections, i, j) })
|
|
||||||
|
|
||||||
return corrections, nil
|
|
||||||
}
|
}
|
||||||
|
msg := fmt.Sprintf("Changes:\n%s", buf)
|
||||||
|
corrections = append(corrections,
|
||||||
|
&models.Correction{
|
||||||
|
Msg: msg,
|
||||||
|
F: func() error {
|
||||||
|
rc := rrs
|
||||||
|
err := c.upsertRR(rc, dc.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
// Insert Future diff2 version here.
|
// NB(tlim): This sort is just to make updates look pretty. It is
|
||||||
|
// cosmetic. The risk here is that there may be some updates that
|
||||||
|
// require a specific order (for example a delete before an add).
|
||||||
|
// However the code doesn't seem to have such situation. All tests
|
||||||
|
// pass. That said, if this breaks anything, the easiest fix might
|
||||||
|
// be to just remove the sort.
|
||||||
|
sort.Slice(corrections, func(i, j int) bool { return diff.CorrectionLess(corrections, i, j) })
|
||||||
|
|
||||||
// Insert Future diff2 version here.
|
|
||||||
return corrections, nil
|
return corrections, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user