-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft: BKTCLT-21 CreateBucket(), GetBucketAttributes()
Implement those API functions.
- Loading branch information
1 parent
1dd5be1
commit 309a5b8
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package bucketclient | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type createBucketOptionSet struct { | ||
sessionId int | ||
} | ||
|
||
type CreateBucketOption func(*createBucketOptionSet) | ||
|
||
func SessionIdOption(sessionId int) CreateBucketOption { | ||
return func(options *createBucketOptionSet) { | ||
options.sessionId = sessionId | ||
} | ||
} | ||
|
||
// CreateBucket creates a bucket in metadata. | ||
// bucketAttributes is a JSON blob of bucket attributes | ||
// opts is a set of options: | ||
// | ||
// SessionIdOption forces the session ID where the bucket to be created will land | ||
func (client *BucketClient) CreateBucket(ctx context.Context, | ||
bucketName string, bucketAttributes []byte, opts ...CreateBucketOption) error { | ||
parsedOpts := createBucketOptionSet{ | ||
sessionId: 0, | ||
} | ||
for _, opt := range opts { | ||
opt(&parsedOpts) | ||
} | ||
resource := fmt.Sprintf("/default/bucket/%s", bucketName) | ||
if parsedOpts.sessionId > 0 { | ||
resource += fmt.Sprintf("?raftsession=%d", parsedOpts.sessionId) | ||
} | ||
_, err := client.Request(ctx, "CreateBucket", "POST", resource, | ||
RequestBodyOption(bucketAttributes), RequestBodyContentTypeOption("application/json")) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package bucketclient_test | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/jarcoal/httpmock" | ||
|
||
"github.com/scality/bucketclient/go" | ||
) | ||
|
||
var _ = Describe("CreateBucket()", func() { | ||
It("creates a bucket on an available raft session", func(ctx SpecContext) { | ||
httpmock.RegisterResponder( | ||
"POST", "/default/bucket/my-new-bucket", | ||
func(req *http.Request) (*http.Response, error) { | ||
defer req.Body.Close() | ||
Expect(io.ReadAll(req.Body)).To(Equal([]byte(`{"foo":"bar"}`))) | ||
return httpmock.NewStringResponse(200, ""), nil | ||
}, | ||
) | ||
Expect(client.CreateBucket(ctx, "my-new-bucket", []byte(`{"foo":"bar"}`))).To(Succeed()) | ||
}) | ||
|
||
It("creates a bucket on a chosen raft session", func(ctx SpecContext) { | ||
httpmock.RegisterResponder( | ||
"POST", "/default/bucket/my-new-bucket?raftsession=12", | ||
func(req *http.Request) (*http.Response, error) { | ||
defer req.Body.Close() | ||
Expect(io.ReadAll(req.Body)).To(Equal([]byte(`{"foo":"bar"}`))) | ||
return httpmock.NewStringResponse(200, ""), nil | ||
}, | ||
) | ||
Expect(client.CreateBucket(ctx, "my-new-bucket", []byte(`{"foo":"bar"}`), | ||
bucketclient.SessionIdOption(12))).To(Succeed()) | ||
}) | ||
|
||
It("forwards request error", func(ctx SpecContext) { | ||
httpmock.RegisterResponder( | ||
"POST", "/default/bucket/my-new-bucket", | ||
httpmock.NewStringResponder(500, "I'm afraid I can't do this"), | ||
) | ||
err := client.CreateBucket(ctx, "my-new-bucket", []byte(`{"foo":"bar"}`)) | ||
Expect(err).To(HaveOccurred()) | ||
bcErr, ok := err.(*bucketclient.BucketClientError) | ||
Expect(ok).To(BeTrue()) | ||
Expect(bcErr.StatusCode).To(Equal(500)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package bucketclient | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// GetBucketAttributes retrieves the JSON blob containing the bucket | ||
// attributes attached to a bucket. | ||
func (client *BucketClient) GetBucketAttributes(ctx context.Context, bucketName string) ([]byte, error) { | ||
resource := fmt.Sprintf("/default/attributes/%s", bucketName) | ||
return client.Request(ctx, "GetBucketAttributes", "GET", resource) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package bucketclient_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/jarcoal/httpmock" | ||
|
||
"github.com/scality/bucketclient/go" | ||
) | ||
|
||
var _ = Describe("GetBucketAttributes()", func() { | ||
It("retrieves the bucket attributes of an existing bucket", func(ctx SpecContext) { | ||
httpmock.RegisterResponder( | ||
"GET", "/default/attributes/my-bucket", | ||
httpmock.NewStringResponder(200, `{"foo":"bar"}`), | ||
) | ||
Expect(client.GetBucketAttributes(ctx, "my-bucket")).To( | ||
Equal([]byte(`{"foo":"bar"}`))) | ||
}) | ||
|
||
It("returns a 404 error if the bucket does not exist", func(ctx SpecContext) { | ||
httpmock.RegisterResponder( | ||
"GET", "/default/attributes/my-bucket", | ||
httpmock.NewStringResponder(404, ""), | ||
) | ||
_, err := client.GetBucketAttributes(ctx, "my-bucket") | ||
Expect(err).To(HaveOccurred()) | ||
bcErr, ok := err.(*bucketclient.BucketClientError) | ||
Expect(ok).To(BeTrue()) | ||
Expect(bcErr.StatusCode).To(Equal(404)) | ||
}) | ||
}) |