Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsz-rb committed Feb 27, 2024
1 parent 25196fd commit 4915cdd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
43 changes: 42 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand Down
41 changes: 0 additions & 41 deletions client/client_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 18 additions & 4 deletions rollbar/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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().
Expand All @@ -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,
}
Expand Down

0 comments on commit 4915cdd

Please sign in to comment.