Skip to content

Commit

Permalink
ft: BKTCLT-21 CreateBucket(), GetBucketAttributes()
Browse files Browse the repository at this point in the history
Implement those API functions.
  • Loading branch information
jonathan-gramain committed Sep 24, 2024
1 parent 1dd5be1 commit 309a5b8
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
40 changes: 40 additions & 0 deletions go/createbucket.go
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
}
52 changes: 52 additions & 0 deletions go/createbucket_test.go
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))
})
})
13 changes: 13 additions & 0 deletions go/getbucketattributes.go
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)
}
33 changes: 33 additions & 0 deletions go/getbucketattributes_test.go
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))
})
})

0 comments on commit 309a5b8

Please sign in to comment.