From 90e9c6afcf21a6340b0de0ad7fe8edba3584c1d8 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Thu, 21 Oct 2021 08:56:30 +1100 Subject: [PATCH] provider: add configuration option to use a specific `base_url` 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. --- .changelog/1270.txt | 3 +++ cloudflare/provider.go | 19 ++++++++++++++++++- website/docs/index.html.markdown | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .changelog/1270.txt diff --git a/.changelog/1270.txt b/.changelog/1270.txt new file mode 100644 index 0000000000..79fa4b5caf --- /dev/null +++ b/.changelog/1270.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +provider: add the ability to configure a different hostname and base path for the API client +``` diff --git a/cloudflare/provider.go b/cloudflare/provider.go index 5f773fd310..e58ac1323d 100644 --- a/cloudflare/provider.go +++ b/cloudflare/provider.go @@ -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{ @@ -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))) diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index 9fe0c6a069..e4739ec871 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -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"