mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-01-13 10:58:17 +08:00
302a74b935
Signed-off-by: Jan-Philipp Benecke <jan-philipp@bnck.me>
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package powerdns
|
|
|
|
import (
|
|
"context"
|
|
"github.com/StackExchange/dnscontrol/v3/models"
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/diff2"
|
|
"github.com/mittwald/go-powerdns/apis/zones"
|
|
"github.com/mittwald/go-powerdns/pdnshttp"
|
|
"net/http"
|
|
)
|
|
|
|
// GetNameservers returns the nameservers for a domain.
|
|
func (dsp *powerdnsProvider) GetNameservers(string) ([]*models.Nameserver, error) {
|
|
var r []string
|
|
for _, j := range dsp.nameservers {
|
|
r = append(r, j.Name)
|
|
}
|
|
return models.ToNameservers(r)
|
|
}
|
|
|
|
// GetZoneRecords gets the records of a zone and returns them in RecordConfig format.
|
|
func (dsp *powerdnsProvider) GetZoneRecords(domain string) (models.Records, error) {
|
|
zone, err := dsp.client.Zones().GetZone(context.Background(), dsp.ServerName, domain)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
curRecords := models.Records{}
|
|
// loop over grouped records by type, called RRSet
|
|
for _, rrset := range zone.ResourceRecordSets {
|
|
if rrset.Type == "SOA" {
|
|
continue
|
|
}
|
|
// loop over single records of this group and create records
|
|
for _, pdnsRecord := range rrset.Records {
|
|
r, err := toRecordConfig(domain, pdnsRecord, rrset.TTL, rrset.Name, rrset.Type)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
curRecords = append(curRecords, r)
|
|
}
|
|
}
|
|
|
|
return curRecords, nil
|
|
}
|
|
|
|
// GetDomainCorrections returns a list of corrections to update a domain.
|
|
func (dsp *powerdnsProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
|
|
// get current zone records
|
|
existing, err := dsp.GetZoneRecords(dc.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// post-process records
|
|
if err := dc.Punycode(); err != nil {
|
|
return nil, err
|
|
}
|
|
models.PostProcessRecords(existing)
|
|
|
|
var corrections []*models.Correction
|
|
if !diff2.EnableDiff2 {
|
|
corrections, err = dsp.getDiff1DomainCorrections(dc, existing)
|
|
} else {
|
|
corrections, err = dsp.getDiff2DomainCorrections(dc, existing)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// DNSSec corrections
|
|
dnssecCorrections, err := dsp.getDNSSECCorrections(dc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return append(corrections, dnssecCorrections...), nil
|
|
}
|
|
|
|
// EnsureZoneExists creates a zone if it does not exist
|
|
func (dsp *powerdnsProvider) EnsureZoneExists(domain string) error {
|
|
if _, err := dsp.client.Zones().GetZone(context.Background(), dsp.ServerName, canonical(domain)); err != nil {
|
|
if e, ok := err.(pdnshttp.ErrUnexpectedStatus); ok {
|
|
if e.StatusCode != http.StatusNotFound {
|
|
return err
|
|
}
|
|
}
|
|
} else { // zone seems to exist
|
|
return nil
|
|
}
|
|
|
|
_, err := dsp.client.Zones().CreateZone(context.Background(), dsp.ServerName, zones.Zone{
|
|
Name: canonical(domain),
|
|
Type: zones.ZoneTypeZone,
|
|
DNSSec: dsp.DNSSecOnCreate,
|
|
Nameservers: dsp.DefaultNS,
|
|
})
|
|
return err
|
|
}
|