Skip to content

Commit

Permalink
rgw/admin: support new-bucket-name for LinkBucket
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Jürgensmeyer <[email protected]>
  • Loading branch information
sj14 committed Nov 28, 2024
1 parent ab45bcd commit 7c206c4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
22 changes: 14 additions & 8 deletions rgw/admin/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"net/http"
)

// BucketLinkInput the bucket link/unlink input parameters
type BucketLinkInput struct {
Bucket string `url:"bucket" json:"bucket"`
BucketID string `url:"bucket-id" json:"bucket_id"`
UID string `url:"uid" json:"uid"`
// BucketUnlinkInput the bucket unlink input parameters
type BucketUnlinkInput struct {
Bucket string `url:"bucket" json:"bucket"`
UID string `url:"uid" json:"uid"`
}

// UnlinkBucket unlink a bucket from a specified user
// Primarily useful for changing bucket ownership.
func (api *API) UnlinkBucket(ctx context.Context, link BucketLinkInput) error {
func (api *API) UnlinkBucket(ctx context.Context, link BucketUnlinkInput) error {
if link.UID == "" {
return errMissingUserID
}
Expand All @@ -25,6 +24,14 @@ func (api *API) UnlinkBucket(ctx context.Context, link BucketLinkInput) error {
return err
}

// BucketLinkInput the bucket link input parameters
type BucketLinkInput struct {
Bucket string `url:"bucket" json:"bucket"`
BucketID string `url:"bucket-id" json:"bucket_id"`
UID string `url:"uid" json:"uid"`
NewBucketName string `url:"new-bucket-name" json:"new_bucket_name"` // Optional; use to rename a bucket. While the tenant-id can be specified, this is not necessary in normal operation.
}

// LinkBucket will link a bucket to a specified user
// unlinking the bucket from any previous user
func (api *API) LinkBucket(ctx context.Context, link BucketLinkInput) error {
Expand All @@ -34,7 +41,6 @@ func (api *API) LinkBucket(ctx context.Context, link BucketLinkInput) error {
if link.Bucket == "" {
return errMissingBucket
}
// valid parameters not supported by go-ceph: new-bucket-name
_, err := api.call(ctx, http.MethodPut, "/bucket", valueToURLParams(link, []string{"uid", "bucket-id", "bucket"}))
_, err := api.call(ctx, http.MethodPut, "/bucket", valueToURLParams(link, []string{"uid", "bucket-id", "bucket", "new-bucket-name"}))
return err
}
42 changes: 41 additions & 1 deletion rgw/admin/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (suite *RadosGWTestSuite) TestLink() {
bucket, err := co.GetBucketInfo(context.Background(), Bucket{Bucket: suite.bucketTestName})
assert.NoError(t, err)

err = co.UnlinkBucket(context.Background(), BucketLinkInput{
err = co.UnlinkBucket(context.Background(), BucketUnlinkInput{
Bucket: suite.bucketTestName,
UID: bucket.Owner,
})
Expand All @@ -77,3 +77,43 @@ func (suite *RadosGWTestSuite) TestLink() {
assert.NoError(suite.T(), err)
})
}

func (suite *RadosGWTestSuite) TestLinkRename() {
suite.SetupConnection()
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, newDebugHTTPClient(http.DefaultClient))
assert.NoError(suite.T(), err)

suite.T().Run("create test user1", func(_ *testing.T) {
user, err := co.CreateUser(context.Background(), User{ID: "test-user1", DisplayName: "test-user1", Email: "[email protected]"})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test-user1", user.ID)
assert.Zero(suite.T(), len(user.Caps))
})

const (
initialBucketName = "initial-name"
renamedBucketName = "renamed-name"
)

suite.T().Run("create test bucket", func(t *testing.T) {
s3, err := newS3Agent(suite.accessKey, suite.secretKey, suite.endpoint, true)
assert.NoError(t, err)

err = s3.createBucket(initialBucketName)
assert.NoError(t, err)
})

suite.T().Run("rename bucket", func(t *testing.T) {
err = co.LinkBucket(context.Background(), BucketLinkInput{
Bucket: initialBucketName,
NewBucketName: renamedBucketName,
})
assert.NoError(suite.T(), err)

_, err = co.GetBucketInfo(context.Background(), Bucket{Bucket: initialBucketName})
assert.Error(t, err)

_, err = co.GetBucketInfo(context.Background(), Bucket{Bucket: renamedBucketName})
assert.NoError(t, ErrNoSuchBucket)
})
}

0 comments on commit 7c206c4

Please sign in to comment.