rgw: add ListBucketsWithStat function

Add an rgw api function that combines listing and stat'ing buckets.

Signed-off-by: moti <motaharesdq@gmail.com>
This commit is contained in:
moti 2023-01-20 09:19:10 +03:30 committed by mergify[bot]
parent 8960979182
commit dafbe65716
2 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,32 @@
//go:build ceph_preview
// +build ceph_preview
package admin
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// ListBucketsWithStat will return the list of all buckets with stat (system admin API only)
func (api *API) ListBucketsWithStat(ctx context.Context) ([]Bucket, error) {
generateStat := true
listingSpec := BucketListingSpec{
GenerateStat: &generateStat,
}
body, err := api.call(ctx, http.MethodGet, "/bucket", valueToURLParams(listingSpec, []string{"stats"}))
if err != nil {
return nil, err
}
ref := []Bucket{}
err = json.Unmarshal(body, &ref)
if err != nil {
return nil, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err)
}
return ref, nil
}

View File

@ -0,0 +1,35 @@
//go:build ceph_preview
// +build ceph_preview
package admin
import (
"context"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func (suite *RadosGWTestSuite) TestListBucketsWithStat() {
suite.SetupConnection()
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)
assert.NoError(suite.T(), err)
err = s3.createBucket(suite.bucketTestName)
assert.NoError(suite.T(), err)
suite.T().Run("list buckets with stat", func(t *testing.T) {
buckets, err := co.ListBucketsWithStat(context.Background())
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 1, len(buckets))
b := buckets[0]
assert.NotNil(suite.T(), b)
assert.Equal(suite.T(), suite.bucketTestName, b.Bucket)
assert.Equal(suite.T(), "admin", b.Owner)
assert.NotNil(suite.T(), b.BucketQuota.MaxSize)
})
}