Skip to content

Commit

Permalink
Remove cookie, header fields when they are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tamas-jozsa committed Feb 1, 2023
1 parent 77a8bd9 commit 4b3b7b2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
32 changes: 26 additions & 6 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,16 @@ 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()
}
}
if len(sectionOutput) > 0 {
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 @@ -467,9 +488,8 @@ func transformToCloudflarePageRuleAction(ctx context.Context, id string, value i
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)
}

0 comments on commit 4b3b7b2

Please sign in to comment.