From 671657fb5f8345d522e03554295b65c8e3db6523 Mon Sep 17 00:00:00 2001 From: Tamas Jozsa Date: Wed, 1 Feb 2023 13:45:02 +0000 Subject: [PATCH] Remove cookie, header fields when they are empty --- .../resource_cloudflare_page_rule.go | 32 +++++++++-- .../resource_cloudflare_page_rule_test.go | 56 +++++++++++++++++++ .../schema_cloudflare_page_rule.go | 6 +- 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/internal/sdkv2provider/resource_cloudflare_page_rule.go b/internal/sdkv2provider/resource_cloudflare_page_rule.go index 51d2ed5c51c..76ca7283d05 100644 --- a/internal/sdkv2provider/resource_cloudflare_page_rule.go +++ b/internal/sdkv2provider/resource_cloudflare_page_rule.go @@ -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{}{} @@ -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": @@ -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 diff --git a/internal/sdkv2provider/resource_cloudflare_page_rule_test.go b/internal/sdkv2provider/resource_cloudflare_page_rule_test.go index 9fde14c26dc..6e79d383d91 100644 --- a/internal/sdkv2provider/resource_cloudflare_page_rule_test.go +++ b/internal/sdkv2provider/resource_cloudflare_page_rule_test.go @@ -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") @@ -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) +} diff --git a/internal/sdkv2provider/schema_cloudflare_page_rule.go b/internal/sdkv2provider/schema_cloudflare_page_rule.go index 80a8304f536..0c3bffb3424 100644 --- a/internal/sdkv2provider/schema_cloudflare_page_rule.go +++ b/internal/sdkv2provider/schema_cloudflare_page_rule.go @@ -350,7 +350,7 @@ func resourceCloudflarePageRuleSchema() map[string]*schema.Schema { "host": { Type: schema.TypeList, - Optional: true, + Optional: false, MinItems: 1, MaxItems: 1, Elem: &schema.Resource{ @@ -366,7 +366,7 @@ func resourceCloudflarePageRuleSchema() map[string]*schema.Schema { "query_string": { Type: schema.TypeList, - Optional: true, + Optional: false, MinItems: 1, MaxItems: 1, Elem: &schema.Resource{ @@ -398,7 +398,7 @@ func resourceCloudflarePageRuleSchema() map[string]*schema.Schema { "user": { Type: schema.TypeList, - Optional: true, + Optional: false, MinItems: 1, MaxItems: 1, Elem: &schema.Resource{