github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/dns/config.go (about)

     1  package dns
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/miekg/dns"
     6  	"log"
     7  )
     8  
     9  type Config struct {
    10  	server    string
    11  	port      int
    12  	keyname   string
    13  	keyalgo   string
    14  	keysecret string
    15  }
    16  
    17  type DNSClient struct {
    18  	c         *dns.Client
    19  	srv_addr  string
    20  	keyname   string
    21  	keysecret string
    22  	keyalgo   string
    23  }
    24  
    25  // Configures and returns a fully initialized DNSClient
    26  func (c *Config) Client() (interface{}, error) {
    27  	log.Println("[INFO] Building DNSClient config structure")
    28  
    29  	var client DNSClient
    30  	client.srv_addr = fmt.Sprintf("%s:%d", c.server, c.port)
    31  	authCfgOk := false
    32  	if (c.keyname == "" && c.keysecret == "" && c.keyalgo == "") ||
    33  		(c.keyname != "" && c.keysecret != "" && c.keyalgo != "") {
    34  		authCfgOk = true
    35  	}
    36  	if !authCfgOk {
    37  		return nil, fmt.Errorf("Error configuring provider: when using authentication, \"key_name\", \"key_secret\" and \"key_algorithm\" should be non empty")
    38  	}
    39  	client.c = new(dns.Client)
    40  	if c.keyname != "" {
    41  		client.keyname = c.keyname
    42  		client.keysecret = c.keysecret
    43  		keyalgo, err := convertHMACAlgorithm(c.keyalgo)
    44  		if err != nil {
    45  			return nil, fmt.Errorf("Error configuring provider: %s", err)
    46  		}
    47  		client.keyalgo = keyalgo
    48  		client.c.TsigSecret = map[string]string{c.keyname: c.keysecret}
    49  	}
    50  	return &client, nil
    51  }
    52  
    53  // Validates and converts HMAC algorithm
    54  func convertHMACAlgorithm(name string) (string, error) {
    55  	switch name {
    56  	case "hmac-md5":
    57  		return dns.HmacMD5, nil
    58  	case "hmac-sha1":
    59  		return dns.HmacSHA1, nil
    60  	case "hmac-sha256":
    61  		return dns.HmacSHA256, nil
    62  	case "hmac-sha512":
    63  		return dns.HmacSHA512, nil
    64  	default:
    65  		return "", fmt.Errorf("Unknown HMAC algorithm: %s", name)
    66  	}
    67  }