Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for manipulation, creating and deletion of tiered cache settings through the existing mechanisms in the Cloudflare API. This adds support for settings generic and smart tiered cache topologies through the terraform provider. Implementation: * Create/Update, Delete and Get methods for Tiered Cache settings
- Loading branch information
Mikey Sleevi
committed
Dec 22, 2022
1 parent
2cbee97
commit 6697d85
Showing
5 changed files
with
164 additions
and
0 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,3 @@ | ||
```release-note:new-resource | ||
cloudflare_tiered_cache | ||
``` |
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,76 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/MakeNowJust/heredoc/v2" | ||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceCloudflareTieredCache() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: resourceCloudflareTieredCacheSchema(), | ||
ReadContext: resourceCloudflareTieredCacheRead, | ||
UpdateContext: resourceCloudflareTieredCacheUpdate, | ||
CreateContext: resourceCloudflareTieredCacheUpdate, | ||
DeleteContext: resourceCloudflareTieredCacheDelete, | ||
Description: heredoc.Doc(` | ||
Provides a resource, that manages Cloudflare Tiered Cache settings. | ||
This allows you to adjust topologies between Off, Smart and Generic for your zone. | ||
`), | ||
} | ||
} | ||
|
||
func resourceCloudflareTieredCacheUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
zoneID := d.Get("zone_id").(string) | ||
var cacheType cloudflare.TieredCacheType | ||
switch d.Get("cache_type").(string) { | ||
case "smart": | ||
cacheType = cloudflare.TieredCacheSmart | ||
break | ||
case "generic": | ||
cacheType = cloudflare.TieredCacheGeneric | ||
break | ||
case "off": | ||
cacheType = cloudflare.TieredCacheOff | ||
break | ||
default: | ||
return diag.FromErr(fmt.Errorf("error updating tiered cache settings: Unsupported cache type requested")) | ||
} | ||
|
||
_, err := client.SetTieredCache(ctx, cloudflare.ZoneIdentifier(zoneID), cacheType) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error updating tiered cache settings: %w", err)) | ||
} | ||
d.SetId(zoneID) | ||
return resourceCloudflareTieredCacheRead(ctx, d, meta) | ||
} | ||
|
||
func resourceCloudflareTieredCacheRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
zoneID := d.Get("zone_id").(string) | ||
|
||
result, err := client.GetTieredCache(ctx, cloudflare.ZoneIdentifier(zoneID)) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error retrieving tiered cache settings: %w", err)) | ||
} | ||
d.SetId(zoneID) | ||
d.Set("cache_type", result.Type) | ||
return nil | ||
} | ||
|
||
func resourceCloudflareTieredCacheDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*cloudflare.API) | ||
zoneID := d.Get("zone_id").(string) | ||
|
||
_, err := client.DeleteTieredCache(ctx, cloudflare.ZoneIdentifier(zoneID)) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error creating deleting tiered cache configuration: %w", err)) | ||
} | ||
|
||
return nil | ||
} |
58 changes: 58 additions & 0 deletions
58
internal/provider/resource_cloudflare_tiered_cache_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,58 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func testTieredCacheConfig(rnd, zoneId, cacheType string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_tiered_cache" "%[1]s" { | ||
zone_id = "%[2]s" | ||
cache_type "%[3]s" | ||
} | ||
`, rnd, zoneID, cacheType) | ||
} | ||
|
||
func TestCloudflareSmartTieredCache(t *testing.T) { | ||
rnd := generateRandomResourceName() | ||
name := "cloudflare_tiered_cache." + rnd | ||
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: providerFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testTieredCacheConfig(rnd, zoneID, "smart"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(name, "zone_id", zoneID), | ||
resource.TestCheckResourceAttr(name, "cache_type", "smart"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestCloudflareGenericTieredCache(t *testing.T) { | ||
rnd := generateRandomResourceName() | ||
name := "cloudflare_tiered_cache." + rnd | ||
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: providerFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testTieredCacheConfig(rnd, zoneID, "generic"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(name, "zone_id", zoneID), | ||
resource.TestCheckResourceAttr(name, "cache_type", "generic"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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,26 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func resourceCloudflareTieredCacheSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"zone_id": { | ||
Description: "The zone identifier to target for the resource.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"cache_type": { | ||
Description: fmt.Sprintf("The typed of tiered cache to utilize on the zone. %s", renderAvailableDocumentationValuesStringSlice([]string{"generic", "smart"})), | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringInSlice([]string{"generic", "smart"}, false), | ||
}, | ||
} | ||
} |