Skip to content

Commit

Permalink
Configure retry_wait parameter through the provider configuration (#1017
Browse files Browse the repository at this point in the history
)

* Configure retry_wait parameter through the provider configuration

Allow configuring wait time between retries for the underlying client by
creating a new optional configuration option for the provider.

* Format

---------

Co-authored-by: Julien Duchesne <[email protected]>
  • Loading branch information
stefanoboriero and julienduchesne authored Sep 25, 2023
1 parent 77c2ce2 commit 1ce86ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ resource "grafana_oncall_escalation" "example_notify_step" {
- `org_id` (Number, Deprecated) Deprecated: Use the `org_id` attributes on resources instead.
- `retries` (Number) The amount of retries to use for Grafana API and Grafana Cloud API calls. May alternatively be set via the `GRAFANA_RETRIES` environment variable.
- `retry_status_codes` (Set of String) The status codes to retry on for Grafana API and Grafana Cloud API calls. Use `x` as a digit wildcard. Defaults to 429 and 5xx. May alternatively be set via the `GRAFANA_RETRY_STATUS_CODES` environment variable.
- `retry_wait` (Number) The amount of time in seconds to wait between retries for Grafana API and Grafana Cloud API calls. May alternatively be set via the `GRAFANA_RETRY_WAIT` environment variable.
- `sm_access_token` (String, Sensitive) A Synthetic Monitoring access token. May alternatively be set via the `GRAFANA_SM_ACCESS_TOKEN` environment variable.
- `sm_url` (String) Synthetic monitoring backend address. May alternatively be set via the `GRAFANA_SM_URL` environment variable. The correct value for each service region is cited in the [Synthetic Monitoring documentation](https://grafana.com/docs/grafana-cloud/monitor-public-endpoints/private-probes/#probe-api-server-url). Note the `sm_url` value is optional, but it must correspond with the value specified as the `region_slug` in the `grafana_cloud_stack` resource. Also note that when a Terraform configuration contains multiple provider instances managing SM resources associated with the same Grafana stack, specifying an explicit `sm_url` set to the same value for each provider ensures all providers interact with the same SM API.
- `store_dashboard_sha256` (Boolean) Set to true if you want to save only the sha256sum instead of complete dashboard model JSON in the tfstate.
Expand Down
23 changes: 16 additions & 7 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"strings"
"time"

"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -198,6 +199,12 @@ func Provider(version string) func() *schema.Provider {
Description: "The status codes to retry on for Grafana API and Grafana Cloud API calls. Use `x` as a digit wildcard. Defaults to 429 and 5xx. May alternatively be set via the `GRAFANA_RETRY_STATUS_CODES` environment variable.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"retry_wait": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GRAFANA_RETRY_WAIT", 0),
Description: "The amount of time in seconds to wait between retries for Grafana API and Grafana Cloud API calls. May alternatively be set via the `GRAFANA_RETRY_WAIT` environment variable.",
},
"org_id": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -378,11 +385,12 @@ func createGrafanaClient(d *schema.ResourceData) (string, *gapi.Config, *gapi.Cl
}

cfg := gapi.Config{
Client: cli,
NumRetries: d.Get("retries").(int),
BasicAuth: userInfo,
OrgID: orgID,
APIKey: apiKey,
Client: cli,
NumRetries: d.Get("retries").(int),
RetryTimeout: time.Second * time.Duration(d.Get("retry_wait").(int)),
BasicAuth: userInfo,
OrgID: orgID,
APIKey: apiKey,
}

if v, ok := d.GetOk("retry_status_codes"); ok {
Expand Down Expand Up @@ -450,8 +458,9 @@ func createMLClient(url string, grafanaCfg *gapi.Config) (*mlapi.Client, error)

func createCloudClient(d *schema.ResourceData) (*gapi.Client, error) {
cfg := gapi.Config{
APIKey: d.Get("cloud_api_key").(string),
NumRetries: d.Get("retries").(int),
APIKey: d.Get("cloud_api_key").(string),
NumRetries: d.Get("retries").(int),
RetryTimeout: time.Second * time.Duration(d.Get("retry_wait").(int)),
}

var err error
Expand Down

0 comments on commit 1ce86ab

Please sign in to comment.