1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Internal: deps updates and linting (#905)

* Update dependencies
* writing-providers.md: Update for Go Mod, etc.
* Linting pkg/version
* s/CloudflareAPI/api/g
* s/ApiKey/APIKey/g
* s/ApiToken/APIToken/g
* s/ApiUser/APIUser/g
This commit is contained in:
Tom Limoncelli
2020-10-18 14:04:50 -04:00
committed by GitHub
parent ac3ee9afea
commit 3a2b1b2f7b
9 changed files with 111 additions and 70 deletions

View File

@@ -57,11 +57,11 @@ func init() {
providers.RegisterCustomRecordType("CF_TEMP_REDIRECT", "CLOUDFLAREAPI", "")
}
// CloudflareAPI is the handle for API calls.
type CloudflareAPI struct {
ApiKey string `json:"apikey"`
ApiToken string `json:"apitoken"`
ApiUser string `json:"apiuser"`
// api is the handle for API calls.
type api struct {
APIKey string `json:"apikey"`
APIToken string `json:"apitoken"`
APIUser string `json:"apiuser"`
AccountID string `json:"accountid"`
AccountName string `json:"accountname"`
domainIndex map[string]string
@@ -82,7 +82,7 @@ func labelMatches(label string, matches []string) bool {
}
// GetNameservers returns the nameservers for a domain.
func (c *CloudflareAPI) GetNameservers(domain string) ([]*models.Nameserver, error) {
func (c *api) GetNameservers(domain string) ([]*models.Nameserver, error) {
if c.domainIndex == nil {
if err := c.fetchDomainList(); err != nil {
return nil, err
@@ -96,7 +96,7 @@ func (c *CloudflareAPI) GetNameservers(domain string) ([]*models.Nameserver, err
}
// ListZones returns a list of the DNS zones.
func (c *CloudflareAPI) ListZones() ([]string, error) {
func (c *api) ListZones() ([]string, error) {
if err := c.fetchDomainList(); err != nil {
return nil, err
}
@@ -108,7 +108,7 @@ func (c *CloudflareAPI) ListZones() ([]string, error) {
}
// GetZoneRecords gets the records of a zone and returns them in RecordConfig format.
func (c *CloudflareAPI) GetZoneRecords(domain string) (models.Records, error) {
func (c *api) GetZoneRecords(domain string) (models.Records, error) {
id, err := c.getDomainID(domain)
if err != nil {
return nil, err
@@ -125,7 +125,7 @@ func (c *CloudflareAPI) GetZoneRecords(domain string) (models.Records, error) {
return records, nil
}
func (c *CloudflareAPI) getDomainID(name string) (string, error) {
func (c *api) getDomainID(name string) (string, error) {
if c.domainIndex == nil {
if err := c.fetchDomainList(); err != nil {
return "", err
@@ -139,7 +139,7 @@ func (c *CloudflareAPI) getDomainID(name string) (string, error) {
}
// GetDomainCorrections returns a list of corrections to update a domain.
func (c *CloudflareAPI) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
func (c *api) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
id, err := c.getDomainID(dc.Name)
if err != nil {
return nil, err
@@ -270,7 +270,7 @@ func checkNSModifications(dc *models.DomainConfig) {
dc.Records = newList
}
func (c *CloudflareAPI) checkUniversalSSL(dc *models.DomainConfig, id string) (changed bool, newState bool, err error) {
func (c *api) checkUniversalSSL(dc *models.DomainConfig, id string) (changed bool, newState bool, err error) {
expectedStr := dc.Metadata[metaUniversalSSL]
if expectedStr == "" {
return false, false, fmt.Errorf("metadata not set")
@@ -309,7 +309,7 @@ func checkProxyVal(v string) (string, error) {
return v, nil
}
func (c *CloudflareAPI) preprocessConfig(dc *models.DomainConfig) error {
func (c *api) preprocessConfig(dc *models.DomainConfig) error {
// Determine the default proxy setting.
var defProxy string
@@ -414,13 +414,13 @@ func (c *CloudflareAPI) preprocessConfig(dc *models.DomainConfig) error {
}
func newCloudflare(m map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
api := &CloudflareAPI{}
api.ApiUser, api.ApiKey, api.ApiToken = m["apiuser"], m["apikey"], m["apitoken"]
api := &api{}
api.APIUser, api.APIKey, api.APIToken = m["apiuser"], m["apikey"], m["apitoken"]
// check api keys from creds json file
if api.ApiToken == "" && (api.ApiKey == "" || api.ApiUser == "") {
if api.APIToken == "" && (api.APIKey == "" || api.APIUser == "") {
return nil, fmt.Errorf("if cloudflare apitoken is not set, apikey and apiuser must be provided")
}
if api.ApiToken != "" && (api.ApiKey != "" || api.ApiUser != "") {
if api.APIToken != "" && (api.APIKey != "" || api.APIUser != "") {
return nil, fmt.Errorf("if cloudflare apitoken is set, apikey and apiuser should not be provided")
}
@@ -611,7 +611,7 @@ func getProxyMetadata(r *models.RecordConfig) map[string]string {
}
// EnsureDomainExists returns an error of domain does not exist.
func (c *CloudflareAPI) EnsureDomainExists(domain string) error {
func (c *api) EnsureDomainExists(domain string) error {
if _, ok := c.domainIndex[domain]; ok {
return nil
}

View File

@@ -27,7 +27,7 @@ func makeRCmeta(meta map[string]string) *models.RecordConfig {
}
func TestPreprocess_BoolValidation(t *testing.T) {
cf := &CloudflareAPI{}
cf := &api{}
domain := newDomainConfig()
domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "on"}))
@@ -49,7 +49,7 @@ func TestPreprocess_BoolValidation(t *testing.T) {
}
func TestPreprocess_BoolValidation_Fails(t *testing.T) {
cf := &CloudflareAPI{}
cf := &api{}
domain := newDomainConfig()
domain.Records = append(domain.Records, &models.RecordConfig{Metadata: map[string]string{metaProxy: "true"}})
err := cf.preprocessConfig(domain)
@@ -59,7 +59,7 @@ func TestPreprocess_BoolValidation_Fails(t *testing.T) {
}
func TestPreprocess_DefaultProxy(t *testing.T) {
cf := &CloudflareAPI{}
cf := &api{}
domain := newDomainConfig()
domain.Metadata[metaProxyDefault] = "full"
domain.Records = append(domain.Records, makeRCmeta(map[string]string{metaProxy: "on"}))
@@ -78,7 +78,7 @@ func TestPreprocess_DefaultProxy(t *testing.T) {
}
func TestPreprocess_DefaultProxy_Validation(t *testing.T) {
cf := &CloudflareAPI{}
cf := &api{}
domain := newDomainConfig()
domain.Metadata[metaProxyDefault] = "true"
err := cf.preprocessConfig(domain)
@@ -100,7 +100,7 @@ func TestIpRewriting(t *testing.T) {
// inside range and proxied
{"1.2.3.4", "255.255.255.4", "full"},
}
cf := &CloudflareAPI{}
cf := &api{}
domain := newDomainConfig()
cf.ipConversions = []transform.IPConversion{{
Low: net.ParseIP("1.2.3.0"),

View File

@@ -23,7 +23,7 @@ const (
)
// get list of domains for account. Cache so the ids can be looked up from domain name
func (c *CloudflareAPI) fetchDomainList() error {
func (c *api) fetchDomainList() error {
c.domainIndex = map[string]string{}
c.nameservers = map[string][]string{}
page := 1
@@ -50,7 +50,7 @@ func (c *CloudflareAPI) fetchDomainList() error {
}
// get all records for a domain
func (c *CloudflareAPI) getRecordsForDomain(id string, domain string) ([]*models.RecordConfig, error) {
func (c *api) getRecordsForDomain(id string, domain string) ([]*models.RecordConfig, error) {
url := fmt.Sprintf(recordsURL, id)
page := 1
records := []*models.RecordConfig{}
@@ -77,7 +77,7 @@ func (c *CloudflareAPI) getRecordsForDomain(id string, domain string) ([]*models
}
// create a correction to delete a record
func (c *CloudflareAPI) deleteRec(rec *cfRecord, domainID string) *models.Correction {
func (c *api) deleteRec(rec *cfRecord, domainID string) *models.Correction {
return &models.Correction{
Msg: fmt.Sprintf("DELETE record: %s %s %d %s (id=%s)", rec.Name, rec.Type, rec.TTL, rec.Content, rec.ID),
F: func() error {
@@ -93,7 +93,7 @@ func (c *CloudflareAPI) deleteRec(rec *cfRecord, domainID string) *models.Correc
}
}
func (c *CloudflareAPI) createZone(domainName string) (string, error) {
func (c *api) createZone(domainName string) (string, error) {
type createZone struct {
Name string `json:"name"`
@@ -173,7 +173,7 @@ func cfSshfpData(rec *models.RecordConfig) *cfRecData {
}
}
func (c *CloudflareAPI) createRec(rec *models.RecordConfig, domainID string) []*models.Correction {
func (c *api) createRec(rec *models.RecordConfig, domainID string) []*models.Correction {
type createRecord struct {
Name string `json:"name"`
Type string `json:"type"`
@@ -245,7 +245,7 @@ func (c *CloudflareAPI) createRec(rec *models.RecordConfig, domainID string) []*
return arr
}
func (c *CloudflareAPI) modifyRecord(domainID, recID string, proxied bool, rec *models.RecordConfig) error {
func (c *api) modifyRecord(domainID, recID string, proxied bool, rec *models.RecordConfig) error {
if domainID == "" || recID == "" {
return fmt.Errorf("cannot modify record if domain or record id are empty")
}
@@ -302,7 +302,7 @@ func (c *CloudflareAPI) modifyRecord(domainID, recID string, proxied bool, rec *
}
// change universal ssl state
func (c *CloudflareAPI) changeUniversalSSL(domainID string, state bool) error {
func (c *api) changeUniversalSSL(domainID string, state bool) error {
type setUniversalSSL struct {
Enabled bool `json:"enabled"`
}
@@ -330,7 +330,7 @@ func (c *CloudflareAPI) changeUniversalSSL(domainID string, state bool) error {
}
// change universal ssl state
func (c *CloudflareAPI) getUniversalSSL(domainID string) (bool, error) {
func (c *api) getUniversalSSL(domainID string) (bool, error) {
type universalSSLResponse struct {
Success bool `json:"success"`
Errors []interface{} `json:"errors"`
@@ -368,17 +368,17 @@ func handleActionResponse(resp *http.Response, err error) (id string, e error) {
return result.Result.ID, nil
}
func (c *CloudflareAPI) setHeaders(req *http.Request) {
if len(c.ApiToken) > 0 {
req.Header.Set("Authorization", "Bearer "+c.ApiToken)
func (c *api) setHeaders(req *http.Request) {
if len(c.APIToken) > 0 {
req.Header.Set("Authorization", "Bearer "+c.APIToken)
} else {
req.Header.Set("X-Auth-Key", c.ApiKey)
req.Header.Set("X-Auth-Email", c.ApiUser)
req.Header.Set("X-Auth-Key", c.APIKey)
req.Header.Set("X-Auth-Email", c.APIUser)
}
}
// generic get handler. makes request and unmarshalls response to given interface
func (c *CloudflareAPI) get(endpoint string, target interface{}) error {
func (c *api) get(endpoint string, target interface{}) error {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
@@ -398,7 +398,7 @@ func (c *CloudflareAPI) get(endpoint string, target interface{}) error {
return decoder.Decode(target)
}
func (c *CloudflareAPI) getPageRules(id string, domain string) ([]*models.RecordConfig, error) {
func (c *api) getPageRules(id string, domain string) ([]*models.RecordConfig, error) {
url := fmt.Sprintf(pageRulesURL, id)
data := pageRuleResponse{}
if err := c.get(url, &data); err != nil {
@@ -437,7 +437,7 @@ func (c *CloudflareAPI) getPageRules(id string, domain string) ([]*models.Record
return recs, nil
}
func (c *CloudflareAPI) deletePageRule(recordID, domainID string) error {
func (c *api) deletePageRule(recordID, domainID string) error {
endpoint := fmt.Sprintf(singlePageRuleURL, domainID, recordID)
req, err := http.NewRequest("DELETE", endpoint, nil)
if err != nil {
@@ -448,19 +448,19 @@ func (c *CloudflareAPI) deletePageRule(recordID, domainID string) error {
return err
}
func (c *CloudflareAPI) updatePageRule(recordID, domainID string, target string) error {
func (c *api) updatePageRule(recordID, domainID string, target string) error {
if err := c.deletePageRule(recordID, domainID); err != nil {
return err
}
return c.createPageRule(domainID, target)
}
func (c *CloudflareAPI) createPageRule(domainID string, target string) error {
func (c *api) createPageRule(domainID string, target string) error {
endpoint := fmt.Sprintf(pageRulesURL, domainID)
return c.sendPageRule(endpoint, "POST", target)
}
func (c *CloudflareAPI) sendPageRule(endpoint, method string, data string) error {
func (c *api) sendPageRule(endpoint, method string, data string) error {
// from to priority code
parts := strings.Split(data, ",")
priority, _ := strconv.Atoi(parts[2])

View File

@@ -37,7 +37,7 @@ func newProvider(conf map[string]string) (*HXClient, error) {
api := &HXClient{
client: hxcl.NewAPIClient(),
}
api.client.SetUserAgent("DNSControl", version.VersionString())
api.client.SetUserAgent("DNSControl", version.Banner())
api.APILogin, api.APIPassword, api.APIEntity = conf["apilogin"], conf["apipassword"], conf["apientity"]
if conf["debugmode"] == "1" {
api.client.EnableDebugMode()