Skip to content

Commit

Permalink
resource/cloudflare_zone_cache_reserve: add a new resource type
Browse files Browse the repository at this point in the history
Add a new resource that can be used to manage a Cache Reserve feature of
a given zone.

While at it, add a new data source to complete the newly added resource
so that the current status of the Cache Reserve feature can be read for
a given zone.

Signed-off-by: Krzysztof Wilczyński <[email protected]>
  • Loading branch information
kwilczynski committed Jul 31, 2023
1 parent d5886e9 commit 1f0589f
Show file tree
Hide file tree
Showing 18 changed files with 700 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changelog/2642.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
cloudflare_zone_cache_reserve
```

```release-note:new-data-source
cloudflare_zone_cache_reserve
```
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 4.12.0 (Unreleased)

FEATURES:

* **New Data Source:** `cloudflare_zone_cache_reserve` ([#2642](https://github.com/cloudflare/terraform-provider-cloudflare/issues/2642))
* **New Resource:** `cloudflare_zone_cache_reserve` ([#2642](https://github.com/cloudflare/terraform-provider-cloudflare/issues/2642))

ENHANCEMENTS:

* resource/cloudflare_user_agent_blocking_rules: add support for importing resources ([#2640](https://github.com/cloudflare/terraform-provider-cloudflare/issues/2640))
Expand Down
37 changes: 37 additions & 0 deletions docs/data-sources/zone_cache_reserve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
page_title: "cloudflare_zone_cache_reserve Data Source - Cloudflare"
subcategory: ""
description: |-
Provides a Cloudflare data source to look up Cache Reserve
status for a given zone.
Requires Cache Reserve subscription.
---

# cloudflare_zone_cache_reserve (Data Source)

~> Requires Cache Reserve subscription.

Provides a Cloudflare data source to look up [Cache Reserve][cache-reserve]
status for a given zone.

[cache-reserve]: https://developers.cloudflare.com/cache/advanced-configuration/cache-reserve

## Example Usage

```terraform
data "cloudflare_zone_cache_reserve" "example" {
zone_id = "0da42c8d2132a9ddaf714f9e7c920711"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `zone_id` (String) The zone identifier to target for the resource.

### Read-Only

- `enabled` (Boolean) The status of Cache Reserve support.
- `id` (String) The ID of this resource.
45 changes: 45 additions & 0 deletions docs/resources/zone_cache_reserve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
page_title: "cloudflare_zone_cache_reserve Resource - Cloudflare"
subcategory: ""
description: |-
Provides a Cloudflare Cache Reserve resource. Cache Reserve can
increase cache lifetimes by automatically storing all cacheable
files in Cloudflare's persistent object storage buckets.
Note: Using Cache Reserve without Tiered Cache is not recommended.
Requires Cache Reserve subscription.
---

# cloudflare_zone_cache_reserve (Resource)

~> Requires Cache Reserve subscription.

Provides a Cloudflare [Cache Reserve][cache-reserve] resource. Cache
Reserve can increase cache lifetimes by automatically storing all
cacheable files in Cloudflare's persistent object storage buckets.

-> Using Cache Reserve without [Tiered Cache][tiered-cache] is not recommended.

[cache-reserve]: https://developers.cloudflare.com/cache/advanced-configuration/cache-reserve
[tiered-cache]: https://developers.cloudflare.com/cache/how-to/tiered-cache

## Example Usage

```terraform
# Enable the Cache Reserve support for a given zone.
resource "cloudflare_zone_cache_variants" "example" {
zone_id = "0da42c8d2132a9ddaf714f9e7c920711"
enabled = true
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `enabled` (Boolean) Whether to enable or disable Cache Reserve support for a given zone.
- `zone_id` (String) The zone identifier to target for the resource. **Modifying this attribute will force creation of a new resource.**

### Read-Only

- `id` (String) The ID of this resource.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudflare_zone_cache_reserve" "example" {
zone_id = "0da42c8d2132a9ddaf714f9e7c920711"
}
1 change: 1 addition & 0 deletions examples/resources/cloudflare_zone_cache_reserve/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ terraform import cloudflare_zone_cache_reserve.example <zone_id>
5 changes: 5 additions & 0 deletions examples/resources/cloudflare_zone_cache_reserve/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Enable the Cache Reserve support for a given zone.
resource "cloudflare_zone_cache_variants" "example" {
zone_id = "0da42c8d2132a9ddaf714f9e7c920711"
enabled = true
}
71 changes: 71 additions & 0 deletions internal/sdkv2provider/data_source_zone_cache_reserve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package sdkv2provider

import (
"context"
"errors"

"github.com/MakeNowJust/heredoc/v2"
"github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudflareZoneCacheReserve() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudflareZoneCacheReserveRead,

Schema: map[string]*schema.Schema{
consts.ZoneIDSchemaKey: {
Type: schema.TypeString,
Required: true,
Description: consts.ZoneIDSchemaDescription,
ValidateFunc: func(value any, key string) (_ []string, errs []error) {
// Ensure that a valid Zone ID was passed.
if err := validateZoneID(value.(string)); err != nil {
errs = append(errs, err)
}
return
},
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "The status of Cache Reserve support.",
},
},
Description: heredoc.Doc(`
Provides a Cloudflare data source to look up Cache Reserve
status for a given zone.
Requires Cache Reserve subscription.
`),
}
}

func dataSourceCloudflareZoneCacheReserveRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
zoneID := d.Get(consts.ZoneIDSchemaKey).(string)

tflog.Info(ctx, "reading Cache Reserve", map[string]interface{}{
"zone_id": zoneID,
})

params := cloudflare.GetCacheReserveParams{}
output, err := client.GetCacheReserve(ctx, cloudflare.ZoneIdentifier(zoneID), params)
if err != nil {
var notFoundError *cloudflare.NotFoundError
if errors.As(err, &notFoundError) {
return diag.Errorf("unable to find zone: %s", zoneID)
}
return diag.Errorf("unable to read Cache Reserve for zone %q: %s", zoneID, err)
}

d.Set(consts.ZoneIDSchemaKey, zoneID)
d.Set("enabled", output.Value == cacheReserveEnabled)

d.SetId(stringChecksum(output.ModifiedOn.String()))

return nil
}
59 changes: 59 additions & 0 deletions internal/sdkv2provider/data_source_zone_cache_reserve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sdkv2provider

import (
"fmt"
"os"
"regexp"
"testing"

"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataCloudflareZoneCacheReserve_Simple(t *testing.T) {
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
name := fmt.Sprintf("data.cloudflare_zone_cache_reserve.%s", rnd)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccCloudflareZoneCacheReserveUpdate(t, zoneID, true)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDataCloudflareZoneCacheReserveConfig(zoneID, rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareZoneCacheReserveValuesUpdated(zoneID, true),
resource.TestCheckResourceAttrSet(name, consts.ZoneIDSchemaKey),
resource.TestCheckResourceAttr(name, "enabled", "true"),
),
},
},
})
}

func TestAccDataCloudflareZoneCacheReserve_Error(t *testing.T) {
rnd := generateRandomResourceName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDataCloudflareZoneCacheReserveConfig("this is a test", rnd),
ExpectError: regexp.MustCompile(regexp.QuoteMeta("must be a valid Zone ID, got: this is a test")),
},
},
})
}

func testAccDataCloudflareZoneCacheReserveConfig(zoneID, name string) string {
return fmt.Sprintf(`
data "cloudflare_zone_cache_reserve" "%[2]s" {
zone_id = "%[1]s"
}`, zoneID, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sdkv2provider

import (
"fmt"
"os"
"testing"

"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccCloudflareZoneCacheReserve_Import(t *testing.T) {
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_zone_cache_reserve.%s", rnd)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccCloudflareZoneCacheReserveUpdate(t, zoneID, true)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareZoneCacheReserveConfig(zoneID, rnd, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(name, consts.ZoneIDSchemaKey),
resource.TestCheckResourceAttr(name, "enabled", "false"),
),
},
{
ImportState: true,
ImportStateId: zoneID, // Ensure that a zone ID, not resource ID, is passed.
ImportStateVerify: true,
ResourceName: name,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(name, consts.ZoneIDSchemaKey),
resource.TestCheckResourceAttr(name, "enabled", "true"),
),
},
},
CheckDestroy: testAccCheckCloudflareZoneCacheReserveDestroy(zoneID),
})
}
2 changes: 2 additions & 0 deletions internal/sdkv2provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func New(version string) func() *schema.Provider {
"cloudflare_origin_ca_root_certificate": dataSourceCloudflareOriginCARootCertificate(),
"cloudflare_record": dataSourceCloudflareRecord(),
"cloudflare_rulesets": dataSourceCloudflareRulesets(),
"cloudflare_zone_cache_reserve": dataSourceCloudflareZoneCacheReserve(),
"cloudflare_zone_dnssec": dataSourceCloudflareZoneDNSSEC(),
"cloudflare_zone": dataSourceCloudflareZone(),
"cloudflare_zones": dataSourceCloudflareZones(),
Expand Down Expand Up @@ -263,6 +264,7 @@ func New(version string) func() *schema.Provider {
"cloudflare_worker_script": resourceCloudflareWorkerScript(),
"cloudflare_workers_kv_namespace": resourceCloudflareWorkersKVNamespace(),
"cloudflare_workers_kv": resourceCloudflareWorkerKV(),
"cloudflare_zone_cache_reserve": resourceCloudflareZoneCacheReserve(),
"cloudflare_zone_cache_variants": resourceCloudflareZoneCacheVariants(),
"cloudflare_zone_dnssec": resourceCloudflareZoneDNSSEC(),
"cloudflare_zone_lockdown": resourceCloudflareZoneLockdown(),
Expand Down
Loading

0 comments on commit 1f0589f

Please sign in to comment.