-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resource/cloudflare_zone_cache_reserve: add a new resource type
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
1 parent
d5886e9
commit 1f0589f
Showing
18 changed files
with
700 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
3 changes: 3 additions & 0 deletions
3
examples/data-sources/cloudflare_zone_cache_reserve/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "cloudflare_zone_cache_reserve" "example" { | ||
zone_id = "0da42c8d2132a9ddaf714f9e7c920711" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$ terraform import cloudflare_zone_cache_reserve.example <zone_id> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¬FoundError) { | ||
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
59
internal/sdkv2provider/data_source_zone_cache_reserve_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
44 changes: 44 additions & 0 deletions
44
internal/sdkv2provider/import_resource_cloudflare_zone_cache_reserve_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.