forked from StackExchange/dnscontrol
/
namedotcomProvider.go
128 lines (113 loc) · 3.26 KB
/
namedotcomProvider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Package namedotcom implements a registrar that uses the name.com api to set name servers. It will self register it's providers when imported.
package namedotcom
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/StackExchange/dnscontrol/providers"
)
const defaultApiBase = "https://api.name.com/api"
type nameDotCom struct {
APIUrl string `json:"apiurl"`
APIUser string `json:"apiuser"`
APIKey string `json:"apikey"`
}
var docNotes = providers.DocumentationNotes{
providers.DocDualHost: providers.Cannot("Apex NS records not editable"),
providers.DocCreateDomains: providers.Cannot("New domains require registration"),
providers.DocOfficiallySupported: providers.Can(),
providers.CanUsePTR: providers.Cannot("PTR records are not supported (See Link)", "https://www.name.com/support/articles/205188508-Reverse-DNS-records"),
}
func newReg(conf map[string]string) (providers.Registrar, error) {
return newProvider(conf)
}
func newDsp(conf map[string]string, meta json.RawMessage) (providers.DNSServiceProvider, error) {
return newProvider(conf)
}
func newProvider(conf map[string]string) (*nameDotCom, error) {
api := &nameDotCom{}
api.APIUser, api.APIKey, api.APIUrl = conf["apiuser"], conf["apikey"], conf["apiurl"]
if api.APIKey == "" || api.APIUser == "" {
return nil, fmt.Errorf("Name.com apikey and apiuser must be provided.")
}
if api.APIUrl == "" {
api.APIUrl = defaultApiBase
}
return api, nil
}
func init() {
providers.RegisterRegistrarType("NAMEDOTCOM", newReg)
providers.RegisterDomainServiceProviderType("NAMEDOTCOM", newDsp, providers.CanUseAlias, providers.CanUseSRV, docNotes)
}
///
//various http helpers for interacting with api
///
func (n *nameDotCom) addAuth(r *http.Request) {
r.Header.Add("Api-Username", n.APIUser)
r.Header.Add("Api-Token", n.APIKey)
}
type apiResult struct {
Result struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"result"`
}
func (r *apiResult) getErr() error {
if r == nil {
return nil
}
if r.Result.Code != 100 {
if r.Result.Message == "" {
return fmt.Errorf("Unknown error from name.com")
}
return fmt.Errorf(r.Result.Message)
}
return nil
}
//perform http GET and unmarshal response json into target struct
func (n *nameDotCom) get(url string, target interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
n.addAuth(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(data, target)
}
// perform http POST, json marshalling the given data into the body
func (n *nameDotCom) post(url string, data interface{}) (*apiResult, error) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
if err := enc.Encode(data); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return nil, err
}
n.addAuth(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
text, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := &apiResult{}
if err = json.Unmarshal(text, result); err != nil {
return nil, err
}
return result, nil
}