CLOUDFLARE: Stop requiring accountname (#1280)

There is no API call I've found that requires it, only the accountID.  Also, we now set the cfClient.AccountID similar to b55278140f (h/t @fdcastel) and no longer store duplicate information in the cfClient and api objects.

Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
Brian Hartvigsen 2021-10-03 14:40:50 -06:00 committed by GitHub
parent 413d5a15c3
commit d08a8f6c4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 17 deletions

View file

@ -43,7 +43,6 @@ If your Cloudflare account has access to multiple Cloudflare accounts, you can s
"cloudflare": {
"apitoken": "...",
"accountid": "your-cloudflare-account-id",
"accountname": "your-cloudflare-account-name"
}
}
{% endhighlight %}

View file

@ -25,7 +25,6 @@ Info required in `creds.json`:
- apikey
- apiuser
- accountid (optional)
- accountname (optional)
Record level metadata available:
- cloudflare_proxy ("on", "off", or "full")
@ -63,11 +62,6 @@ func init() {
// cloudflareProvider is the handle for API calls.
type cloudflareProvider 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
nameservers map[string][]string
ipConversions []transform.IPConversion
@ -450,20 +444,19 @@ func (c *cloudflareProvider) preprocessConfig(dc *models.DomainConfig) error {
func newCloudflare(m map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
api := &cloudflareProvider{}
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 m["apitoken"] == "" && (m["apikey"] == "" || m["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 m["apitoken"] != "" && (m["apikey"] != "" || m["apiuser"] != "") {
return nil, fmt.Errorf("if cloudflare apitoken is set, apikey and apiuser should not be provided")
}
var err error
if api.APIToken != "" {
api.cfClient, err = cloudflare.NewWithAPIToken(api.APIToken)
if m["apitoken"] != "" {
api.cfClient, err = cloudflare.NewWithAPIToken(m["apitoken"])
} else {
api.cfClient, err = cloudflare.New(api.APIKey, api.APIUser)
api.cfClient, err = cloudflare.New(m["apikey"], m["apiuser"])
}
if err != nil {
@ -471,9 +464,8 @@ func newCloudflare(m map[string]string, metadata json.RawMessage) (providers.DNS
}
// Check account data if set
api.AccountID, api.AccountName = m["accountid"], m["accountname"]
if (api.AccountID != "" && api.AccountName == "") || (api.AccountID == "" && api.AccountName != "") {
return nil, fmt.Errorf("either both cloudflare accountid and accountname must be provided or neither")
if m["accountid"] != "" {
api.cfClient.AccountID = m["accountid"]
}
if len(metadata) > 0 {

View file

@ -56,7 +56,7 @@ func (c *cloudflareProvider) deleteRec(rec cloudflare.DNSRecord, domainID string
}
func (c *cloudflareProvider) createZone(domainName string) (string, error) {
zone, err := c.cfClient.CreateZone(context.Background(), domainName, false, cloudflare.Account{Name: c.AccountName, ID: c.AccountID}, "full")
zone, err := c.cfClient.CreateZone(context.Background(), domainName, false, cloudflare.Account{ID: c.cfClient.AccountID}, "full")
return zone.ID, err
}