Repository: cloudflare/cloudflare-go (go)

Issue:
R2 bucket creation is broken due to API change
### Confirmation

- [X] My issue isn't already found on the issue tracker.
- [X] I have replicated my issue using the latest version of the library and it is still present.

### cloudflare-go version

### Expected output

Able to create R2 bucket

### Actual output

✘ [ERROR] A request to the Cloudflare API (/accounts/<account id>/r2/buckets/bucketname) failed.

  No route matches this url. [code: 10015]

### Steps to reproduce

Attempt to create R2 bucket with API

### References

https://github.com/cloudflare/wrangler2/issues/1654

Pull request:
Should fix the R2 bucket create endpoint
## Description

Should fix the R2 bucket creation API request

See https://github.com/cloudflare/wrangler2/pull/1653
Fixes #1033 

## Has your change been tested?

Unit tests pass

## Types of changes

What sort of change does your code introduce/modify?

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [X] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.

Should wait for wrangler to be merged to confirm that this is the correct fix.

Patch:
diff --git a/.changelog/1035.txt b/.changelog/1035.txt
new file mode 100644
index 000000000..4de54bece
--- /dev/null
+++ b/.changelog/1035.txt
@@ -0,0 +1,3 @@
+```release-note:bug
+r2: fix create bucket endpoint
+```
\ No newline at end of file
diff --git a/r2_bucket.go b/r2_bucket.go
index 63cb64a6b..d946b633d 100644
--- a/r2_bucket.go
+++ b/r2_bucket.go
@@ -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
 }


Test patch:
diff --git a/r2_bucket_test.go b/r2_bucket_test.go
index 227f36e9c..92926d5e9 100644
--- a/r2_bucket_test.go
+++ b/r2_bucket_test.go
@@ -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, `{
@@ -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)
 }