rgw/admin: remove Debug field from API type

Instead of implementing our own logging let's the consumer implement
their own potential debug for each HTTP requests.

Closes: https://github.com/ceph/go-ceph/issues/508
Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
Sébastien Han 2021-07-27 18:21:53 +02:00
parent 402707f2f5
commit 9cae71ab54
7 changed files with 38 additions and 33 deletions

View File

@ -24,9 +24,6 @@ func main() {
panic(err)
}
// To enable debug requests
// co.Debug = true
// Get the "admin" user
user, err := co.GetUser(context.Background(), "admin")
if err != nil {

View File

@ -3,6 +3,7 @@ package admin
import (
"context"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
@ -10,8 +11,7 @@ import (
func (suite *RadosGWTestSuite) TestBucket() {
suite.SetupConnection()
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, nil)
co.Debug = true
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, newDebugHTTPClient(http.DefaultClient))
assert.NoError(suite.T(), err)
s3, err := newS3Agent(suite.accessKey, suite.secretKey, suite.endpoint, true)

View File

@ -2,6 +2,7 @@ package admin
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
@ -9,8 +10,7 @@ import (
func (suite *RadosGWTestSuite) TestQuota() {
suite.SetupConnection()
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, nil)
co.Debug = true
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, newDebugHTTPClient(http.DefaultClient))
assert.NoError(suite.T(), err)
suite.T().Run("set quota to user but user ID is empty", func(t *testing.T) {

View File

@ -4,9 +4,7 @@ import (
"bytes"
"context"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"
@ -39,7 +37,6 @@ type API struct {
SecretKey string
Endpoint string
HTTPClient HTTPClient
Debug bool
}
// New returns client for Ceph RGW
@ -69,7 +66,6 @@ func New(endpoint, accessKey, secretKey string, httpClient HTTPClient) (*API, er
AccessKey: accessKey,
SecretKey: secretKey,
HTTPClient: httpClient,
Debug: false,
}, nil
}
@ -94,15 +90,6 @@ func (api *API) call(ctx context.Context, httpMethod, path string, args url.Valu
return nil, err
}
// Print request if Debug is enabled
if api.Debug {
dump, err := httputil.DumpRequestOut(request, true)
if err != nil {
return nil, err
}
log.Printf("\n%s\n", string(dump))
}
// Send HTTP request
resp, err := api.HTTPClient.Do(request)
if err != nil {
@ -110,15 +97,6 @@ func (api *API) call(ctx context.Context, httpMethod, path string, args url.Valu
}
defer resp.Body.Close()
// Print response if Debug is enabled
if api.Debug {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
return nil, err
}
log.Printf("\n%s\n", string(dump))
}
// Decode HTTP response
decodedResponse, err := ioutil.ReadAll(resp.Body)
if err != nil {

View File

@ -3,6 +3,7 @@ package admin
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
"reflect"
"testing"
@ -24,6 +25,36 @@ type RadosGWTestSuite struct {
bucketTestName string
}
type debugHTTPClient struct {
client HTTPClient
}
func newDebugHTTPClient(client HTTPClient) *debugHTTPClient {
return &debugHTTPClient{client}
}
func (c *debugHTTPClient) Do(req *http.Request) (*http.Response, error) {
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
fmt.Printf("\n%s\n", string(dump))
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
dump, err = httputil.DumpResponse(resp, true)
if err != nil {
return nil, err
}
fmt.Printf("\n%s\n", string(dump))
return resp, nil
}
func TestRadosGWTestSuite(t *testing.T) {
suite.Run(t, new(RadosGWTestSuite))
}

View File

@ -2,6 +2,7 @@ package admin
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
@ -9,8 +10,7 @@ import (
func (suite *RadosGWTestSuite) TestUsage() {
suite.SetupConnection()
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, nil)
co.Debug = true
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, newDebugHTTPClient(http.DefaultClient))
assert.NoError(suite.T(), err)
suite.T().Run("get usage", func(t *testing.T) {

View File

@ -79,8 +79,7 @@ func TestUnmarshal(t *testing.T) {
func (suite *RadosGWTestSuite) TestUser() {
suite.SetupConnection()
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, nil)
co.Debug = true
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, newDebugHTTPClient(http.DefaultClient))
assert.NoError(suite.T(), err)
suite.T().Run("fail to create user since no UID provided", func(t *testing.T) {