Skip to content

Commit

Permalink
resource/cloudflare_ruleset: ensure custom keys via query strings are…
Browse files Browse the repository at this point in the history
… known

Updates the state handling to ensure that all query string parameters
are known to the state handler when defining explicit query parameters
as part of the custom cache key.

Closes #2360
  • Loading branch information
jacobbednarz committed Apr 19, 2023
1 parent 13dd770 commit 076450f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/framework/service/rulesets/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,20 @@ func toRulesetResourceModel(zoneID, accountID basetypes.StringValue, in cloudfla
include, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include)
exclude, _ := basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude)

if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include != nil && ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include.All {
include, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, []string{"*"})
if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include != nil {
if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include.All {
include, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, []string{"*"})
} else {
include, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Include.List)
}
}

if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude != nil && ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude.All {
exclude, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, []string{"*"})
if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude != nil {
if ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude.All {
exclude, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, []string{"*"})
} else {
exclude, _ = basetypes.NewSetValueFrom(context.Background(), types.StringType, ruleResponse.ActionParameters.CacheKey.CustomKey.Query.Exclude.List)
}
}

key.QueryString = []*ActionParameterCacheKeyCustomKeyQueryStringModel{{
Expand Down
96 changes: 96 additions & 0 deletions internal/framework/service/rulesets/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,36 @@ func TestAccCloudflareRuleset_ConfigConflictingCacheByDevice(t *testing.T) {
})
}

func TestAccCloudflareRuleset_CacheSettingsDefinedQueryStringExcludeKeys(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareRulesetCacheSettingsExplicitCustomKeyCacheKeysQueryStringsExclude(rnd, zoneID),
},
},
})
}

func TestAccCloudflareRuleset_CacheSettingsDefinedQueryStringIncludeKeys(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCloudflareRulesetCacheSettingsExplicitCustomKeyCacheKeysQueryStringsInclude(rnd, zoneID),
},
},
})
}

func testAccCheckCloudflareRulesetMagicTransitSingle(rnd, name, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
Expand Down Expand Up @@ -3725,6 +3755,72 @@ func testAccCloudflareRulesetConfigConflictingCacheByDeviceConfigs(rnd, zoneID s
}`, rnd, zoneID)
}

func testAccCloudflareRulesetCacheSettingsExplicitCustomKeyCacheKeysQueryStringsExclude(rnd, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "cache_settings_example" {
zone_id = "%[2]s"
name = "%[1]s"
description = "set cache settings for the request"
kind = "zone"
phase = "http_request_cache_settings"
rules {
action = "set_cache_settings"
description = "example"
enabled = true
expression = "(http.host eq \"example.com\" and starts_with(http.request.uri.path, \"/example\"))"
action_parameters {
cache = true
edge_ttl {
mode = "override_origin"
default = 7200
}
cache_key {
ignore_query_strings_order = true
custom_key {
query_string {
exclude = ["example"]
}
}
}
}
}
}
`, rnd, zoneID)
}

func testAccCloudflareRulesetCacheSettingsExplicitCustomKeyCacheKeysQueryStringsInclude(rnd, zoneID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "cache_settings_example" {
zone_id = "%[2]s"
name = "%[1]s"
description = "set cache settings for the request"
kind = "zone"
phase = "http_request_cache_settings"
rules {
action = "set_cache_settings"
description = "example"
enabled = true
expression = "(http.host eq \"example.com\" and starts_with(http.request.uri.path, \"/example\"))"
action_parameters {
cache = true
edge_ttl {
mode = "override_origin"
default = 7200
}
cache_key {
ignore_query_strings_order = true
custom_key {
query_string {
include = ["another_example"]
}
}
}
}
}
}
`, rnd, zoneID)
}

func testAccCheckCloudflareRulesetDestroy(s *terraform.State) error {
return nil
}

0 comments on commit 076450f

Please sign in to comment.