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

Add support for R2 bucket creation and deletion #1028

Merged
merged 2 commits into from
Aug 9, 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/1028.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
r2: Add in support for creating and deleting R2 buckets
```
48 changes: 48 additions & 0 deletions r2_bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cloudflare

import (
"context"
"errors"
"fmt"
"net/http"
)

var (
ErrMissingBucketName = errors.New("require bucket name missing")
)

// 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 {
if rc.Identifier == "" {
return ErrMissingAccountID
}

if bucketName == "" {
return ErrMissingBucketName
}

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

return err
}

// DeleteR2Bucket Deletes an existing R2 bucket.
//
// API reference: https://api.cloudflare.com/#r2-bucket-delete-bucket
func (api *API) DeleteR2Bucket(ctx context.Context, rc *ResourceContainer, bucketName string) error {
if rc.Identifier == "" {
return ErrMissingAccountID
}

if bucketName == "" {
return ErrMissingBucketName
}

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

return err
}
70 changes: 70 additions & 0 deletions r2_bucket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cloudflare

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

const testBucketName = "example-bucket"

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) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {}
}`)
})

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

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

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

func TestR2_DeleteBucket(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/accounts/%s/r2/buckets/%s", testAccountID, testBucketName), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {}
}`)
})

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

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

err = client.DeleteR2Bucket(context.Background(), AccountIdentifier(testAccountID), "example-bucket")
assert.NoError(t, err)
}