Skip to content

Commit

Permalink
provider: add configuration option to use a specific base_url
Browse files Browse the repository at this point in the history
Following on from cloudflare/cloudflare-go#606, this allows the provider
to pass through the ability to override the base URL that the API client
uses for calls.
  • Loading branch information
jacobbednarz committed Oct 21, 2021
1 parent 73a37ae commit 90e9c6a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/1270.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
provider: add the ability to configure a different hostname and base path for the API client
```
19 changes: 18 additions & 1 deletion cloudflare/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("CLOUDFLARE_ACCOUNT_ID", nil),
Description: "Configure API client to always use that account",
},

"api_hostname": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CLOUDFLARE_API_HOSTNAME", "api.cloudflare.com"),
Description: "Configure the hostname used by the API client",
},

"api_base_path": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CLOUDFLARE_API_BASE_PATH", "/client/v4"),
Description: "Configure the base path used by the API client",
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -183,9 +197,12 @@ func Provider() *schema.Provider {
}

func providerConfigure(d *schema.ResourceData, terraformVersion string) (interface{}, error) {
baseURL := cloudflare.BaseURL(
"https://" + d.Get("api_hostname").(string) + d.Get("api_base_path").(string),
)
limitOpt := cloudflare.UsingRateLimit(float64(d.Get("rps").(int)))
retryOpt := cloudflare.UsingRetryPolicy(d.Get("retries").(int), d.Get("min_backoff").(int), d.Get("max_backoff").(int))
options := []cloudflare.Option{limitOpt, retryOpt}
options := []cloudflare.Option{limitOpt, retryOpt, baseURL}

if d.Get("api_client_logging").(bool) {
options = append(options, cloudflare.UsingLogger(log.New(os.Stderr, "", log.LstdFlags)))
Expand Down
2 changes: 2 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ The following arguments are supported:
* `account_id` - (Optional) Configure API client with this account ID, so calls use the account API rather than the (default) user API.
This is required for other users in your account to have access to the resources you manage.
This can also be specified with the `CLOUDFLARE_ACCOUNT_ID` shell environment variable.
* `api_hostname` - (Optional) Configure the API client to use a specific hostname. Default: "api.cloudflare.com"
* `api_base_path` - (Optional) Configure the API client to use a specific base path. Default: "/client/v4"

0 comments on commit 90e9c6a

Please sign in to comment.