Skip to content

Commit

Permalink
fix(r2_bucket): handle jurisdiction as part of the import string
Browse files Browse the repository at this point in the history
Closes #4986
  • Loading branch information
jacobbednarz authored and stainless-app[bot] committed Jan 31, 2025
1 parent 4b4dec4 commit 3c5eef5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/resources/r2_bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ resource "cloudflare_r2_bucket" "example_r2_bucket" {
Import is supported using the following syntax:

```shell
$ terraform import cloudflare_r2_bucket.example '<account_id>/<bucket_name>'
$ terraform import cloudflare_r2_bucket.example '<account_id>/<bucket_name>/<jurisdiction>'
```
2 changes: 1 addition & 1 deletion examples/resources/cloudflare_r2_bucket/import.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$ terraform import cloudflare_r2_bucket.example '<account_id>/<bucket_name>'
$ terraform import cloudflare_r2_bucket.example '<account_id>/<bucket_name>/<jurisdiction>'
11 changes: 10 additions & 1 deletion internal/services/r2_bucket/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,13 @@ func (r *R2BucketResource) ImportState(ctx context.Context, req resource.ImportS

path_account_id := ""
path_bucket_name := ""
jurisdiction := "default"
diags := importpath.ParseImportID(
req.ID,
"<account_id>/<bucket_name>",
"<account_id>/<bucket_name>/<jurisdiction>",
&path_account_id,
&path_bucket_name,
&jurisdiction,
)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand All @@ -237,6 +239,12 @@ func (r *R2BucketResource) ImportState(ctx context.Context, req resource.ImportS
data.AccountID = types.StringValue(path_account_id)
data.Name = types.StringValue(path_bucket_name)

if jurisdiction == "default" {
data.Jurisdiction = types.StringValue("default")
} else {
data.Jurisdiction = types.StringValue(jurisdiction)
}

res := new(http.Response)
env := R2BucketResultEnvelope{*data}
_, err := r.client.R2.Buckets.Get(
Expand All @@ -245,6 +253,7 @@ func (r *R2BucketResource) ImportState(ctx context.Context, req resource.ImportS
r2.BucketGetParams{
AccountID: cloudflare.F(path_account_id),
},
option.WithHeader(jurisdictionHTTPHeaderName, data.Jurisdiction.ValueString()),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
)
Expand Down
26 changes: 20 additions & 6 deletions internal/services/r2_bucket/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest"
"github.com/cloudflare/terraform-provider-cloudflare/internal/utils"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -120,12 +122,15 @@ func TestAccCloudflareR2Bucket_Basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "location", "ENAM"),
),
},
// {
// ResourceName: resourceName,
// ImportStateIdPrefix: fmt.Sprintf("%s/", accountID),
// ImportState: true,
// ImportStateVerify: true,
// },
{
ResourceName: resourceName,
ImportStateIdFunc: func(*terraform.State) (string, error) {
return strings.Join([]string{accountID, rnd, "default"}, "/"), nil
},
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "storage_class"},
},
},
})
}
Expand Down Expand Up @@ -172,6 +177,15 @@ func TestAccCloudflareR2Bucket_Jurisdiction(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "id", rnd),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: func(*terraform.State) (string, error) {
return strings.Join([]string{accountID, rnd, "eu"}, "/"), nil
},
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"storage_class"},
},
},
})
}
Expand Down

0 comments on commit 3c5eef5

Please sign in to comment.