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

resource/cloudflare_ruleset: ensure custom keys via query strings are known #2388

Merged
merged 1 commit into from
Apr 19, 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/2388.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_ruleset: ensure custom cache keys using query parameters are defined as known values for state handling
```
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
}