2018-02-16 01:02:50 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
"fmt"
|
2018-02-16 01:02:50 +08:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PopulateFromString populates a RecordConfig given a type and string.
|
|
|
|
// Many providers give all the parameters of a resource record in one big
|
|
|
|
// string (all the parameters of an MX, SRV, CAA, etc). Rather than have
|
|
|
|
// each provider rewrite this code many times, here's a helper function to use.
|
|
|
|
//
|
2020-11-14 05:32:40 +08:00
|
|
|
// If this doesn't work for all rtypes, process the special cases then
|
|
|
|
// call this for the remainder.
|
2018-02-16 01:02:50 +08:00
|
|
|
func (r *RecordConfig) PopulateFromString(rtype, contents, origin string) error {
|
|
|
|
if r.Type != "" && r.Type != rtype {
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
panic(fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, r.Type))
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
|
|
|
switch r.Type = rtype; rtype { // #rtype_variations
|
|
|
|
case "A":
|
|
|
|
ip := net.ParseIP(contents)
|
|
|
|
if ip == nil || ip.To4() == nil {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("invalid IP in A record: %s", contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
|
|
|
return r.SetTargetIP(ip) // Reformat to canonical form.
|
|
|
|
case "AAAA":
|
|
|
|
ip := net.ParseIP(contents)
|
|
|
|
if ip == nil || ip.To16() == nil {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("invalid IP in AAAA record: %s", contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
|
|
|
return r.SetTargetIP(ip) // Reformat to canonical form.
|
|
|
|
case "ANAME", "CNAME", "NS", "PTR":
|
|
|
|
return r.SetTarget(contents)
|
|
|
|
case "CAA":
|
|
|
|
return r.SetTargetCAAString(contents)
|
2020-05-30 22:40:21 +08:00
|
|
|
case "DS":
|
|
|
|
return r.SetTargetDSString(contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
case "MX":
|
|
|
|
return r.SetTargetMXString(contents)
|
2019-04-18 04:13:49 +08:00
|
|
|
case "NAPTR":
|
|
|
|
return r.SetTargetNAPTRString(contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
case "SRV":
|
|
|
|
return r.SetTargetSRVString(contents)
|
2020-02-24 02:58:49 +08:00
|
|
|
case "SOA":
|
|
|
|
return r.SetTargetSOAString(contents)
|
2019-01-29 06:26:20 +08:00
|
|
|
case "SSHFP":
|
|
|
|
return r.SetTargetSSHFPString(contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
case "TLSA":
|
|
|
|
return r.SetTargetTLSAString(contents)
|
|
|
|
case "TXT":
|
|
|
|
return r.SetTargetTXTString(contents)
|
|
|
|
default:
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)",
|
2018-02-16 01:02:50 +08:00
|
|
|
rtype, contents, origin)
|
|
|
|
}
|
|
|
|
}
|