github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/dns/provider.go (about) 1 package dns 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/miekg/dns" 10 ) 11 12 // Provider returns a schema.Provider for DNS dynamic updates. 13 func Provider() terraform.ResourceProvider { 14 return &schema.Provider{ 15 Schema: map[string]*schema.Schema{ 16 "update": &schema.Schema{ 17 Type: schema.TypeList, 18 MaxItems: 1, 19 Optional: true, 20 Elem: &schema.Resource{ 21 Schema: map[string]*schema.Schema{ 22 "server": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 DefaultFunc: schema.EnvDefaultFunc("DNS_UPDATE_SERVER", nil), 26 }, 27 "port": &schema.Schema{ 28 Type: schema.TypeInt, 29 Optional: true, 30 Default: 53, 31 }, 32 "key_name": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 DefaultFunc: schema.EnvDefaultFunc("DNS_UPDATE_KEYNAME", nil), 36 }, 37 "key_algorithm": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 DefaultFunc: schema.EnvDefaultFunc("DNS_UPDATE_KEYALGORITHM", nil), 41 }, 42 "key_secret": &schema.Schema{ 43 Type: schema.TypeString, 44 Optional: true, 45 DefaultFunc: schema.EnvDefaultFunc("DNS_UPDATE_KEYSECRET", nil), 46 }, 47 }, 48 }, 49 }, 50 }, 51 52 DataSourcesMap: map[string]*schema.Resource{ 53 "dns_a_record_set": dataSourceDnsARecordSet(), 54 "dns_cname_record_set": dataSourceDnsCnameRecordSet(), 55 "dns_txt_record_set": dataSourceDnsTxtRecordSet(), 56 }, 57 58 ResourcesMap: map[string]*schema.Resource{ 59 "dns_a_record_set": resourceDnsARecordSet(), 60 "dns_aaaa_record_set": resourceDnsAAAARecordSet(), 61 "dns_cname_record": resourceDnsCnameRecord(), 62 "dns_ptr_record": resourceDnsPtrRecord(), 63 }, 64 65 ConfigureFunc: configureProvider, 66 } 67 } 68 69 func configureProvider(d *schema.ResourceData) (interface{}, error) { 70 71 var server, keyname, keyalgo, keysecret string 72 var port int 73 74 // if the update block is missing, schema.EnvDefaultFunc is not called 75 if v, ok := d.GetOk("update"); ok { 76 update := v.([]interface{})[0].(map[string]interface{}) 77 if val, ok := update["port"]; ok { 78 port = int(val.(int)) 79 } 80 if val, ok := update["server"]; ok { 81 server = val.(string) 82 } 83 if val, ok := update["key_name"]; ok { 84 keyname = val.(string) 85 } 86 if val, ok := update["key_algorithm"]; ok { 87 keyalgo = val.(string) 88 } 89 if val, ok := update["key_secret"]; ok { 90 keysecret = val.(string) 91 } 92 } else { 93 if len(os.Getenv("DNS_UPDATE_SERVER")) > 0 { 94 server = os.Getenv("DNS_UPDATE_SERVER") 95 } else { 96 return nil, nil 97 } 98 port = 53 99 if len(os.Getenv("DNS_UPDATE_KEYNAME")) > 0 { 100 keyname = os.Getenv("DNS_UPDATE_KEYNAME") 101 } 102 if len(os.Getenv("DNS_UPDATE_KEYALGORITHM")) > 0 { 103 keyalgo = os.Getenv("DNS_UPDATE_KEYALGORITHM") 104 } 105 if len(os.Getenv("DNS_UPDATE_KEYSECRET")) > 0 { 106 keysecret = os.Getenv("DNS_UPDATE_KEYSECRET") 107 } 108 } 109 110 config := Config{ 111 server: server, 112 port: port, 113 keyname: keyname, 114 keyalgo: keyalgo, 115 keysecret: keysecret, 116 } 117 118 return config.Client() 119 } 120 121 func getAVal(record interface{}) (string, error) { 122 123 recstr := record.(*dns.A).String() 124 var name, ttl, class, typ, addr string 125 126 _, err := fmt.Sscanf(recstr, "%s\t%s\t%s\t%s\t%s", &name, &ttl, &class, &typ, &addr) 127 if err != nil { 128 return "", fmt.Errorf("Error parsing record: %s", err) 129 } 130 131 return addr, nil 132 } 133 134 func getAAAAVal(record interface{}) (string, error) { 135 136 recstr := record.(*dns.AAAA).String() 137 var name, ttl, class, typ, addr string 138 139 _, err := fmt.Sscanf(recstr, "%s\t%s\t%s\t%s\t%s", &name, &ttl, &class, &typ, &addr) 140 if err != nil { 141 return "", fmt.Errorf("Error parsing record: %s", err) 142 } 143 144 return addr, nil 145 } 146 147 func getCnameVal(record interface{}) (string, error) { 148 149 recstr := record.(*dns.CNAME).String() 150 var name, ttl, class, typ, cname string 151 152 _, err := fmt.Sscanf(recstr, "%s\t%s\t%s\t%s\t%s", &name, &ttl, &class, &typ, &cname) 153 if err != nil { 154 return "", fmt.Errorf("Error parsing record: %s", err) 155 } 156 157 return cname, nil 158 } 159 160 func getPtrVal(record interface{}) (string, error) { 161 162 recstr := record.(*dns.PTR).String() 163 var name, ttl, class, typ, ptr string 164 165 _, err := fmt.Sscanf(recstr, "%s\t%s\t%s\t%s\t%s", &name, &ttl, &class, &typ, &ptr) 166 if err != nil { 167 return "", fmt.Errorf("Error parsing record: %s", err) 168 } 169 170 return ptr, nil 171 }