diff --git a/client/client.go b/client/client.go index 3dfed8ba..72e0c9a3 100644 --- a/client/client.go +++ b/client/client.go @@ -44,6 +44,47 @@ type RollbarAPIClient struct { Resty *resty.Client } +// NewTestClient sets up a new Rollbar API test client. +func NewTestClient(baseURL, token string) *RollbarAPIClient { + log.Debug().Msg("Initializing Rollbar client") + + // New Resty HTTP client + r := resty.New() + + // Use default transport - needed for VCR + r.SetTransport(http.DefaultTransport). + // set timeout on http client + SetTimeout(30 * time.Second). + // Set retry count to 1 (try 2 times before it fails) + SetRetryCount(1). + SetRetryWaitTime(1 * time.Second). + SetRetryMaxWaitTime(5 * time.Second) + + // Authentication + if token != "" { + r = r.SetHeaders(map[string]string{ + "X-Rollbar-Access-Token": token, + "X-Rollbar-Terraform": "true"}) + } else { + log.Warn().Msg("Rollbar API token not set") + } + + // Authentication + if baseURL == "" { + log.Error().Msg("Rollbar API base URL not set") + } + + // Configure Resty to use Zerolog for logging + r.SetLogger(restyZeroLogger{log.Logger}) + + // Rollbar client + c := RollbarAPIClient{ + Resty: r, + BaseURL: baseURL, + } + return &c +} + // NewClient sets up a new Rollbar API client. func NewClient(baseURL, token string) *RollbarAPIClient { log.Debug().Msg("Initializing Rollbar client") @@ -56,7 +97,7 @@ func NewClient(baseURL, token string) *RollbarAPIClient { // set timeout on http client SetTimeout(30 * time.Second). // Set retry count to 4 (try 5 times before it fails) - SetRetryCount(4). + SetRetryCount(0). SetRetryWaitTime(8 * time.Second). SetRetryMaxWaitTime(50 * time.Second). AddRetryCondition( diff --git a/client/client_suite_test.go b/client/client_suite_test.go index a3768521..2f2269eb 100644 --- a/client/client_suite_test.go +++ b/client/client_suite_test.go @@ -27,10 +27,8 @@ import ( "net/http" "os" "testing" - "time" "github.com/brianvoe/gofakeit/v5" - "github.com/go-resty/resty/v2" "github.com/jarcoal/httpmock" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -78,45 +76,6 @@ type Suite struct { client *RollbarAPIClient } -func NewTestClient(baseURL, token string) *RollbarAPIClient { - log.Debug().Msg("Initializing Rollbar client") - - // New Resty HTTP client - r := resty.New() - - // Use default transport - needed for VCR - r.SetTransport(http.DefaultTransport). - // set timeout on http client - SetTimeout(30 * time.Second). - // Set retry count to 3 (try 3 times before it fails) - SetRetryCount(2). - SetRetryWaitTime(1 * time.Second). - SetRetryMaxWaitTime(5 * time.Second) - - // Authentication - if token != "" { - r = r.SetHeaders(map[string]string{ - "X-Rollbar-Access-Token": token, - "X-Rollbar-Terraform": "true"}) - } else { - log.Warn().Msg("Rollbar API token not set") - } - - // Authentication - if baseURL == "" { - log.Error().Msg("Rollbar API base URL not set") - } - - // Configure Resty to use Zerolog for logging - r.SetLogger(restyZeroLogger{log.Logger}) - - // Rollbar client - c := RollbarAPIClient{ - Resty: r, - BaseURL: baseURL, - } - return &c -} func (s *Suite) SetupSuite() { // Pretty logging log.Logger = log. diff --git a/rollbar/provider_test.go b/rollbar/provider_test.go index d9c8a761..ead3483f 100644 --- a/rollbar/provider_test.go +++ b/rollbar/provider_test.go @@ -23,7 +23,14 @@ package rollbar import ( + "context" "fmt" + "os" + "runtime" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -32,10 +39,6 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/stretchr/testify/suite" - "os" - "runtime" - "strconv" - "testing" ) func init() { @@ -66,6 +69,16 @@ type AccSuite struct { randID int // ID of a Rollbar resource } +// providerConfigure sets up authentication in a Resty HTTP client. +func providerConfigureTest(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { + var diags diag.Diagnostics + token := d.Get(schemaKeyToken).(string) + projectToken := d.Get(projectKeyToken).(string) + baseURL := d.Get(schemaKeyBaseURL).(string) + c := client.NewTestClient(baseURL, token) + pc := client.NewTestClient(baseURL, projectToken) + return map[string]*client.RollbarAPIClient{schemaKeyToken: c, projectKeyToken: pc}, diags +} func (s *AccSuite) SetupSuite() { maxprocs := runtime.GOMAXPROCS(0) log.Debug(). @@ -74,6 +87,7 @@ func (s *AccSuite) SetupSuite() { // Setup testing s.provider = Provider() + s.provider.ConfigureContextFunc = providerConfigureTest s.providers = map[string]*schema.Provider{ "rollbar": s.provider, }