mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
GetNameservers is inconsistent across providers (#655)
* Warn if GetNameservers returns FQDN+dot strings * Simplify logic that covers for the inconsistency * Fix azuredns, gcloud, bind, route53 * Clean up cloudflare, digitalocean, dnsimple, gandi_v5, namedotcom
This commit is contained in:
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -49,6 +50,7 @@ type DNSProviderConfig struct {
|
||||
// Nameserver describes a nameserver.
|
||||
type Nameserver struct {
|
||||
Name string `json:"name"` // Normalized to a FQDN with NO trailing "."
|
||||
// NB(tlim): DomainConfig.Nameservers are stored WITH a trailing "." (Sorry!)
|
||||
}
|
||||
|
||||
func (n *Nameserver) String() string {
|
||||
@@ -56,6 +58,8 @@ func (n *Nameserver) String() string {
|
||||
}
|
||||
|
||||
// StringsToNameservers constructs a list of *Nameserver structs using a list of FQDNs.
|
||||
// Deprecated. Please use ToNameservers, or maybe ToNameserversStripTD instead.
|
||||
// See https://github.com/StackExchange/dnscontrol/issues/491
|
||||
func StringsToNameservers(nss []string) []*Nameserver {
|
||||
nservers := []*Nameserver{}
|
||||
for _, ns := range nss {
|
||||
@@ -64,7 +68,37 @@ func StringsToNameservers(nss []string) []*Nameserver {
|
||||
return nservers
|
||||
}
|
||||
|
||||
// NameserversToStrings constructs a list of lists from *Nameserver structs
|
||||
// ToNameservers turns a list of strings into a list of Nameservers.
|
||||
// It is an error if any string has a trailing dot. Either remove the
|
||||
// trailing dot before you call this or (much preferred) use ToNameserversStripTD.
|
||||
func ToNameservers(nss []string) ([]*Nameserver, error) {
|
||||
nservers := []*Nameserver{}
|
||||
for _, ns := range nss {
|
||||
if strings.HasSuffix(ns, ".") {
|
||||
return nil, fmt.Errorf("provider code leaves trailing dot on nameserver")
|
||||
// If you see this error, maybe the provider should call
|
||||
// ToNameserversStripTD instead.
|
||||
}
|
||||
nservers = append(nservers, &Nameserver{Name: ns})
|
||||
}
|
||||
return nservers, nil
|
||||
}
|
||||
|
||||
// ToNameserversStripTD is like ToNameservers but strips the trailing
|
||||
// dot from each item. It is an error if there is no trailing dot.
|
||||
func ToNameserversStripTD(nss []string) ([]*Nameserver, error) {
|
||||
nservers := []*Nameserver{}
|
||||
for _, ns := range nss {
|
||||
if !strings.HasSuffix(ns, ".") {
|
||||
return nil, fmt.Errorf("provider code already removed nameserver trailing dot (%v)", ns)
|
||||
// If you see this error, maybe the provider should call ToNameservers instead.
|
||||
}
|
||||
nservers = append(nservers, &Nameserver{Name: ns[0 : len(ns)-1]})
|
||||
}
|
||||
return nservers, nil
|
||||
}
|
||||
|
||||
// NameserversToStrings constructs a list of strings from *Nameserver structs
|
||||
func NameserversToStrings(nss []*Nameserver) (s []string) {
|
||||
for _, ns := range nss {
|
||||
s = append(s, ns.Name)
|
||||
|
||||
Reference in New Issue
Block a user