github.com/philhug/dnscontrol@v0.2.4-0.20180625181521-921fa9849001/providers/providers.go (about)

     1  package providers
     2  
     3  import (
     4  	"encoding/json"
     5  	"log"
     6  
     7  	"github.com/StackExchange/dnscontrol/models"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // Registrar is an interface for a domain registrar. It can return a list of needed corrections to be applied in the future.
    12  type Registrar interface {
    13  	models.Registrar
    14  }
    15  
    16  // DNSServiceProvider is able to generate a set of corrections that need to be made to correct records for a domain
    17  type DNSServiceProvider interface {
    18  	models.DNSProvider
    19  }
    20  
    21  // DomainCreator should be implemented by providers that have the ability to add domains to an account. the create-domains command
    22  // can be run to ensure all domains are present before running preview/push
    23  type DomainCreator interface {
    24  	EnsureDomainExists(domain string) error
    25  }
    26  
    27  // RegistrarInitializer is a function to create a registrar. Function will be passed the unprocessed json payload from the configuration file for the given provider.
    28  type RegistrarInitializer func(map[string]string) (Registrar, error)
    29  
    30  // RegistrarTypes stores initializer for each registrar.
    31  var RegistrarTypes = map[string]RegistrarInitializer{}
    32  
    33  // DspInitializer is a function to create a DNS service provider. Function will be passed the unprocessed json payload from the configuration file for the given provider.
    34  type DspInitializer func(map[string]string, json.RawMessage) (DNSServiceProvider, error)
    35  
    36  // DNSProviderTypes stores initializer for each DSP.
    37  var DNSProviderTypes = map[string]DspInitializer{}
    38  
    39  // RegisterRegistrarType adds a registrar type to the registry by providing a suitable initialization function.
    40  func RegisterRegistrarType(name string, init RegistrarInitializer, pm ...ProviderMetadata) {
    41  	if _, ok := RegistrarTypes[name]; ok {
    42  		log.Fatalf("Cannot register registrar type %s multiple times", name)
    43  	}
    44  	RegistrarTypes[name] = init
    45  	unwrapProviderCapabilities(name, pm)
    46  }
    47  
    48  // RegisterDomainServiceProviderType adds a dsp to the registry with the given initialization function.
    49  func RegisterDomainServiceProviderType(name string, init DspInitializer, pm ...ProviderMetadata) {
    50  	if _, ok := DNSProviderTypes[name]; ok {
    51  		log.Fatalf("Cannot register registrar type %s multiple times", name)
    52  	}
    53  	DNSProviderTypes[name] = init
    54  	unwrapProviderCapabilities(name, pm)
    55  }
    56  
    57  // CreateRegistrar initializes a registrar instance from given credentials.
    58  func CreateRegistrar(rType string, config map[string]string) (Registrar, error) {
    59  	initer, ok := RegistrarTypes[rType]
    60  	if !ok {
    61  		return nil, errors.Errorf("registrar type %s not declared", rType)
    62  	}
    63  	return initer(config)
    64  }
    65  
    66  // CreateDNSProvider initializes a dns provider instance from given credentials.
    67  func CreateDNSProvider(dType string, config map[string]string, meta json.RawMessage) (DNSServiceProvider, error) {
    68  	initer, ok := DNSProviderTypes[dType]
    69  	if !ok {
    70  		return nil, errors.Errorf("DSP type %s not declared", dType)
    71  	}
    72  	return initer(config, meta)
    73  }
    74  
    75  // None is a basic provider type that does absolutely nothing. Can be useful as a placeholder for third parties or unimplemented providers.
    76  type None struct{}
    77  
    78  // GetRegistrarCorrections returns corrections to update registrars.
    79  func (n None) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
    80  	return nil, nil
    81  }
    82  
    83  // GetNameservers returns the current nameservers for a domain.
    84  func (n None) GetNameservers(string) ([]*models.Nameserver, error) {
    85  	return nil, nil
    86  }
    87  
    88  // GetDomainCorrections returns corrections to update a domain.
    89  func (n None) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
    90  	return nil, nil
    91  }
    92  
    93  func init() {
    94  	RegisterRegistrarType("NONE", func(map[string]string) (Registrar, error) {
    95  		return None{}, nil
    96  	})
    97  }
    98  
    99  // CustomRType stores an rtype that is only valid for this DSP.
   100  type CustomRType struct {
   101  	Name     string
   102  	Provider string
   103  	RealType string
   104  }
   105  
   106  // RegisterCustomRecordType registers a record type that is only valid for one provider.
   107  // provider is the registered type of provider this is valid with
   108  // name is the record type as it will appear in the js. (should be something like $PROVIDER_FOO)
   109  // realType is the record type it will be replaced with after validation
   110  func RegisterCustomRecordType(name, provider, realType string) {
   111  	customRecordTypes[name] = &CustomRType{Name: name, Provider: provider, RealType: realType}
   112  }
   113  
   114  // GetCustomRecordType returns a registered custom record type, or nil if none
   115  func GetCustomRecordType(rType string) *CustomRType {
   116  	return customRecordTypes[rType]
   117  }
   118  
   119  var customRecordTypes = map[string]*CustomRType{}