Skip to content

Commit

Permalink
SDK-95: cache_ttl_by_status support for Page Rules
Browse files Browse the repository at this point in the history
  • Loading branch information
patryk committed Jun 11, 2020
1 parent a522de3 commit cb21356
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cloudflare/resource_cloudflare_page_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,22 @@ func resourceCloudflarePageRule() *schema.Resource {
},
},
},
"cache_ttl_by_status": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"codes": {
Type: schema.TypeString,
Required: true,
},
"ttl": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -746,6 +762,30 @@ func transformFromCloudflarePageRuleAction(pageRuleAction *cloudflare.PageRuleAc
value = []interface{}{output}
break

case pageRuleAction.ID == "cache_ttl_by_status":
output := make([]map[string]interface{}, 0)

for key, value := range pageRuleAction.Value.(map[string]interface{}) {
entry := map[string]interface{}{"codes": key}

switch value := value.(type) {
case float64:
entry["ttl"] = int32(value)
case string:
switch value {
case "no-cache":
entry["ttl"] = 0
case "no-store":
entry["ttl"] = -1
}
}

output = append(output, entry)
}

value = output
break

default:
// User supplied ID is already validated, so this is always an internal error
err = fmt.Errorf("Unimplemented action ID %q - this is always an internal error", pageRuleAction.ID)
Expand Down Expand Up @@ -845,6 +885,21 @@ func transformToCloudflarePageRuleAction(id string, value interface{}, d *schema
output[sectionID] = sectionOutput
}

pageRuleAction.Value = output
}
} else if id == "cache_ttl_by_status" {
cacheTTLActionSchema := value.(*schema.Set)

log.Printf("[DEBUG] cache_ttl_by_status action to be applied: %#v", cacheTTLActionSchema)

if cacheTTLActionSchema.Len() != 0 {
output := make(map[string]int)

for _, code := range cacheTTLActionSchema.List() {
code := code.(map[string]interface{})
output[code["codes"].(string)] = code["ttl"].(int)
}

pageRuleAction.Value = output
}
} else {
Expand Down
51 changes: 51 additions & 0 deletions cloudflare/resource_cloudflare_page_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,28 @@ func TestAccCloudflarePageRuleCacheKeyFields2(t *testing.T) {
})
}

func TestAccCloudflarePageRuleCacheTTLByStatus(t *testing.T) {
var pageRule cloudflare.PageRule
domain := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
target := fmt.Sprintf("test-cache-ttl-by-status.%s", domain)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudflarePageRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflarePageRuleConfigCacheTTLByStatus(zoneID, target),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists("cloudflare_page_rule.test", &pageRule),
testAccCheckCloudflarePageRuleAttributesBasic(&pageRule),
),
},
},
})
}

func testAccCheckCloudflarePageRuleRecreated(before, after *cloudflare.PageRule) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before.ID == after.ID {
Expand Down Expand Up @@ -767,6 +789,35 @@ resource "cloudflare_page_rule" "test" {
}`, zoneID, target)
}

func testAccCheckCloudflarePageRuleConfigCacheTTLByStatus(zoneID, target string) string {
return fmt.Sprintf(`
resource "cloudflare_page_rule" "test" {
zone_id = "%s"
target = "%s"
actions {
cache_ttl_by_status {
codes = "200-299"
ttl = 300
}
cache_ttl_by_status {
codes = "300-399"
ttl = 60
}
cache_ttl_by_status {
codes = "400-403"
ttl = -1
}
cache_ttl_by_status {
codes = "404-499"
ttl = 30
}
cache_ttl_by_status {
codes = "500-599"
ttl = 0
}
}
}`, zoneID, target)
}
func buildPageRuleConfig(resourceName string, actions string) string {
domain := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
Expand Down

0 comments on commit cb21356

Please sign in to comment.