Skip to content

Commit

Permalink
feat(r2_bucket): support jurisdiction handling
Browse files Browse the repository at this point in the history
Manually updates the client to handle managing the jurisdiction while we
incorporate HTTP header handling into the pipeline.
  • Loading branch information
jacobbednarz committed Jan 6, 2025
1 parent 797fc53 commit 0ba79f6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/services/r2_bucket/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type R2BucketModel struct {
Name types.String `tfsdk:"name" json:"name,required"`
AccountID types.String `tfsdk:"account_id" path:"account_id,required"`
Location types.String `tfsdk:"location" json:"locationHint,optional"`
Jurisdiction types.String `tfsdk:"jurisdiction" json:"-,computed_optional"`
StorageClass types.String `tfsdk:"storage_class" json:"storageClass,computed_optional"`
CreationDate types.String `tfsdk:"creation_date" json:"creation_date,computed"`
}
Expand Down
6 changes: 6 additions & 0 deletions internal/services/r2_bucket/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var _ resource.ResourceWithConfigure = (*R2BucketResource)(nil)
var _ resource.ResourceWithModifyPlan = (*R2BucketResource)(nil)
var _ resource.ResourceWithImportState = (*R2BucketResource)(nil)

const jurisdictionHTTPHeaderName = "cf-r2-jurisdiction"

func NewResource() resource.Resource {
return &R2BucketResource{}
}
Expand Down Expand Up @@ -76,6 +78,7 @@ func (r *R2BucketResource) Create(ctx context.Context, req resource.CreateReques
r2.BucketNewParams{
AccountID: cloudflare.F(data.AccountID.ValueString()),
},
option.WithHeader(jurisdictionHTTPHeaderName, data.Jurisdiction.ValueString()),
option.WithRequestBody("application/json", dataBytes),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
Expand Down Expand Up @@ -125,6 +128,7 @@ func (r *R2BucketResource) Update(ctx context.Context, req resource.UpdateReques
r2.BucketNewParams{
AccountID: cloudflare.F(data.Name.ValueString()),
},
option.WithHeader(jurisdictionHTTPHeaderName, data.Jurisdiction.ValueString()),
option.WithRequestBody("application/json", dataBytes),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
Expand Down Expand Up @@ -162,6 +166,7 @@ func (r *R2BucketResource) Read(ctx context.Context, req resource.ReadRequest, r
r2.BucketGetParams{
AccountID: cloudflare.F(data.AccountID.ValueString()),
},
option.WithHeader(jurisdictionHTTPHeaderName, data.Jurisdiction.ValueString()),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
)
Expand Down Expand Up @@ -201,6 +206,7 @@ func (r *R2BucketResource) Delete(ctx context.Context, req resource.DeleteReques
r2.BucketDeleteParams{
AccountID: cloudflare.F(data.AccountID.ValueString()),
},
option.WithHeader(jurisdictionHTTPHeaderName, data.Jurisdiction.ValueString()),
option.WithMiddleware(logging.Middleware(ctx)),
)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions internal/services/r2_bucket/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,37 @@ func TestAccCloudflareR2Bucket_Minimum(t *testing.T) {
})
}

func TestAccCloudflareR2Bucket_Jurisdiction(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
resourceName := "cloudflare_r2_bucket." + rnd

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.TestAccPreCheck(t)
acctest.TestAccPreCheck_AccountID(t)
},
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareR2BucketJurisdiction(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", rnd),
resource.TestCheckResourceAttr(resourceName, "id", rnd),
),
},
},
})
}

func testAccCheckCloudflareR2BucketMinimum(rnd, accountID string) string {
return acctest.LoadTestCase("r2bucketminimum.tf", rnd, accountID)
}

func testAccCheckCloudflareR2BucketBasic(rnd, accountID string) string {
return acctest.LoadTestCase("r2bucketbasic.tf", rnd, accountID)
}

func testAccCheckCloudflareR2BucketJurisdiction(rnd, accountID string) string {
return acctest.LoadTestCase("jurisdiction.tf", rnd, accountID)
}
13 changes: 13 additions & 0 deletions internal/services/r2_bucket/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func ResourceSchema(ctx context.Context) schema.Schema {
),
},
},
"jurisdiction": schema.StringAttribute{
Description: "Jurisdiction of the bucket",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("default"),
Validators: []validator.String{
stringvalidator.OneOfCaseInsensitive(
"default",
"eu",
"fedramp",
),
},
},
"storage_class": schema.StringAttribute{
Description: "Storage class for newly uploaded objects, unless specified otherwise.",
Computed: true,
Expand Down
5 changes: 5 additions & 0 deletions internal/services/r2_bucket/testdata/jurisdiction.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "cloudflare_r2_bucket" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
jurisdiction = "eu"
}

0 comments on commit 0ba79f6

Please sign in to comment.