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 GetR2Bucket #1265

Merged
merged 9 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions .changelog/1265.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
r2_bucket: add support for getting a bucket
```

```release-note:breaking-change
r2_bucket: change creation time from string to *time.Time
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
```
46 changes: 40 additions & 6 deletions r2_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"time"
)

var (
Expand All @@ -14,8 +15,9 @@ var (

// R2Bucket defines a container for objects stored in R2 Storage.
type R2Bucket struct {
Name string `json:"name"`
CreationDate string `json:"creation_date,omitempty"`
Name string `json:"name"`
CreationDate *time.Time `json:"creation_date,omitempty"`
Location string `json:"location,omitempty"`
}

// R2Buckets represents the map of buckets response from
Expand All @@ -40,6 +42,15 @@ type ListR2BucketsParams struct {
Cursor string `url:"cursor,omitempty"`
}

type CreateR2BucketParameters struct {
Name string `json:"name,omitempty"`
Cyb3r-Jak3 marked this conversation as resolved.
Show resolved Hide resolved
}

type R2BucketResponse struct {
Result R2Bucket `json:"result"`
Response
}

// ListR2Buckets Lists R2 buckets.
func (api *API) ListR2Buckets(ctx context.Context, rc *ResourceContainer, params ListR2BucketsParams) ([]R2Bucket, error) {
if rc.Identifier == "" {
Expand All @@ -61,10 +72,6 @@ func (api *API) ListR2Buckets(ctx context.Context, rc *ResourceContainer, params
return r2BucketListResponse.Result.Buckets, nil
}

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

// CreateR2Bucket Creates a new R2 bucket.
//
// API reference: https://api.cloudflare.com/#r2-bucket-create-bucket
Expand All @@ -83,6 +90,33 @@ func (api *API) CreateR2Bucket(ctx context.Context, rc *ResourceContainer, param
return err
}

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

if bucketName == "" {
return R2Bucket{}, ErrMissingBucketName
}

uri := fmt.Sprintf("/accounts/%s/r2/buckets/%s", rc.Identifier, bucketName)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return R2Bucket{}, err
}

var r2BucketResponse R2BucketResponse
err = json.Unmarshal(res, &r2BucketResponse)
if err != nil {
return R2Bucket{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return r2BucketResponse.Result, nil
}

// DeleteR2Bucket Deletes an existing R2 bucket.
//
// API reference: https://api.cloudflare.com/#r2-bucket-delete-bucket
Expand Down
48 changes: 46 additions & 2 deletions r2_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -32,11 +33,11 @@ func TestR2_ListBuckets(t *testing.T) {
}
}`)
})

createDate, _ := time.Parse(time.RFC3339, "2022-06-24T19:58:49.477Z")
want := []R2Bucket{
{
Name: "example-bucket",
CreationDate: "2022-06-24T19:58:49.477Z",
CreationDate: &createDate,
},
}
actual, err := client.ListR2Buckets(context.Background(), AccountIdentifier(testAccountID), ListR2BucketsParams{})
Expand All @@ -45,6 +46,49 @@ func TestR2_ListBuckets(t *testing.T) {
}
}

func TestR2_GetBucket(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.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"name": "example-bucket",
"creation_date": "2022-06-24T19:58:49.477Z",
"location": "ENAM"
}
}`)
})

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

_, err = client.GetR2Bucket(context.Background(), AccountIdentifier(testAccountID), "")

if assert.Error(t, err) {
assert.Equal(t, ErrMissingBucketName, err)
}

createDate, _ := time.Parse(time.RFC3339, "2022-06-24T19:58:49.477Z")
want := R2Bucket{
Name: testBucketName,
CreationDate: &createDate,
Location: "ENAM",
}

actual, err := client.GetR2Bucket(context.Background(), AccountIdentifier(testAccountID), testBucketName)
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestR2_CreateBucket(t *testing.T) {
setup()
defer teardown()
Expand Down