From d346d561a0112cbbcdf1906578a37836a1d306df Mon Sep 17 00:00:00 2001 From: Tom Limoncelli Date: Thu, 13 Jul 2017 11:52:56 -0400 Subject: [PATCH] NAMEDOTCOM needs parameterization to permit integration testing. --- docs/_providers/name.com.md | 7 ++++++- integrationTest/providers.json | 3 ++- providers/namedotcom/namedotcomProvider.go | 10 +++++++--- providers/namedotcom/nameservers.go | 12 ++++++------ providers/namedotcom/nameservers_test.go | 2 +- providers/namedotcom/records.go | 18 +++++++++--------- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/docs/_providers/name.com.md b/docs/_providers/name.com.md index 4d26a6e99..545225bd2 100644 --- a/docs/_providers/name.com.md +++ b/docs/_providers/name.com.md @@ -19,6 +19,11 @@ In your providers config json file you must provide your name.com api username a } {% endhighlight %} +There is another key name `apiurl` but it is optional and defaults to the correct value. If you +want to use the test environment ("OT&E"), then add this: + + "apiurl": "https://api.dev.name.com", + ## Metadata This provider does not recognize any special metadata fields unique to name.com. @@ -53,4 +58,4 @@ D("example.tld", REG_NAMECOM, DnsProvider(R53), In order to activate api functionality on your name.com account, you must apply to the api program. The application form is [located here](https://www.name.com/reseller/apply). It usually takes a few days to get a response. -After you are accepted, you should receive your api token via email. \ No newline at end of file +After you are accepted, you should receive your api token via email. diff --git a/integrationTest/providers.json b/integrationTest/providers.json index b573b35ea..35bff3f90 100644 --- a/integrationTest/providers.json +++ b/integrationTest/providers.json @@ -34,7 +34,8 @@ "NAMEDOTCOM": { "apikey": "$NAMEDOTCOM_KEY", "apiurl": "$NAMEDOTCOM_URL", - "apiuser": "$NAMEDOTCOM_USER" + "apiuser": "$NAMEDOTCOM_USER", + "domain": "$NAMEDOTCOM_DOMAIN" }, "ROUTE53": { "KeyId": "$R53_KEY_ID", diff --git a/providers/namedotcom/namedotcomProvider.go b/providers/namedotcom/namedotcomProvider.go index d1a9c500e..0891871d6 100644 --- a/providers/namedotcom/namedotcomProvider.go +++ b/providers/namedotcom/namedotcomProvider.go @@ -11,7 +11,10 @@ import ( "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"` } @@ -26,10 +29,13 @@ func newDsp(conf map[string]string, meta json.RawMessage) (providers.DNSServiceP func newProvider(conf map[string]string) (*nameDotCom, error) { api := &nameDotCom{} - api.APIUser, api.APIKey = conf["apiuser"], conf["apikey"] + 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 } @@ -67,8 +73,6 @@ func (r *apiResult) getErr() error { return nil } -var apiBase = "https://api.name.com/api" - //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) diff --git a/providers/namedotcom/nameservers.go b/providers/namedotcom/nameservers.go index f00e23646..5b568ce88 100644 --- a/providers/namedotcom/nameservers.go +++ b/providers/namedotcom/nameservers.go @@ -31,7 +31,7 @@ func (n *nameDotCom) GetNameservers(domain string) ([]*models.Nameserver, error) func (n *nameDotCom) getNameserversRaw(domain string) ([]string, error) { result := &getDomainResult{} - if err := n.get(apiGetDomain(domain), result); err != nil { + if err := n.get(n.apiGetDomain(domain), result); err != nil { return nil, err } if err := result.getErr(); err != nil { @@ -69,11 +69,11 @@ func (n *nameDotCom) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models //even if you provide them "ns1.name.com", they will set it to "ns1qrt.name.com". This will match that pattern to see if defaults are in use. var defaultNsRegexp = regexp.MustCompile(`ns1[a-z]{0,3}\.name\.com,ns2[a-z]{0,3}\.name\.com,ns3[a-z]{0,3}\.name\.com,ns4[a-z]{0,3}\.name\.com`) -func apiGetDomain(domain string) string { - return fmt.Sprintf("%s/domain/get/%s", apiBase, domain) +func (n *nameDotCom) apiGetDomain(domain string) string { + return fmt.Sprintf("%s/domain/get/%s", n.APIUrl, domain) } -func apiUpdateNS(domain string) string { - return fmt.Sprintf("%s/domain/update_nameservers/%s", apiBase, domain) +func (n *nameDotCom) apiUpdateNS(domain string) string { + return fmt.Sprintf("%s/domain/update_nameservers/%s", n.APIUrl, domain) } type getDomainResult struct { @@ -87,7 +87,7 @@ func (n *nameDotCom) updateNameservers(ns []string, domain string) func() error dat := struct { Nameservers []string `json:"nameservers"` }{ns} - resp, err := n.post(apiUpdateNS(domain), dat) + resp, err := n.post(n.apiUpdateNS(domain), dat) if err != nil { return err } diff --git a/providers/namedotcom/nameservers_test.go b/providers/namedotcom/nameservers_test.go index 6061b5988..396982494 100644 --- a/providers/namedotcom/nameservers_test.go +++ b/providers/namedotcom/nameservers_test.go @@ -23,8 +23,8 @@ func setup() { client = &nameDotCom{ APIUser: "bob", APIKey: "123", + APIUrl: server.URL, } - apiBase = server.URL } func teardown() { diff --git a/providers/namedotcom/records.go b/providers/namedotcom/records.go index d5d9a7d11..a5832df98 100644 --- a/providers/namedotcom/records.go +++ b/providers/namedotcom/records.go @@ -58,14 +58,14 @@ func (n *nameDotCom) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Co return corrections, nil } -func apiGetRecords(domain string) string { - return fmt.Sprintf("%s/dns/list/%s", apiBase, domain) +func (n *nameDotCom) apiGetRecords(domain string) string { + return fmt.Sprintf("%s/dns/list/%s", n.APIUrl, domain) } -func apiCreateRecord(domain string) string { - return fmt.Sprintf("%s/dns/create/%s", apiBase, domain) +func (n *nameDotCom) apiCreateRecord(domain string) string { + return fmt.Sprintf("%s/dns/create/%s", n.APIUrl, domain) } -func apiDeleteRecord(domain string) string { - return fmt.Sprintf("%s/dns/delete/%s", apiBase, domain) +func (n *nameDotCom) apiDeleteRecord(domain string) string { + return fmt.Sprintf("%s/dns/delete/%s", n.APIUrl, domain) } type nameComRecord struct { @@ -108,7 +108,7 @@ type listRecordsResponse struct { func (n *nameDotCom) getRecords(domain string) ([]*nameComRecord, error) { result := &listRecordsResponse{} - err := n.get(apiGetRecords(domain), result) + err := n.get(n.apiGetRecords(domain), result) if err != nil { return nil, err } @@ -149,7 +149,7 @@ func (n *nameDotCom) createRecord(rc *models.RecordConfig, domain string) error if dat.Hostname == "@" { dat.Hostname = "" } - resp, err := n.post(apiCreateRecord(domain), dat) + resp, err := n.post(n.apiCreateRecord(domain), dat) if err != nil { return err } @@ -160,7 +160,7 @@ func (n *nameDotCom) deleteRecord(id, domain string) error { dat := struct { ID string `json:"record_id"` }{id} - resp, err := n.post(apiDeleteRecord(domain), dat) + resp, err := n.post(n.apiDeleteRecord(domain), dat) if err != nil { return err }