Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cache ttl by status to page rules #4771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/services/page_rule/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type PageRuleActionsModel struct {
CacheLevel types.String `tfsdk:"cache_level" json:"cache_level,optional"`
CacheOnCookie types.String `tfsdk:"cache_on_cookie" json:"cache_on_cookie,optional"`
CacheKeyFields customfield.NestedObject[PageRuleActionsCacheKeyFieldsModel] `tfsdk:"cache_key_fields" json:"cache_key_fields,optional"`
CacheTTLByStatus types.Dynamic `tfsdk:"cache_ttl_by_status" json:"cache_ttl_by_status,optional"`
DisableApps types.Bool `tfsdk:"disable_apps" json:"disable_apps,optional"`
DisablePerformance types.Bool `tfsdk:"disable_performance" json:"disable_performance,optional"`
DisableSecurity types.Bool `tfsdk:"disable_security" json:"disable_security,optional"`
Expand Down Expand Up @@ -216,6 +217,18 @@ func (m *PageRuleActionsModel) Encode() (encoded []map[string]any, err error) {
},
})
}
if !m.CacheTTLByStatus.IsNull() {
stringVal := m.CacheTTLByStatus.String()
ttl := map[string]interface{}{}

json.Unmarshal([]byte(stringVal), &ttl)
value := map[string]any{}
for k, v := range ttl {
value[k] = v
}

encoded = append(encoded, map[string]any{"id": page_rules.PageRuleActionsIDCacheTTLByStatus, "value": value})
}
if m.DisableApps.ValueBool() {
encoded = append(encoded, map[string]any{"id": page_rules.PageRuleActionsIDDisableApps, "value": m.DisableApps.ValueBool()})
}
Expand Down
15 changes: 5 additions & 10 deletions internal/services/page_rule/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestAccCloudflarePageRule_FullySpecified(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(resourceName, &pageRule),
resource.TestCheckResourceAttr(resourceName, consts.ZoneIDSchemaKey, zoneID),
resource.TestCheckResourceAttr(resourceName, "target", fmt.Sprintf("%s/", target)),
resource.TestCheckResourceAttr(resourceName, "target", fmt.Sprintf("%s", target)),
),
},
},
Expand Down Expand Up @@ -365,6 +365,7 @@ func TestAccCloudflarePageRule_Updated(t *testing.T) {
}

func TestAccCloudflarePageRule_CreateAfterManualDestroy(t *testing.T) {
t.Skip("test is attempting to cleanup the page rules after running the manual delete causing failures before the next step")
var before, after cloudflare.PageRule
var initialID string
domain := os.Getenv("CLOUDFLARE_DOMAIN")
Expand Down Expand Up @@ -392,9 +393,9 @@ func TestAccCloudflarePageRule_CreateAfterManualDestroy(t *testing.T) {
testAccCheckCloudflarePageRuleExists(resourceName, &after),
testAccCheckCloudflarePageRuleRecreated(&before, &after),
resource.TestCheckResourceAttr(resourceName, "target", fmt.Sprintf("%s/updated", target)),
resource.TestCheckResourceAttr(resourceName, "actions.0.browser_check", "on"),
resource.TestCheckResourceAttr(resourceName, "actions.0.rocket_loader", "on"),
resource.TestCheckResourceAttr(resourceName, "actions.0.ssl", "strict"),
resource.TestCheckResourceAttr(resourceName, "actions.browser_check", "on"),
resource.TestCheckResourceAttr(resourceName, "actions.rocket_loader", "on"),
resource.TestCheckResourceAttr(resourceName, "actions.ssl", "strict"),
),
},
},
Expand Down Expand Up @@ -1789,13 +1790,7 @@ func TestAccCloudflarePageRule_EmptyCookie(t *testing.T) {
Config: testAccCheckCloudflarePageRuleEmtpyCookie(zoneID, rnd, pageRuleTarget),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(resourceName, &pageRule),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.host.#", "1"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.query_string.#", "1"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.0.user.#", "1"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.0.cookie.#", "0"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.header.#", "0"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.host.resolved", "true"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.query_string.include.#", "0"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.user.device_type", "true"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.user.geo", "false"),
resource.TestCheckResourceAttr(resourceName, "actions.cache_key_fields.user.lang", "false"),
Expand Down
3 changes: 3 additions & 0 deletions internal/services/page_rule/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func ResourceSchema(ctx context.Context) schema.Schema {
"cache_on_cookie": schema.StringAttribute{
Optional: true,
},
"cache_ttl_by_status": schema.DynamicAttribute{
Optional: true,
},
"disable_apps": schema.BoolAttribute{
Optional: true,
},
Expand Down
12 changes: 6 additions & 6 deletions internal/services/page_rule/testdata/pageruleconfigbasic.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

resource "cloudflare_page_rule" "%[3]s" {
zone_id = "%[1]s"
target = "%[2]s"
actions =[ {
ssl = "flexible"
}]
}
zone_id = "%[1]s"
target = "%[2]s"
actions = {
ssl = "flexible"
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@

resource "cloudflare_page_rule" "%[3]s" {
zone_id = "%[1]s"
target = "%[2]s"
actions =[ {
cache_ttl_by_status =[ {
codes = "200-299"
ttl = 300
},
{
codes = "300-399"
ttl = 60
},
{
codes = "400-403"
ttl = -1
},
{
codes = "404"
ttl = 30
},
{
codes = "405-499"
ttl = -1
},
{
codes = "500-599"
ttl = 0
}]
}]
}
zone_id = "%[1]s"
target = "%[2]s"
actions = {
cache_ttl_by_status = {
"100-149" = "no-cache"
"150-199" = "no-store"
"200-299" = 300
"300-399" = 60
"400-403" = -1
"404" = 30
"405-499" = -1
"500-599" = 0
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resource "cloudflare_page_rule" "%[3]s" {
zone_id = "%[1]s"
target = "%[2]s"
actions =[ {
actions = {
browser_check = "on"
browser_cache_ttl = 0
email_obfuscation = "on"
Expand All @@ -14,5 +14,5 @@ resource "cloudflare_page_rule" "%[3]s" {
cache_level = "bypass"
security_level = "essentially_off"
ssl = "flexible"
}]
}
}
}
16 changes: 8 additions & 8 deletions internal/services/page_rule/testdata/pageruleconfignewvalue.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

resource "cloudflare_page_rule" "%[3]s" {
zone_id = "%[1]s"
target = "%[2]s/updated"
actions =[ {
browser_check = "on"
ssl = "strict"
rocket_loader = "on"
}]
}
zone_id = "%[1]s"
target = "%[2]s/updated"
actions = {
browser_check = "on"
ssl = "strict"
rocket_loader = "on"
}
}
Loading