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

Retryable http client in Sumologic TF provider masking error code bug fix. #610

Merged
merged 4 commits into from
Jan 24, 2024
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
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
Loading