Skip to content

Commit

Permalink
log: Enable API client log
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed May 18, 2024
1 parent f2a3090 commit 85f31b6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
20 changes: 20 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func New(ctx context.Context, baseURL string, opt *BuildOption) (*Client, error)
}

client := resty.NewWithClient(httpClient)
client.SetDebug(true)

if opt.Security != nil {
if err := opt.Security.configureClient(ctx, client); err != nil {
return nil, err
Expand All @@ -109,6 +111,12 @@ func New(ctx context.Context, baseURL string, opt *BuildOption) (*Client, error)
return &Client{client}, nil
}

// SetLoggerContext sets the ctx to the internal resty logger, as the tflog requires the current ctx.
// This needs to be called at the start of each CRUD function.
func (c *Client) SetLoggerContext(ctx context.Context) {
c.Client.SetLogger(tflogger{ctx: ctx})
}

type RetryOption struct {
StatusLocator ValueLocator
Status PollingStatus
Expand Down Expand Up @@ -175,6 +183,8 @@ type CreateOption struct {
}

func (c *Client) Create(ctx context.Context, path string, body interface{}, opt CreateOption) (*resty.Response, error) {
c.SetLoggerContext(ctx)

if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
Expand Down Expand Up @@ -203,6 +213,8 @@ type ReadOption struct {
}

func (c *Client) Read(ctx context.Context, path string, opt ReadOption) (*resty.Response, error) {
c.SetLoggerContext(ctx)

if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
Expand All @@ -224,6 +236,8 @@ type UpdateOption struct {
}

func (c *Client) Update(ctx context.Context, path string, body interface{}, opt UpdateOption) (*resty.Response, error) {
c.SetLoggerContext(ctx)

if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
Expand Down Expand Up @@ -254,6 +268,8 @@ type DeleteOption struct {
}

func (c *Client) Delete(ctx context.Context, path string, opt DeleteOption) (*resty.Response, error) {
c.SetLoggerContext(ctx)

if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
Expand Down Expand Up @@ -281,6 +297,8 @@ type OperationOption struct {
}

func (c *Client) Operation(ctx context.Context, path string, body interface{}, opt OperationOption) (*resty.Response, error) {
c.SetLoggerContext(ctx)

if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
Expand Down Expand Up @@ -317,6 +335,8 @@ type ReadOptionDS struct {
}

func (c *Client) ReadDS(ctx context.Context, path string, opt ReadOptionDS) (*resty.Response, error) {
c.SetLoggerContext(ctx)

if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
Expand Down
27 changes: 27 additions & 0 deletions internal/client/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package client

import (
"context"
"fmt"

"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

type tflogger struct {
ctx context.Context
}

var _ resty.Logger = tflogger{}

func (t tflogger) Debugf(format string, v ...interface{}) {
tflog.Debug(t.ctx, fmt.Sprintf(format, v...))
}

func (t tflogger) Warnf(format string, v ...interface{}) {
tflog.Warn(t.ctx, fmt.Sprintf(format, v...))
}

func (t tflogger) Errorf(format string, v ...interface{}) {
tflog.Error(t.ctx, fmt.Sprintf(format, v...))
}
4 changes: 2 additions & 2 deletions internal/provider/resource_dead_simple_json_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestResource_DeadSimpleServer_CreateRetString(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"create_method", "read_path"},
ImportStateIdFunc: func(s *terraform.State) (string, error) {
return fmt.Sprintf(`{"path": "/test", "id": "/test/%s", "body": {}}`, id), nil
return fmt.Sprintf(`{"path": "test", "id": "test/%s", "body": {}}`, id), nil
},
},
},
Expand Down Expand Up @@ -185,7 +185,7 @@ provider "restful" {
}
resource "restful_resource" "test" {
path = "/test"
path = "test"
create_method = "PUT"
read_path = "$(path)/$(body)"
body = "{}"
Expand Down

0 comments on commit 85f31b6

Please sign in to comment.