1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

NEW FEATURE: diff2: A better "diff" mechanism (#1852)

This commit is contained in:
Tom Limoncelli
2022-12-11 17:28:58 -05:00
committed by GitHub
parent b0f2945510
commit 54fc2e9ce3
40 changed files with 2581 additions and 81 deletions

View File

@@ -230,7 +230,7 @@ func (client *gandiv5Provider) GenerateDomainCorrections(dc *models.DomainConfig
txtutil.SplitSingleLongTxt(dc.Records) // Autosplit long TXT records
var corrections []*models.Correction
if !diff2.EnableDiff2 || true { // Remove "|| true" when diff2 version arrives
if !diff2.EnableDiff2 {
// diff existing vs. current.
differ := diff.New(dc)
@@ -305,6 +305,7 @@ func (client *gandiv5Provider) GenerateDomainCorrections(dc *models.DomainConfig
// First time putting data on this label. Create it.
// We have to create the label one rtype at a time.
ns := recordsToNative(desiredRecords[label], dc.Name)
for _, n := range ns {
msg := strings.Join(msgsForLabel[label], "\n")
domain := dc.Name
@@ -339,7 +340,83 @@ func (client *gandiv5Provider) GenerateDomainCorrections(dc *models.DomainConfig
return corrections, nil
}
// Insert Future diff2 version here.
g := gandi.NewLiveDNSClient(config.Config{
APIKey: client.apikey,
SharingID: client.sharingid,
Debug: client.debug,
})
// Gandi is a "ByLabel" API with the odd exception that changes must be
// done one label:rtype at a time.
instructions, err := diff2.ByLabel(existing, dc, nil)
if err != nil {
return nil, err
}
for _, inst := range instructions {
switch inst.Type {
case diff2.CREATE:
// We have to create the label one rtype at a time.
natives := recordsToNative(inst.New, dc.Name)
for _, n := range natives {
label := inst.Key.NameFQDN
rtype := n.RrsetType
domain := dc.Name
shortname := dnsutil.TrimDomainName(label, dc.Name)
ttl := n.RrsetTTL
values := n.RrsetValues
key := models.RecordKey{NameFQDN: label, Type: rtype}
msg := strings.Join(inst.MsgsByKey[key], "\n")
corrections = append(corrections,
&models.Correction{
Msg: msg,
F: func() error {
res, err := g.CreateDomainRecord(domain, shortname, rtype, ttl, values)
if err != nil {
return fmt.Errorf("%+v: %w", res, err)
}
return nil
},
})
}
case diff2.CHANGE:
msgs := strings.Join(inst.Msgs, "\n")
domain := dc.Name
label := inst.Key.NameFQDN
shortname := dnsutil.TrimDomainName(label, dc.Name)
ns := recordsToNative(inst.New, dc.Name)
corrections = append(corrections,
&models.Correction{
Msg: msgs,
F: func() error {
res, err := g.UpdateDomainRecordsByName(domain, shortname, ns)
if err != nil {
return fmt.Errorf("%+v: %w", res, err)
}
return nil
},
})
case diff2.DELETE:
msgs := strings.Join(inst.Msgs, "\n")
domain := dc.Name
label := inst.Key.NameFQDN
shortname := dnsutil.TrimDomainName(label, dc.Name)
corrections = append(corrections,
&models.Correction{
Msg: msgs,
F: func() error {
err := g.DeleteDomainRecordsByName(domain, shortname)
if err != nil {
return err
}
return nil
},
})
}
}
return corrections, nil
}