2021-03-09 09:14:30 +08:00
|
|
|
package gandiv5
|
2020-01-21 03:13:32 +08:00
|
|
|
|
|
|
|
// Convert the provider's native record description to models.RecordConfig.
|
|
|
|
|
|
|
|
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"
|
2022-07-22 02:30:26 +08:00
|
|
|
|
2020-04-15 04:47:30 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v3/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/printer"
|
2022-08-15 08:46:56 +08:00
|
|
|
"github.com/go-gandi/go-gandi/livedns"
|
2020-01-21 03:13:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// nativeToRecord takes a DNS record from Gandi and returns a native RecordConfig struct.
|
2022-07-22 02:30:26 +08:00
|
|
|
func nativeToRecords(n livedns.DomainRecord, origin string) (rcs []*models.RecordConfig, err error) {
|
2020-01-21 03:13:32 +08:00
|
|
|
|
|
|
|
// Gandi returns all the values for a given label/rtype pair in each
|
|
|
|
// livedns.DomainRecord. In other words, if there are multiple A
|
|
|
|
// records for a label, all the IP addresses are listed in
|
|
|
|
// n.RrsetValues rather than having many livedns.DomainRecord's.
|
|
|
|
// We must split them out into individual records, one for each value.
|
|
|
|
for _, value := range n.RrsetValues {
|
|
|
|
rc := &models.RecordConfig{
|
|
|
|
TTL: uint32(n.RrsetTTL),
|
|
|
|
Original: n,
|
|
|
|
}
|
|
|
|
rc.SetLabel(n.RrsetName, origin)
|
2022-07-22 02:30:26 +08:00
|
|
|
|
2020-01-21 03:13:32 +08:00
|
|
|
switch rtype := n.RrsetType; rtype {
|
2020-03-01 22:36:12 +08:00
|
|
|
case "ALIAS":
|
|
|
|
rc.Type = "ALIAS"
|
2022-07-22 02:30:26 +08:00
|
|
|
err = rc.SetTarget(value)
|
|
|
|
case "TXT":
|
|
|
|
err = rc.SetTargetTXTfromRFC1035Quoted(value)
|
|
|
|
default:
|
|
|
|
err = rc.PopulateFromString(rtype, value, origin)
|
2020-01-21 03:13:32 +08:00
|
|
|
}
|
2022-07-22 02:30:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unparsable record received from gandi: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-01-21 03:13:32 +08:00
|
|
|
rcs = append(rcs, rc)
|
|
|
|
}
|
|
|
|
|
2022-07-22 02:30:26 +08:00
|
|
|
return rcs, nil
|
2020-01-21 03:13:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func recordsToNative(rcs []*models.RecordConfig, origin string) []livedns.DomainRecord {
|
|
|
|
// Take a list of RecordConfig and return an equivalent list of ZoneRecords.
|
|
|
|
// Gandi requires one ZoneRecord for each label:key tuple, therefore we
|
|
|
|
// might collapse many RecordConfig into one ZoneRecord.
|
|
|
|
|
|
|
|
var keys = map[models.RecordKey]*livedns.DomainRecord{}
|
|
|
|
var zrs []livedns.DomainRecord
|
|
|
|
|
|
|
|
for _, r := range rcs {
|
|
|
|
label := r.GetLabel()
|
|
|
|
if label == "@" {
|
|
|
|
label = origin
|
|
|
|
}
|
|
|
|
key := r.Key()
|
|
|
|
|
|
|
|
if zr, ok := keys[key]; !ok {
|
|
|
|
// Allocate a new ZoneRecord:
|
|
|
|
zr := livedns.DomainRecord{
|
|
|
|
RrsetType: r.Type,
|
|
|
|
RrsetTTL: int(r.TTL),
|
|
|
|
RrsetName: label,
|
|
|
|
RrsetValues: []string{r.GetTargetCombined()},
|
|
|
|
}
|
2021-11-02 03:41:37 +08:00
|
|
|
keys[key] = &zr
|
2020-01-21 03:13:32 +08:00
|
|
|
} else {
|
|
|
|
zr.RrsetValues = append(zr.RrsetValues, r.GetTargetCombined())
|
|
|
|
|
|
|
|
if r.TTL != uint32(zr.RrsetTTL) {
|
|
|
|
printer.Warnf("All TTLs for a rrset (%v) must be the same. Using smaller of %v and %v.\n", key, r.TTL, zr.RrsetTTL)
|
|
|
|
if r.TTL < uint32(zr.RrsetTTL) {
|
|
|
|
zr.RrsetTTL = int(r.TTL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 03:41:37 +08:00
|
|
|
for _, zr := range keys {
|
|
|
|
zrs = append(zrs, *zr)
|
|
|
|
}
|
2020-01-21 03:13:32 +08:00
|
|
|
return zrs
|
|
|
|
}
|