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

Refactor RecordConfig: Add getters/setters (#314)

* Replace RecordConfig.Name and .NameFQDN with getters and setters.
* Replace RecordConfig.Target with getters and setters.
* Eliminate the CombinedTarget concept.
* Add RecordConfig.PopulateFromString to reduce code in all providers.
* encode and decode name.com txt records (#315)
* Replace fmt.Errorf with errors.Errorf
This commit is contained in:
Tom Limoncelli
2018-02-15 12:02:50 -05:00
committed by GitHub
parent 324b1ea930
commit de4455942b
37 changed files with 1237 additions and 965 deletions

View File

@@ -3,6 +3,7 @@ package gandi
import (
"fmt"
"github.com/pkg/errors"
gandiclient "github.com/prasmussen/gandi-api/client"
gandidomain "github.com/prasmussen/gandi-api/domain"
gandinameservers "github.com/prasmussen/gandi-api/domain/nameservers"
@@ -12,7 +13,6 @@ import (
gandioperation "github.com/prasmussen/gandi-api/operation"
"github.com/StackExchange/dnscontrol/models"
"github.com/miekg/dns/dnsutil"
)
// fetchDomainList gets list of domains for account. Cache ids for easy lookup.
@@ -58,11 +58,32 @@ func (c *GandiApi) getZoneRecords(zoneid int64, origin string) ([]*models.Record
}
rcs := make([]*models.RecordConfig, 0, len(recs))
for _, r := range recs {
rcs = append(rcs, convert(r, origin))
rcs = append(rcs, nativeToRecord(r, origin))
}
return rcs, nil
}
// convert takes a DNS record from Gandi and returns our native RecordConfig format.
func nativeToRecord(r *gandirecord.RecordInfo, origin string) *models.RecordConfig {
rc := &models.RecordConfig{
//NameFQDN: dnsutil.AddOrigin(r.Name, origin),
//Name: r.Name,
//Type: r.Type,
TTL: uint32(r.Ttl),
Original: r,
//Target: r.Value,
}
rc.SetLabel(r.Name, origin)
switch rtype := r.Type; rtype {
default: // "A", "AAAA", "CAA", "NS", "CNAME", "MX", "PTR", "SRV", "TXT"
if err := rc.PopulateFromString(rtype, r.Value, origin); err != nil {
panic(errors.Wrap(err, "unparsable record received from gandi"))
}
}
return rc
}
// listZones retrieves the list of zones.
func (c *GandiApi) listZones() ([]*gandizone.ZoneInfoBase, error) {
gc := gandiclient.New(c.ApiKey, gandiclient.Production)
@@ -150,7 +171,6 @@ func (c *GandiApi) createGandiZone(domainname string, zoneID int64, records []ga
if err != nil {
return err
}
// fmt.Println("ZONEINFO:", zoneinfo)
zoneID, err = c.getEditableZone(domainname, zoneinfo)
if err != nil {
return err
@@ -180,44 +200,3 @@ func (c *GandiApi) createGandiZone(domainname string, zoneID int64, records []ga
return nil
}
// convert takes a DNS record from Gandi and returns our native RecordConfig format.
func convert(r *gandirecord.RecordInfo, origin string) *models.RecordConfig {
rc := &models.RecordConfig{
NameFQDN: dnsutil.AddOrigin(r.Name, origin),
Name: r.Name,
Type: r.Type,
Original: r,
Target: r.Value,
TTL: uint32(r.Ttl),
}
switch r.Type {
case "A", "AAAA", "NS", "CNAME", "PTR":
// no-op
case "TXT":
rc.SetTxtParse(r.Value)
case "CAA":
var err error
rc.CaaTag, rc.CaaFlag, rc.Target, err = models.SplitCombinedCaaValue(r.Value)
if err != nil {
panic(fmt.Sprintf("gandi.convert bad caa value format: %#v (%s)", r.Value, err))
}
case "SRV":
var err error
rc.SrvPriority, rc.SrvWeight, rc.SrvPort, rc.Target, err = models.SplitCombinedSrvValue(r.Value)
if err != nil {
panic(fmt.Sprintf("gandi.convert bad srv value format: %#v (%s)", r.Value, err))
}
case "MX":
var err error
rc.MxPreference, rc.Target, err = models.SplitCombinedMxValue(r.Value)
if err != nil {
panic(fmt.Sprintf("gandi.convert bad mx value format: %#v", r.Value))
}
default:
panic(fmt.Sprintf("gandi.convert unimplemented rtype %v", r.Type))
// We panic so that we quickly find any switch statements
// that have not been updated for a new RR type.
}
return rc
}