From ca3bc0f9bf58dbdf14d0bc6ad86d2abb36b47eda Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Thu, 4 Apr 2024 14:17:57 -0700 Subject: [PATCH] Enable resty debug logging based on TF_LOG --- CHANGELOG.md | 9 +++++++++ client/client.go | 24 +++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee2aa06..1cba524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## 1.22.4 (Apr 4, 2024) + +IMPROVEMENTS: + +* Enable Resty's debug logging when `TF_LOG` is set to `DEBUG` or `TRACE`. + +Issue [#16](https://github.com/jfrog/terraform-provider-shared/issues/16) +PR: [#58](https://github.com/jfrog/terraform-provider-shared/pull/57) + ## 1.22.3 (Apr 4, 2024) BUG FIXES: diff --git a/client/client.go b/client/client.go index 8a2ab8c..0e033fa 100644 --- a/client/client.go +++ b/client/client.go @@ -2,9 +2,10 @@ package client import ( "fmt" - "net/http" "net/url" + "os" "regexp" + "slices" "strings" "github.com/go-resty/resty/v2" @@ -21,19 +22,16 @@ func Build(URL, productId string) (*resty.Client, error) { restyBase := resty.New(). SetBaseURL(baseUrl). - OnAfterResponse(func(client *resty.Client, response *resty.Response) error { - if response == nil { - return fmt.Errorf("no response found") + OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { + tfLogLevel := strings.ToLower(os.Getenv("TF_LOG")) + if slices.Contains([]string{"debug", "trace"}, tfLogLevel) { + r.SetDebug(true) } - - // Don't log the response if we have 413 erorr from call home request - // This happens when we make request to call home endpoint too frequently - // for Artifactory to aggregate. Generally only happens during test execution - if strings.Contains(response.Request.URL, "artifactory/api/system/usage") && - response.StatusCode() == http.StatusRequestEntityTooLarge { - return nil - } - + return nil + }). + OnRequestLog(func(r *resty.RequestLog) error { + // Never log auth token + r.Header.Set("Authorization", "") return nil }). SetHeader("content-type", "application/json").