Skip to content

Commit

Permalink
Merge pull request #610 from SumoLogic/Test
Browse files Browse the repository at this point in the history
Retryable http client in Sumologic TF provider masking error code bug fix.
  • Loading branch information
kaushik-sumo authored Jan 24, 2024
2 parents 58016cd + 2561439 commit 7685d51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package main
import (
"github.com/SumoLogic/terraform-provider-sumologic/sumologic"
"github.com/hashicorp/terraform-plugin-sdk/plugin"
"log"
)

var version string // provider version is passed as compile time argument
var defaultVersion = "dev"

func main() {
// Remove any date and time prefix in log package function output to
// prevent duplicate timestamp and incorrect log level setting
// See: https://developer.hashicorp.com/terraform/plugin/log/writing#duplicate-timestamp-and-incorrect-level-messages
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
if version == "" {
sumologic.ProviderVersion = defaultVersion
} else {
Expand Down
9 changes: 7 additions & 2 deletions sumologic/sumologic_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func createNewRequest(method, url string, body io.Reader, accessID string, acces
func logRequestAndResponse(req *http.Request, resp *http.Response) {
var maskedHeader = req.Header.Clone()
maskedHeader.Set("Authorization", "xxxxxxxxxxx")
log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [StatusCode=%s]\n", req.Method, req.URL, maskedHeader, resp.Status)
log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [Status=%s]\n", req.Method, req.URL, maskedHeader, resp.Status)
}

func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, []*http.Cookie, error) {
Expand Down Expand Up @@ -328,12 +328,17 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
return d, nil
}

func ErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) {
log.Printf("[ERROR] Request %s failed after %d attempts with response: [%s]", resp.Request.URL, numTries, resp.Status)
return resp, err
}

func NewClient(accessID, accessKey, authJwt, environment, base_url string, admin bool) (*Client, error) {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
// Disable DEBUG logs (https://github.com/hashicorp/go-retryablehttp/issues/31)
retryClient.Logger = nil

retryClient.ErrorHandler = ErrorHandler
client := Client{
AccessID: accessID,
AccessKey: accessKey,
Expand Down

0 comments on commit 7685d51

Please sign in to comment.