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

Enable resty debug logging based on TF_LOG #58

Merged
merged 1 commit into from
Apr 4, 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
24 changes: 11 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package client

import (
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"slices"
"strings"

"github.com/go-resty/resty/v2"
Expand All @@ -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", "<REDACTED>")
return nil
}).
SetHeader("content-type", "application/json").
Expand Down
Loading