Skip to content

Commit

Permalink
page_rule: Remove HasChange for edge_cache_ttl (#453)
Browse files Browse the repository at this point in the history
Prior to swapping to PUT requests for updating page rules, we used to
perform manual diff checks that would determine which actions we would
add to the page rule before sending it. This is no longer now as we
swapped to using a PUT request which replaces the whole rule and we are
able to send the whole page rule as-is without doing the work ourselves.

This popped up as it's caused a bug whereby updating a page rule that
includes a `edge_cache_ttl` (but not change it) results in the
`edge_cache_ttl` being dropped completely.

`TestAccCloudflarePageRuleEdgeCacheTTLNotClobbered` is the regression
test case that demonstrates the behaviour.
  • Loading branch information
jacobbednarz authored and patryk committed Aug 22, 2019
1 parent d1b586b commit 79c359a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
4 changes: 1 addition & 3 deletions cloudflare/resource_cloudflare_page_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,6 @@ func transformToCloudflarePageRuleAction(id string, value interface{}, d *schema

pageRuleAction.ID = id

changed := d.HasChange(fmt.Sprintf("actions.0.%s", id))

if strValue, ok := value.(string); ok {
if id == "browser_cache_ttl" {
intValue, err := strconv.Atoi(strValue)
Expand All @@ -633,7 +631,7 @@ func transformToCloudflarePageRuleAction(id string, value interface{}, d *schema
}
}
} else if intValue, ok := value.(int); ok {
if id == "edge_cache_ttl" && intValue > 0 && changed {
if id == "edge_cache_ttl" && intValue > 0 {
pageRuleAction.Value = intValue
} else {
pageRuleAction.Value = nil
Expand Down
53 changes: 53 additions & 0 deletions cloudflare/resource_cloudflare_page_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,34 @@ func TestAccCloudflarePageRule_DeletesBrowserCacheTTLThatRespectsExistingHeaders
})
}

func TestAccCloudflarePageRuleEdgeCacheTTLNotClobbered(t *testing.T) {
var before, after cloudflare.PageRule
zone := os.Getenv("CLOUDFLARE_DOMAIN")
target := fmt.Sprintf("test-edge-cache-ttl-not-clobbered.%s", zone)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudflarePageRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflarePageRuleConfigWithEdgeCacheTtl(zone, target),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists("cloudflare_page_rule.test", &before),
resource.TestCheckResourceAttr("cloudflare_page_rule.test", "actions.0.edge_cache_ttl", "10"),
),
},
{
Config: testAccCheckCloudflarePageRuleConfigWithEdgeCacheTtlAndAlwaysOnline(zone, target),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists("cloudflare_page_rule.test", &after),
resource.TestCheckResourceAttr("cloudflare_page_rule.test", "actions.0.edge_cache_ttl", "10"),
),
},
},
})
}

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

func testAccCheckCloudflarePageRuleConfigWithEdgeCacheTtl(zone, target string) string {
return fmt.Sprintf(`
resource "cloudflare_page_rule" "test" {
zone = "%s"
target = "%s"
actions {
always_online = "on"
ssl = "flexible"
edge_cache_ttl = 10
}
}`, zone, target)
}

func testAccCheckCloudflarePageRuleConfigWithEdgeCacheTtlAndAlwaysOnline(zone, target string) string {
return fmt.Sprintf(`
resource "cloudflare_page_rule" "test" {
zone = "%s"
target = "%s"
actions {
always_online = "on"
edge_cache_ttl = 10
}
}`, zone, target)
}

func buildPageRuleConfig(resourceName string, actions string) string {
zone := os.Getenv("CLOUDFLARE_DOMAIN")
target := fmt.Sprintf("terraform-test.%s", zone)
Expand Down

0 comments on commit 79c359a

Please sign in to comment.