Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to customize the API backoff mechanism #296

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions opsgenie/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package opsgenie

import (
"log"
"net/http"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
Expand All @@ -12,16 +14,25 @@ type OpsgenieClient struct {
}

type Config struct {
ApiKey string
ApiUrl string
ApiKey string
ApiUrl string
ApiRetryCount int
ApiRetryWaitMin int
ApiRetryWaitMax int
}

func (c *Config) Client() (*OpsgenieClient, error) {
config := &client.Config{
ApiKey: c.ApiKey,
RetryCount: 10,
RetryCount: c.ApiRetryCount,
OpsGenieAPIURL: client.ApiUrl(c.ApiUrl),
Backoff: retryablehttp.DefaultBackoff,
Backoff: func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if c.ApiRetryWaitMin > 0 && c.ApiRetryWaitMax > 0 {
return retryablehttp.DefaultBackoff(time.Duration(c.ApiRetryWaitMin)*time.Second, time.Duration(c.ApiRetryWaitMax)*time.Second, attemptNum, resp)
} else {
return retryablehttp.DefaultBackoff(min, max, attemptNum, resp)
}
},
}
ogCli, err := client.NewOpsGenieClient(config)
if err != nil {
Expand Down
25 changes: 22 additions & 3 deletions opsgenie/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package opsgenie

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -22,6 +23,21 @@ func Provider() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OPSGENIE_API_URL", "api.opsgenie.com"),
},
"api_retry_count": {
Type: schema.TypeInt,
Optional: true,
Default: 10,
},
"api_retry_wait_min": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
"api_retry_wait_max": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -65,8 +81,11 @@ func providerConfigure(ctx context.Context, data *schema.ResourceData) (interfac
log.Println("[INFO] Initializing OpsGenie client")

config := Config{
ApiKey: data.Get("api_key").(string),
ApiUrl: data.Get("api_url").(string),
ApiKey: data.Get("api_key").(string),
ApiUrl: data.Get("api_url").(string),
ApiRetryCount: data.Get("api_retry_count").(int),
ApiRetryWaitMin: data.Get("api_retry_wait_min").(int),
ApiRetryWaitMax: data.Get("api_retry_wait_max").(int),
}
cli, err := config.Client()
if err != nil {
Expand Down
Loading