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

Fix browser_cache_ttl page rule action bugs #379

Merged
merged 15 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 14 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
46 changes: 46 additions & 0 deletions cloudflare/import_cloudflare_page_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,49 @@ func TestAccCloudflarePageRule_Import(t *testing.T) {
},
})
}

func TestAccCloudflarePageRule_ImportWithBrowserCacheTTL30(t *testing.T) {
var pageRule cloudflare.PageRule
zone := os.Getenv("CLOUDFLARE_DOMAIN")
name := "cloudflare_page_rule.test"
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Config: buildPageRuleConfig("test", "browser_cache_ttl = 30"),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(name, &pageRule),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(name, &pageRule),
),
},
})
}

func TestAccCloudflarePageRule_ImportWithoutBrowserCacheTTL(t *testing.T) {
var pageRule cloudflare.PageRule
zone := os.Getenv("CLOUDFLARE_DOMAIN")
name := "cloudflare_page_rule.test"
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Config: buildPageRuleConfig("test", `browser_check = "on"`),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(name, &pageRule),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zone),
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(name, &pageRule),
),
},
})
}
26 changes: 17 additions & 9 deletions cloudflare/resource_cloudflare_page_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cloudflare
import (
"fmt"
"log"

"strconv"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
Expand Down Expand Up @@ -206,9 +206,8 @@ func resourceCloudflarePageRule() *schema.Resource {
},

"browser_cache_ttl": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtMost(31536000),
Type: schema.TypeString,
Optional: true,
},

"edge_cache_ttl": {
Expand Down Expand Up @@ -542,9 +541,11 @@ var pageRuleAPINilFields = []string{
"disable_security",
}
var pageRuleAPIFloatFields = []string{
"browser_cache_ttl",
"edge_cache_ttl",
}
var pageRuleAPIFloatAsStringFields = []string{
"browser_cache_ttl",
}
var pageRuleAPIStringFields = []string{
"bypass_cache_on_cookie",
"cache_key",
Expand Down Expand Up @@ -578,6 +579,10 @@ func transformFromCloudflarePageRuleAction(pageRuleAction *cloudflare.PageRuleAc
value = pageRuleAction.Value.(string)
break

case contains(pageRuleAPIFloatAsStringFields, pageRuleAction.ID):
stevehodgkiss marked this conversation as resolved.
Show resolved Hide resolved
value = fmt.Sprintf("%.0f", pageRuleAction.Value.(float64))
break

case pageRuleAction.ID == "forwarding_url" || pageRuleAction.ID == "minify":
value = []interface{}{pageRuleAction.Value.(map[string]interface{})}
break
Expand All @@ -596,7 +601,12 @@ func transformToCloudflarePageRuleAction(id string, value interface{}, d *schema
changed := d.HasChange(fmt.Sprintf("actions.0.%s", id))

if strValue, ok := value.(string); ok {
if strValue == "" && !changed {
if id == "browser_cache_ttl" && changed {
intValue, err := strconv.Atoi(strValue)
if err == nil {
pageRuleAction.Value = intValue
}
} else if strValue == "" && !changed {
pageRuleAction.Value = nil
} else {
pageRuleAction.Value = strValue
Expand All @@ -616,9 +626,7 @@ func transformToCloudflarePageRuleAction(id string, value interface{}, d *schema
}
}
} else if intValue, ok := value.(int); ok {
if id == "browser_cache_ttl" && changed {
pageRuleAction.Value = intValue
} else if id == "edge_cache_ttl" && intValue > 0 && changed {
if id == "edge_cache_ttl" && intValue > 0 && changed {
pageRuleAction.Value = intValue
} else {
pageRuleAction.Value = nil
Expand Down
103 changes: 101 additions & 2 deletions cloudflare/resource_cloudflare_page_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package cloudflare
import (
"fmt"
"os"
"testing"

"reflect"
"regexp"
"testing"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -270,6 +269,67 @@ func TestTranformForwardingURL(t *testing.T) {
}
}

func TestAccCloudflarePageRule_CreatesBrowserCacheTTLIntegerValues(t *testing.T) {
var pageRule cloudflare.PageRule
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Config: buildPageRuleConfig("test", "browser_cache_ttl = 1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists("cloudflare_page_rule.test", &pageRule),
testAccCheckCloudflarePageRuleHasAction(&pageRule, "browser_cache_ttl", float64(1)),
resource.TestCheckResourceAttr("cloudflare_page_rule.test", "actions.0.browser_cache_ttl", "1"),
),
},
})
}

func TestAccCloudflarePageRule_CreatesBrowserCacheTTLThatRespectsExistingHeaders(t *testing.T) {
var pageRule cloudflare.PageRule
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Config: buildPageRuleConfig("test", "browser_cache_ttl = 0"),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists("cloudflare_page_rule.test", &pageRule),
resource.TestCheckResourceAttr("cloudflare_page_rule.test", "actions.0.browser_cache_ttl", "0"),
testAccCheckCloudflarePageRuleHasAction(&pageRule, "browser_cache_ttl", float64(0)),
),
},
})
}

func TestAccCloudflarePageRule_UpdatesBrowserCacheTTLThatRespectsExistingHeaders(t *testing.T) {
var pageRule cloudflare.PageRule
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Config: buildPageRuleConfig("test", "browser_cache_ttl = 1"),
},
{
Config: buildPageRuleConfig("test", "browser_cache_ttl = 0"),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists("cloudflare_page_rule.test", &pageRule),
testAccCheckCloudflarePageRuleHasAction(&pageRule, "browser_cache_ttl", float64(0)),
resource.TestCheckResourceAttr("cloudflare_page_rule.test", "actions.0.browser_cache_ttl", "0"),
),
},
})
}

func TestAccCloudflarePageRule_DeletesBrowserCacheTTLThatRespectsExistingHeaders(t *testing.T) {
var pageRule cloudflare.PageRule
testAccRunResourceTestSteps(t, []resource.TestStep{
{
Config: buildPageRuleConfig("test", "browser_cache_ttl = 0"),
},
{
Config: buildPageRuleConfig("test", `browser_check = "on"`),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists("cloudflare_page_rule.test", &pageRule),
resource.TestCheckResourceAttr("cloudflare_page_rule.test", "actions.0.browser_cache_ttl", ""),
),
},
})
}

func testAccCheckCloudflarePageRuleRecreated(before, after *cloudflare.PageRule) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before.ID == after.ID {
Expand Down Expand Up @@ -481,6 +541,7 @@ resource "cloudflare_page_rule" "test" {
actions {
always_online = "on"
browser_check = "on"
browser_cache_ttl = 0
email_obfuscation = "on"
ip_geolocation = "on"
server_side_exclude = "on"
Expand Down Expand Up @@ -523,3 +584,41 @@ resource "cloudflare_page_rule" "test" {
}
}`, zone, target)
}

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

return fmt.Sprintf(`
resource "cloudflare_page_rule" "%s" {
zone = "%s"
target = "%s"
actions {
%s
}
}`,
resourceName,
zone,
target,
actions)
}

func testAccRunResourceTestSteps(t *testing.T, testSteps []resource.TestStep) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudflarePageRuleDestroy,
Steps: testSteps,
})
}

func testAccCheckCloudflarePageRuleHasAction(pageRule *cloudflare.PageRule, key string, value interface{}) resource.TestCheckFunc {
return func(state *terraform.State) error {
for _, pageRuleAction := range pageRule.Actions {
if pageRuleAction.ID == key && pageRuleAction.Value == value {
return nil
}
}
return fmt.Errorf("cloudflare page rule action not found %#v:%#v\nAction State\n%#v", key, value, pageRule.Actions)
}
}