mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-09-20 06:46:19 +08:00
POWERDNS: Improve preformance by using new batched add/remove functions in 0.6.6 API (#3105)
Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
parent
74826d3430
commit
9178a837de
|
@ -3,9 +3,11 @@ package powerdns
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/StackExchange/dnscontrol/v4/models"
|
||||
"github.com/StackExchange/dnscontrol/v4/pkg/diff2"
|
||||
"github.com/fatih/color"
|
||||
"github.com/mittwald/go-powerdns/apis/zones"
|
||||
)
|
||||
|
||||
|
@ -16,6 +18,14 @@ func (dsp *powerdnsProvider) getDiff2DomainCorrections(dc *models.DomainConfig,
|
|||
}
|
||||
|
||||
var corrections []*models.Correction
|
||||
var changeMsgs []string
|
||||
var rrChangeSets []zones.ResourceRecordSet
|
||||
var deleteMsgs []string
|
||||
var rrDeleteSets []zones.ResourceRecordSet
|
||||
|
||||
// for pretty alignment, add an empty string
|
||||
changeMsgs = append(changeMsgs, color.YellowString("± BATCHED CHANGE/CREATEs for %s", dc.Name))
|
||||
deleteMsgs = append(deleteMsgs, color.RedString("- BATCHED DELETEs for %s", dc.Name))
|
||||
|
||||
for _, change := range changes {
|
||||
labelName := canonical(change.Key.NameFQDN)
|
||||
|
@ -28,30 +38,43 @@ func (dsp *powerdnsProvider) getDiff2DomainCorrections(dc *models.DomainConfig,
|
|||
labelTTL := int(change.New[0].TTL)
|
||||
records := buildRecordList(change)
|
||||
|
||||
corrections = append(corrections, &models.Correction{
|
||||
Msg: change.MsgsJoined,
|
||||
F: func() error {
|
||||
return dsp.client.Zones().AddRecordSetToZone(context.Background(), dsp.ServerName, canonical(dc.Name), zones.ResourceRecordSet{
|
||||
Name: labelName,
|
||||
Type: labelType,
|
||||
TTL: labelTTL,
|
||||
Records: records,
|
||||
ChangeType: zones.ChangeTypeReplace,
|
||||
})
|
||||
},
|
||||
rrChangeSets = append(rrChangeSets, zones.ResourceRecordSet{
|
||||
Name: labelName,
|
||||
Type: labelType,
|
||||
TTL: labelTTL,
|
||||
Records: records,
|
||||
// ChangeType is not needed since zone API sets it when calling Add
|
||||
})
|
||||
changeMsgs = append(changeMsgs, change.MsgsJoined)
|
||||
case diff2.DELETE:
|
||||
corrections = append(corrections, &models.Correction{
|
||||
Msg: change.MsgsJoined,
|
||||
F: func() error {
|
||||
return dsp.client.Zones().RemoveRecordSetFromZone(context.Background(), dsp.ServerName, canonical(dc.Name), labelName, labelType)
|
||||
},
|
||||
rrDeleteSets = append(rrDeleteSets, zones.ResourceRecordSet{
|
||||
Name: labelName,
|
||||
Type: labelType,
|
||||
// ChangeType is not needed since zone API sets it when calling Remove
|
||||
})
|
||||
deleteMsgs = append(deleteMsgs, change.MsgsJoined)
|
||||
default:
|
||||
panic(fmt.Sprintf("unhandled change.Type %s", change.Type))
|
||||
}
|
||||
}
|
||||
|
||||
// only append a Correction if there are any, otherwise causes an error when sending an empty rrset
|
||||
if len(rrDeleteSets) > 0 {
|
||||
corrections = append(corrections, &models.Correction{
|
||||
Msg: strings.Join(deleteMsgs, "\n"),
|
||||
F: func() error {
|
||||
return dsp.client.Zones().RemoveRecordSetsFromZone(context.Background(), dsp.ServerName, canonical(dc.Name), rrDeleteSets)
|
||||
},
|
||||
})
|
||||
}
|
||||
if len(rrChangeSets) > 0 {
|
||||
corrections = append(corrections, &models.Correction{
|
||||
Msg: strings.Join(changeMsgs, "\n"),
|
||||
F: func() error {
|
||||
return dsp.client.Zones().AddRecordSetsToZone(context.Background(), dsp.ServerName, canonical(dc.Name), rrChangeSets)
|
||||
},
|
||||
})
|
||||
}
|
||||
return corrections, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue