rgw/admin: Individual bucket quota support

This commit is contained in:
Kharitonov Maksim Andreevich 2022-07-27 13:47:26 +03:00 committed by mergify[bot]
parent fde1e1b66b
commit 16df743c45
4 changed files with 78 additions and 0 deletions

28
rgw/admin/bucket_quota.go Normal file
View File

@ -0,0 +1,28 @@
//go:build ceph_preview
// +build ceph_preview
package admin
import (
"context"
"net/http"
)
// SetIndividualBucketQuota sets quota to a specific bucket
// https://docs.ceph.com/en/latest/radosgw/adminops/#set-quota-for-an-individual-bucket
func (api *API) SetIndividualBucketQuota(ctx context.Context, quota QuotaSpec) error {
if quota.UID == "" {
return errMissingUserID
}
if quota.Bucket == "" {
return errMissingUserBucket
}
_, err := api.call(ctx, http.MethodPut, "/bucket?quota", valueToURLParams(quota, []string{"bucket", "uid", "enabled", "max-size", "max-size-kb", "max-objects"}))
if err != nil {
return err
}
return nil
}

View File

@ -0,0 +1,48 @@
//go:build ceph_preview
// +build ceph_preview
package admin
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
var testBucketQuota = 1000000
func (suite *RadosGWTestSuite) TestBucketQuota() {
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("set bucket quota but no user is specified", func(t *testing.T) {
err := co.SetIndividualBucketQuota(context.Background(), QuotaSpec{})
assert.Error(suite.T(), err)
assert.EqualError(suite.T(), err, errMissingUserID.Error())
})
suite.T().Run("set bucket quota but no bucket is specified", func(t *testing.T) {
err := co.SetIndividualBucketQuota(context.Background(), QuotaSpec{UID: "admin"})
assert.Error(suite.T(), err)
assert.EqualError(suite.T(), err, errMissingUserBucket.Error())
})
suite.T().Run("set bucket quota", func(t *testing.T) {
err := co.SetIndividualBucketQuota(context.Background(), QuotaSpec{UID: "admin", Bucket: suite.bucketTestName, MaxSizeKb: &testBucketQuota})
assert.NoError(suite.T(), err)
bucketInfo, err := co.GetBucketInfo(context.Background(), Bucket{Bucket: suite.bucketTestName})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), &testBucketQuota, bucketInfo.BucketQuota.MaxSizeKb)
})
}

View File

@ -93,6 +93,7 @@ var (
errMissingUserCap = errors.New("missing user capabilities")
errMissingBucketID = errors.New("missing bucket ID")
errMissingBucket = errors.New("missing bucket")
errMissingUserBucket = errors.New("missing bucket")
)
// errorReason is the reason of the error

View File

@ -11,6 +11,7 @@ import (
// Only user's quota are supported
type QuotaSpec struct {
UID string `json:"user_id" url:"uid"`
Bucket string `json:"bucket" url:"bucket"`
QuotaType string `url:"quota-type"`
Enabled *bool `json:"enabled" url:"enabled"`
CheckOnRaw bool `json:"check_on_raw"`