Skip to content

Commit

Permalink
Merge pull request #2802 from laliconfigcat/fixexcludeorigin
Browse files Browse the repository at this point in the history
Fix setting only exclude origin in cache_key headers
  • Loading branch information
jacobbednarz authored Oct 1, 2023
2 parents 0da851f + be3c101 commit b051724
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/2802.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_ruleset: ability to use exclude_origin=true in cache_key.custom_key.header without the need of specifying include or check_presence.
```
15 changes: 7 additions & 8 deletions internal/framework/service/rulesets/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,13 @@ func toRulesetResourceModel(ctx context.Context, zoneID, accountID basetypes.Str
if ruleResponse.ActionParameters.CacheKey.CustomKey.Header != nil {
include, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Header.Include)
checkPresence, _ := basetypes.NewSetValueFrom(ctx, types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Header.CheckPresence)
if len(include.Elements()) > 0 || len(checkPresence.Elements()) > 0 {
var excludeOrigin types.Bool
if !reflect.ValueOf(ruleResponse.ActionParameters.CacheKey.CustomKey.Header.ExcludeOrigin).IsNil() {
excludeOrigin = flatteners.Bool(ruleResponse.ActionParameters.CacheKey.CustomKey.Header.ExcludeOrigin)
} else {
excludeOrigin = types.BoolNull()
}

var excludeOrigin types.Bool
if !reflect.ValueOf(ruleResponse.ActionParameters.CacheKey.CustomKey.Header.ExcludeOrigin).IsNil() {
excludeOrigin = flatteners.Bool(ruleResponse.ActionParameters.CacheKey.CustomKey.Header.ExcludeOrigin)
} else {
excludeOrigin = types.BoolNull()
}
if len(include.Elements()) > 0 || len(checkPresence.Elements()) > 0 || excludeOrigin.ValueBool() {
key.Header = []*ActionParameterCacheKeyCustomKeyHeaderModel{{
Include: include,
CheckPresence: checkPresence,
Expand Down
64 changes: 64 additions & 0 deletions internal/framework/service/rulesets/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,36 @@ func TestAccCloudflareRuleset_CacheSettingsOptionalsEmpty(t *testing.T) {
})
}

func TestAccCloudflareRuleset_CacheSettingsOnlyExludeOrigin(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
resourceName := "cloudflare_ruleset." + rnd

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareRulesetCacheSettingsOnlyExludeOrigin(rnd, "my basic cache settings ruleset", zoneID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "my basic cache settings ruleset"),
resource.TestCheckResourceAttr(resourceName, "description", rnd+" ruleset description"),
resource.TestCheckResourceAttr(resourceName, "kind", "zone"),
resource.TestCheckResourceAttr(resourceName, "phase", "http_request_cache_settings"),

resource.TestCheckResourceAttr(resourceName, "rules.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action", "set_cache_settings"),
resource.TestCheckResourceAttr(resourceName, "rules.0.description", rnd+" set cache settings rule"),

resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.include.#", "0"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.check_presence.#", "0"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.exclude_origin", "true"),
),
},
},
})
}

func TestAccCloudflareRuleset_CacheSettingsEdgeTTLRespectOrigin(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
Expand Down Expand Up @@ -4046,6 +4076,40 @@ func testAccCloudflareRulesetCacheSettingsOptionalsEmpty(rnd, accountID, zoneID
}`, rnd, accountID, zoneID)
}

func testAccCloudflareRulesetCacheSettingsOnlyExludeOrigin(rnd, accountID, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
zone_id = "%[3]s"
name = "%[2]s"
description = "%[1]s ruleset description"
kind = "zone"
phase = "http_request_cache_settings"
rules {
action = "set_cache_settings"
action_parameters {
edge_ttl {
mode = "override_origin"
default = 60
}
browser_ttl {
mode = "respect_origin"
}
cache_key {
custom_key {
header {
exclude_origin = true
}
}
}
}
expression = "true"
description = "%[1]s set cache settings rule"
enabled = true
}
}`, rnd, accountID, zoneID)
}

func testAccCloudflareRulesetCacheSettingsMissingDefaultEdgeTTLOverrideOrigin(rnd, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
Expand Down

0 comments on commit b051724

Please sign in to comment.