dnscontrol/models/t_parse.go

77 lines
2.5 KiB
Go
Raw Normal View History

package models
import (
"fmt"
"net"
)
// PopulateFromString populates a RecordConfig given a type and string. Many
// providers give all the parameters of a resource record in one big string.
// This helper function lets you not re-invent the wheel.
//
// NOTE: You almost always want to special-case TXT records. Every provider
// seems to quote them differently.
//
// Recommended calling convention: Process the exceptions first, then use the
// function for everything else.
// var err error
// switch rType {
// case "MX":
// // MX priority in a separate field.
// if err := rc.SetTargetMX(cr.Priority, target); err != nil {
// return nil, fmt.Errorf("unparsable MX record received from cloudflare: %w", err)
// }
// case "TXT":
// // TXT records are stored verbatim; no quoting/escaping to parse.
// err = rc.SetTargetTXT(target)
// // ProTip: Use rc.SetTargetTXTs(manystrings) if the API or parser returns a list of substrings.
// default:
// err = rec.PopulateFromString(rType, target, origin)
// }
// if err != nil {
// return nil, fmt.Errorf("unparsable record received from CHANGE_TO_PROVDER_NAME: %w", err)
// }
//
func (rc *RecordConfig) PopulateFromString(rtype, contents, origin string) error {
if rc.Type != "" && rc.Type != rtype {
panic(fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type))
}
switch rc.Type = rtype; rtype { // #rtype_variations
case "A":
ip := net.ParseIP(contents)
if ip == nil || ip.To4() == nil {
return fmt.Errorf("invalid IP in A record: %s", contents)
}
return rc.SetTargetIP(ip) // Reformat to canonical form.
case "AAAA":
ip := net.ParseIP(contents)
if ip == nil || ip.To16() == nil {
return fmt.Errorf("invalid IP in AAAA record: %s", contents)
}
return rc.SetTargetIP(ip) // Reformat to canonical form.
case "AKAMAICDN", "ALIAS", "ANAME", "CNAME", "NS", "PTR":
return rc.SetTarget(contents)
case "CAA":
return rc.SetTargetCAAString(contents)
case "DS":
return rc.SetTargetDSString(contents)
case "MX":
return rc.SetTargetMXString(contents)
2019-04-18 04:13:49 +08:00
case "NAPTR":
return rc.SetTargetNAPTRString(contents)
case "SOA":
return rc.SetTargetSOAString(contents)
MAINT: Sort various lists (#1433) * maint: sort lists in build/generate/featureMatrix.go * maint: sort lists in docs/byo-secrets.md * maint: sort lists in models/record.go * maint: sort lists in pkg/normalize/validate.go * maint: sort lists in providers/activedir/activedirProvider.go * maint: sort lists in providers/akamaiedgedns/akamaiEdgeDnsProvider.go * maint: sort lists in providers/axfrddns/axfrddnsProvider.go * maint: sort lists in providers/azuredns/azureDnsProvider.go * maint: sort lists in providers/cloudflare/cloudflareProvider.go * maint: sort lists in providers/cloudns/cloudnsProvider.go * maint: sort lists in providers/desec/desecProvider.go * maint: sort lists in providers/digitalocean/digitaloceanProvider.go * maint: sort lists in providers/dnsimple/dnsimpleProvider.go * maint: sort lists in providers/dnsmadeeasy/dnsMadeEasyProvider.go * maint: sort lists in providers/exoscale/exoscaleProvider.go * maint: sort lists in providers/gandiv5/gandi_v5Provider.go * maint: sort lists in providers/hedns/hednsProvider.go * maint: sort lists in providers/hetzner/hetznerProvider.go * maint: sort lists in providers/hexonet/hexonetProvider.go * maint: sort lists in providers/inwx/inwxProvider.go * maint: sort lists in providers/linode/linodeProvider.go * maint: sort lists in providers/namecheap/namecheapProvider.go * maint: sort lists in providers/namedotcom/namedotcomProvider.go * maint: sort lists in providers/netcup/netcupProvider.go * maint: sort lists in providers/ns1/ns1Provider.go * maint: sort lists in providers/octodns/octodnsProvider.go * maint: sort lists in providers/oracle/oracleProvider.go * maint: sort lists in providers/ovh/ovhProvider.go * maint: sort lists in providers/packetframe/packetframeProvider.go * maint: sort lists in providers/powerdns/powerdnsProvider.go * maint: sort lists in providers/route53/route53Provider.go * maint: sort lists in providers/vultr/vultrProvider.go * Update go:build pragmas for windows * More sorting * go generate
2022-03-03 00:19:15 +08:00
case "SPF", "TXT":
return rc.SetTargetTXTString(contents)
case "SRV":
return rc.SetTargetSRVString(contents)
case "SSHFP":
return rc.SetTargetSSHFPString(contents)
case "TLSA":
return rc.SetTargetTLSAString(contents)
default:
return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)",
rtype, contents, origin)
}
}