github.com/teknogeek/dnscontrol@v0.2.8/pkg/nameservers/nameservers.go (about)

     1  // Package nameservers provides logic for dynamically finding nameservers for a domain, and configuring NS records for them.
     2  package nameservers
     3  
     4  import (
     5  	"fmt"
     6  	"strings"
     7  
     8  	"strconv"
     9  
    10  	"github.com/StackExchange/dnscontrol/models"
    11  )
    12  
    13  // DetermineNameservers will find all nameservers we should use for a domain. It follows the following rules:
    14  // 1. All explicitly defined NAMESERVER records will be used.
    15  // 2. Each DSP declares how many nameservers to use. Default is all. 0 indicates to use none.
    16  func DetermineNameservers(dc *models.DomainConfig) ([]*models.Nameserver, error) {
    17  	// always take explicit
    18  	ns := dc.Nameservers
    19  	for _, dnsProvider := range dc.DNSProviderInstances {
    20  		n := dnsProvider.NumberOfNameservers
    21  		if n == 0 {
    22  			continue
    23  		}
    24  		fmt.Printf("----- Getting nameservers from: %s\n", dnsProvider.Name)
    25  		nss, err := dnsProvider.Driver.GetNameservers(dc.Name)
    26  		if err != nil {
    27  			return nil, err
    28  		}
    29  		take := len(nss)
    30  		if n > 0 && n < take {
    31  			take = n
    32  		}
    33  		for i := 0; i < take; i++ {
    34  			ns = append(ns, nss[i])
    35  		}
    36  	}
    37  	return ns, nil
    38  }
    39  
    40  // AddNSRecords creates NS records on a domain corresponding to the nameservers specified.
    41  func AddNSRecords(dc *models.DomainConfig) {
    42  	ttl := uint32(300)
    43  	if ttls, ok := dc.Metadata["ns_ttl"]; ok {
    44  		t, err := strconv.ParseUint(ttls, 10, 32)
    45  		if err != nil {
    46  			fmt.Printf("WARNING: ns_ttl fpr %s (%s) is not a valid int", dc.Name, ttls)
    47  		} else {
    48  			ttl = uint32(t)
    49  		}
    50  	}
    51  	for _, ns := range dc.Nameservers {
    52  		rc := &models.RecordConfig{
    53  			Type:     "NS",
    54  			Metadata: map[string]string{},
    55  			TTL:      ttl,
    56  		}
    57  		rc.SetLabel("@", dc.Name)
    58  		t := ns.Name
    59  		if !strings.HasSuffix(t, ".") {
    60  			t += "."
    61  		}
    62  		rc.SetTarget(t)
    63  
    64  		dc.Records = append(dc.Records, rc)
    65  	}
    66  }