github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/providers/gandi_v5/convert.go (about)

     1  package gandi5
     2  
     3  // Convert the provider's native record description to models.RecordConfig.
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/tiramiseb/go-gandi/livedns"
     9  
    10  	"github.com/StackExchange/dnscontrol/v2/models"
    11  	"github.com/StackExchange/dnscontrol/v2/pkg/printer"
    12  )
    13  
    14  // nativeToRecord takes a DNS record from Gandi and returns a native RecordConfig struct.
    15  func nativeToRecords(n livedns.DomainRecord, origin string) (rcs []*models.RecordConfig) {
    16  
    17  	// Gandi returns all the values for a given label/rtype pair in each
    18  	// livedns.DomainRecord.  In other words, if there are multiple A
    19  	// records for a label, all the IP addresses are listed in
    20  	// n.RrsetValues rather than having many livedns.DomainRecord's.
    21  	// We must split them out into individual records, one for each value.
    22  	for _, value := range n.RrsetValues {
    23  		rc := &models.RecordConfig{
    24  			TTL:      uint32(n.RrsetTTL),
    25  			Original: n,
    26  		}
    27  		rc.SetLabel(n.RrsetName, origin)
    28  		switch rtype := n.RrsetType; rtype {
    29  		default: //  "A", "AAAA", "CAA", "NS", "CNAME", "MX", "PTR", "SRV", "TXT"
    30  			if err := rc.PopulateFromString(rtype, value, origin); err != nil {
    31  				panic(fmt.Errorf("unparsable record received from gandi: %w", err))
    32  			}
    33  		}
    34  		rcs = append(rcs, rc)
    35  	}
    36  
    37  	return rcs
    38  }
    39  
    40  func recordsToNative(rcs []*models.RecordConfig, origin string) []livedns.DomainRecord {
    41  	// Take a list of RecordConfig and return an equivalent list of ZoneRecords.
    42  	// Gandi requires one ZoneRecord for each label:key tuple, therefore we
    43  	// might collapse many RecordConfig into one ZoneRecord.
    44  
    45  	var keys = map[models.RecordKey]*livedns.DomainRecord{}
    46  	var zrs []livedns.DomainRecord
    47  
    48  	for _, r := range rcs {
    49  		label := r.GetLabel()
    50  		if label == "@" {
    51  			label = origin
    52  		}
    53  		key := r.Key()
    54  
    55  		if zr, ok := keys[key]; !ok {
    56  			// Allocate a new ZoneRecord:
    57  			zr := livedns.DomainRecord{
    58  				RrsetType:   r.Type,
    59  				RrsetTTL:    int(r.TTL),
    60  				RrsetName:   label,
    61  				RrsetValues: []string{r.GetTargetCombined()},
    62  			}
    63  			zrs = append(zrs, zr)
    64  			//keys[key] = &zr   // This didn't work.
    65  			keys[key] = &zrs[len(zrs)-1] // This does work. I don't know why.
    66  
    67  		} else {
    68  			zr.RrsetValues = append(zr.RrsetValues, r.GetTargetCombined())
    69  
    70  			if r.TTL != uint32(zr.RrsetTTL) {
    71  				printer.Warnf("All TTLs for a rrset (%v) must be the same. Using smaller of %v and %v.\n", key, r.TTL, zr.RrsetTTL)
    72  				if r.TTL < uint32(zr.RrsetTTL) {
    73  					zr.RrsetTTL = int(r.TTL)
    74  				}
    75  			}
    76  
    77  		}
    78  	}
    79  
    80  	return zrs
    81  }