rgw/admin: List user's bucket support

This commit is contained in:
Kharitonov Maksim Andreevich 2022-07-27 13:56:09 +03:00 committed by mergify[bot]
parent 3b66ca3c53
commit 4ef5a9547e
2 changed files with 135 additions and 0 deletions

69
rgw/admin/user_bucket.go Normal file
View File

@ -0,0 +1,69 @@
//go:build ceph_preview
// +build ceph_preview
package admin
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// BucketListingSpec describes a request
type BucketListingSpec struct {
UID string `url:"uid"`
GenerateStat *bool `url:"stats"`
}
// ListUsersBuckets will return the list of all users buckets without stat
func (api *API) ListUsersBuckets(ctx context.Context, uid string) ([]string, error) {
if uid == "" {
return nil, errMissingUserID
}
generateStat := false
listingSpec := BucketListingSpec{
UID: uid,
GenerateStat: &generateStat,
}
body, err := api.call(ctx, http.MethodGet, "/bucket", valueToURLParams(listingSpec, []string{"uid", "stats"}))
if err != nil {
return nil, err
}
var s []string
err = json.Unmarshal(body, &s)
if err != nil {
return nil, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err)
}
return s, nil
}
// ListUsersBucketsWithStat will return the list of all users buckets with stat
func (api *API) ListUsersBucketsWithStat(ctx context.Context, uid string) ([]Bucket, error) {
if uid == "" {
return nil, errMissingUserID
}
generateStat := true
listingSpec := BucketListingSpec{
UID: uid,
GenerateStat: &generateStat,
}
body, err := api.call(ctx, http.MethodGet, "/bucket", valueToURLParams(listingSpec, []string{"uid", "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,66 @@
//go:build ceph_preview
// +build ceph_preview
package admin
import (
"context"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func (suite *RadosGWTestSuite) TestUserBucket() {
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 empty user's buckets", func(t *testing.T) {
_, err := co.ListUsersBuckets(context.Background(), "")
assert.Error(suite.T(), err)
assert.True(suite.T(), errors.Is(err, errMissingUserID), err)
})
suite.T().Run("list user's buckets", func(t *testing.T) {
buckets, err := co.ListUsersBuckets(context.Background(), "admin")
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 1, len(buckets))
})
suite.T().Run("list unknown user's buckets", func(t *testing.T) {
buckets, err := co.ListUsersBuckets(context.Background(), "foo")
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 0, len(buckets))
})
suite.T().Run("list empty user's buckets with stat", func(t *testing.T) {
_, err := co.ListUsersBucketsWithStat(context.Background(), "")
assert.Error(suite.T(), err)
assert.True(suite.T(), errors.Is(err, errMissingUserID), err)
})
suite.T().Run("list user's buckets with stat", func(t *testing.T) {
buckets, err := co.ListUsersBucketsWithStat(context.Background(), "admin")
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)
})
suite.T().Run("list unknown user's buckets with stat", func(t *testing.T) {
buckets, err := co.ListUsersBucketsWithStat(context.Background(), "foo")
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 0, len(buckets))
})
}