Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should fix the R2 bucket create endpoint #1035

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1035.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
r2: fix create bucket endpoint
```
12 changes: 8 additions & 4 deletions r2_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ var (
ErrMissingBucketName = errors.New("require bucket name missing")
)

type CreateR2BucketParameters struct {
Name string `json:"name,omitempty"`
}

// CreateR2Bucket Creates a new R2 bucket.
//
// API reference: https://api.cloudflare.com/#r2-bucket-create-bucket
func (api *API) CreateR2Bucket(ctx context.Context, rc *ResourceContainer, bucketName string) error {
func (api *API) CreateR2Bucket(ctx context.Context, rc *ResourceContainer, params CreateR2BucketParameters) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}

if bucketName == "" {
if params.Name == "" {
return ErrMissingBucketName
}

uri := fmt.Sprintf("/accounts/%s/r2/buckets/%s", rc.Identifier, bucketName)
_, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
uri := fmt.Sprintf("/accounts/%s/r2/buckets", rc.Identifier)
_, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)

return err
}
Expand Down
8 changes: 4 additions & 4 deletions r2_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestR2_CreateBucket(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/accounts/%s/r2/buckets/%s", testAccountID, testBucketName), func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(fmt.Sprintf("/accounts/%s/r2/buckets", testAccountID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
Expand All @@ -26,17 +26,17 @@ func TestR2_CreateBucket(t *testing.T) {
}`)
})

err := client.CreateR2Bucket(context.Background(), AccountIdentifier(""), "")
err := client.CreateR2Bucket(context.Background(), AccountIdentifier(""), CreateR2BucketParameters{})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingAccountID, err)
}

err = client.CreateR2Bucket(context.Background(), AccountIdentifier(testAccountID), "")
err = client.CreateR2Bucket(context.Background(), AccountIdentifier(testAccountID), CreateR2BucketParameters{Name: ""})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingBucketName, err)
}

err = client.CreateR2Bucket(context.Background(), AccountIdentifier(testAccountID), "example-bucket")
err = client.CreateR2Bucket(context.Background(), AccountIdentifier(testAccountID), CreateR2BucketParameters{Name: testBucketName})
assert.NoError(t, err)
}

Expand Down