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

Remove empty cookie, header fields from page rules - cache by custom key #2208

Merged
merged 1 commit into from
Feb 1, 2023
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
3 changes: 3 additions & 0 deletions .changelog/2208.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_page_rule: remove empty cookie and header fields when applying this resource
```
36 changes: 28 additions & 8 deletions internal/sdkv2provider/resource_cloudflare_page_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,25 @@ func transformFromCloudflarePageRuleAction(pageRuleAction *cloudflare.PageRuleAc

for sectionID, sectionValue := range pageRuleAction.Value.(map[string]interface{}) {
switch sectionID {
case "cookie", "header", "host", "user":
case "host", "user":
output[sectionID] = []interface{}{sectionValue}

case "cookie", "header":
fieldOutput := map[string]interface{}{}
for fieldID, fieldValue := range sectionValue.(map[string]interface{}) {
switch fieldValue.(type) {
case []interface{}:
if len(fieldValue.([]interface{})) > 0 {
fieldOutput[fieldID] = fieldValue
}
default:
fieldOutput[fieldID] = fieldValue
}
}
if len(fieldOutput) > 0 {
output[sectionID] = []interface{}{fieldOutput}
}

case "query_string":
fieldOutput := map[string]interface{}{}

Expand Down Expand Up @@ -422,11 +438,14 @@ func transformToCloudflarePageRuleAction(ctx context.Context, id string, value i

switch sectionID {
case "cookie", "header":
for fieldID, fieldValue := range sectionValue.([]interface{})[0].(map[string]interface{}) {
sectionOutput[fieldID] = fieldValue.(*schema.Set).List()
if len(sectionValue.([]interface{})) > 0 && sectionValue.([]interface{})[0] != nil {
for fieldID, fieldValue := range sectionValue.([]interface{})[0].(map[string]interface{}) {
sectionOutput[fieldID] = fieldValue.(*schema.Set).List()
}
output[sectionID] = sectionOutput
}
case "query_string":
if sectionValue.([]interface{})[0] != nil {
if len(sectionValue.([]interface{})) > 0 && sectionValue.([]interface{})[0] != nil {
for fieldID, fieldValue := range sectionValue.([]interface{})[0].(map[string]interface{}) {
switch fieldID {
case "exclude", "include":
Expand Down Expand Up @@ -464,12 +483,13 @@ func transformToCloudflarePageRuleAction(ctx context.Context, id string, value i

output[sectionID] = sectionOutput
default:
for fieldID, fieldValue := range sectionValue.([]interface{})[0].(map[string]interface{}) {
sectionOutput[fieldID] = fieldValue
if len(sectionValue.([]interface{})) > 0 {
for fieldID, fieldValue := range sectionValue.([]interface{})[0].(map[string]interface{}) {
sectionOutput[fieldID] = fieldValue
}
output[sectionID] = sectionOutput
}
}

output[sectionID] = sectionOutput
}

pageRuleAction.Value = output
Expand Down
56 changes: 56 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_page_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,39 @@ func TestAccCloudflarePageRuleCacheKeyFieldsIncludeMultipleValuesQueryString(t *
})
}

func TestAccCloudflarePageRule_EmptyCookie(t *testing.T) {
var pageRule cloudflare.PageRule
domain := os.Getenv("CLOUDFLARE_DOMAIN")
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
pageRuleTarget := fmt.Sprintf("%s.%s", rnd, domain)
resourceName := fmt.Sprintf("cloudflare_page_rule.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflarePageRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflarePageRuleEmtpyCookie(zoneID, rnd, pageRuleTarget),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflarePageRuleExists(resourceName, &pageRule),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.host.#", "1"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.query_string.#", "1"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.user.#", "1"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.cookie.#", "0"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.header.#", "0"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.host.0.resolved", "true"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.query_string.0.include.#", "0"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.user.0.device_type", "true"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.user.0.geo", "false"),
resource.TestCheckResourceAttr(resourceName, "actions.0.cache_key_fields.0.user.0.lang", "false"),
),
},
},
})
}

func TestAccCloudflarePageRuleCacheTTLByStatus(t *testing.T) {
var pageRule cloudflare.PageRule
domain := os.Getenv("CLOUDFLARE_DOMAIN")
Expand Down Expand Up @@ -1284,3 +1317,26 @@ func testAccCheckCloudflarePageRuleHasAction(pageRule *cloudflare.PageRule, key
return fmt.Errorf("cloudflare page rule action not found %#v:%#v\nAction State\n%#v", key, value, pageRule.Actions)
}
}

func testAccCheckCloudflarePageRuleEmtpyCookie(zoneID, rnd, target string) string {
return fmt.Sprintf(`
resource "cloudflare_page_rule" "%[3]s" {
zone_id = "%[1]s"
target = "%[3]s"
actions {
cache_key_fields {
host {
resolved = true
}
query_string {
ignore = true
}
user {
device_type = true
geo = false
lang = false
}
}
}
}`, zoneID, target, rnd)
}
6 changes: 3 additions & 3 deletions internal/sdkv2provider/schema_cloudflare_page_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func resourceCloudflarePageRuleSchema() map[string]*schema.Schema {

"host": {
Type: schema.TypeList,
Optional: true,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Expand All @@ -366,7 +366,7 @@ func resourceCloudflarePageRuleSchema() map[string]*schema.Schema {

"query_string": {
Type: schema.TypeList,
Optional: true,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -398,7 +398,7 @@ func resourceCloudflarePageRuleSchema() map[string]*schema.Schema {

"user": {
Type: schema.TypeList,
Optional: true,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down