Skip to content

Commit

Permalink
more schema changes
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Lyons <[email protected]>
  • Loading branch information
mephmanx committed Aug 30, 2024
1 parent bdb61c4 commit 552c41c
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .changelog/3820.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/rulesets: add "contains" support to custom cache key headers
```
3 changes: 3 additions & 0 deletions .changelog/3829.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_zone_settings_override: fix migration process with nil initial_settings
```
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@ dist/*
.envrc
.flox/
.vscode/private.env

setvars.sh
1 change: 1 addition & 0 deletions docs/data-sources/rulesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Read-Only:
Read-Only:

- `check_presence` (List of String)
- `contains` (Map of Set of String)
- `exclude_origin` (Boolean)
- `include` (List of String)

Expand Down
6 changes: 6 additions & 0 deletions docs/resources/ruleset.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ resource "cloudflare_ruleset" "cache_settings_example" {
include = ["habc", "hdef"]
check_presence = ["habc_t", "hdef_t"]
exclude_origin = true
contains = {
"accept" = ["image/web", "image/png"]
"accept-encoding" = ["br", "zstd"]
"some-header" = ["some-value", "some-other-value"]
}
}
cookie {
include = ["cabc", "cdef"]
Expand Down Expand Up @@ -602,6 +607,7 @@ Optional:
Optional:

- `check_presence` (Set of String) List of headers to check for presence in the custom key.
- `contains` (Map of Set of String) Dictionary of headers mapping to lists of values to check for presence in the custom key.
- `exclude_origin` (Boolean) Exclude the origin header from the custom key.
- `include` (Set of String) List of headers to include in the custom key.

Expand Down
5 changes: 5 additions & 0 deletions examples/resources/cloudflare_ruleset/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ resource "cloudflare_ruleset" "cache_settings_example" {
include = ["habc", "hdef"]
check_presence = ["habc_t", "hdef_t"]
exclude_origin = true
contains = {
"accept" = ["image/web", "image/png"]
"accept-encoding" = ["br", "zstd"]
"some-header" = ["some-value", "some-other-value"]
}
}
cookie {
include = ["cabc", "cdef"]
Expand Down
7 changes: 4 additions & 3 deletions internal/framework/service/rulesets/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ type ActionParameterCacheKeyCustomKeyQueryStringModel struct {
}

type ActionParameterCacheKeyCustomKeyHeaderModel struct {
Include types.Set `tfsdk:"include"`
CheckPresence types.Set `tfsdk:"check_presence"`
ExcludeOrigin types.Bool `tfsdk:"exclude_origin"`
Include types.Set `tfsdk:"include"`
CheckPresence types.Set `tfsdk:"check_presence"`
ExcludeOrigin types.Bool `tfsdk:"exclude_origin"`
Contains map[string]types.Set `tfsdk:"contains"`
}

type ActionParameterCacheKeyCustomKeyCookieModel struct {
Expand Down
16 changes: 15 additions & 1 deletion internal/framework/service/rulesets/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,20 @@ func toRulesetResourceModel(ctx context.Context, zoneID, accountID basetypes.Str
} else {
excludeOrigin = types.BoolNull()
}
if len(include.Elements()) > 0 || len(checkPresence.Elements()) > 0 || excludeOrigin.ValueBool() {
contains := map[string]types.Set{}
for k, v := range ruleResponse.ActionParameters.CacheKey.CustomKey.Header.Contains {
set, _ := basetypes.NewSetValueFrom(ctx, types.StringType, v)
contains[k] = set
}
if len(include.Elements()) > 0 || len(checkPresence.Elements()) > 0 || excludeOrigin.ValueBool() || len(contains) > 0 {
if len(contains) == 0 {
contains = nil
}
key.Header = []*ActionParameterCacheKeyCustomKeyHeaderModel{{
Include: include,
CheckPresence: checkPresence,
ExcludeOrigin: excludeOrigin,
Contains: contains,
}}
}
}
Expand Down Expand Up @@ -1174,13 +1183,18 @@ func (r *RulesModel) toRulesetRule(ctx context.Context) cfv1.RulesetRule {
if len(ap.CacheKey[0].CustomKey[0].Header) > 0 {
includeQueryList := expanders.StringSet(ctx, ap.CacheKey[0].CustomKey[0].Header[0].Include)
checkPresenceList := expanders.StringSet(ctx, basetypes.SetValue(ap.CacheKey[0].CustomKey[0].Header[0].CheckPresence))
containsMap := map[string][]string{}
for k, v := range ap.CacheKey[0].CustomKey[0].Header[0].Contains {
containsMap[k] = expanders.StringSet(ctx, v)
}

customKey.Header = &cfv1.RulesetRuleActionParametersCustomKeyHeader{
RulesetRuleActionParametersCustomKeyFields: cfv1.RulesetRuleActionParametersCustomKeyFields{
Include: includeQueryList,
CheckPresence: checkPresenceList,
},
ExcludeOrigin: cfv1.BoolPtr(ap.CacheKey[0].CustomKey[0].Header[0].ExcludeOrigin.ValueBool()),
Contains: containsMap,
}
}

Expand Down
33 changes: 33 additions & 0 deletions internal/framework/service/rulesets/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,9 @@ func TestAccCloudflareRuleset_CacheSettingsAllEnabled(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.check_presence.0", "habc_t"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.check_presence.1", "hdef_t"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.exclude_origin", "true"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.%", "3"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.*", "image/web"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.cookie.0.include.#", "2"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.cookie.0.include.0", "cabc"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.cookie.0.include.1", "cdef"),
Expand Down Expand Up @@ -1950,6 +1953,7 @@ func TestAccCloudflareRuleset_CacheSettingsOnlyExludeOrigin(t *testing.T) {

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.contains.%", "0"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.exclude_origin", "true"),
),
},
Expand Down Expand Up @@ -2580,6 +2584,9 @@ func TestAccCloudflareRuleset_CacheSettingsHandleDefaultHeaderExcludeOrigin(t *t
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.include.0", "x-test"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.include.1", "x-test2"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.exclude_origin", "false"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.%", "3"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.*", "image/web"),
),
},
{
Expand All @@ -2599,6 +2606,9 @@ func TestAccCloudflareRuleset_CacheSettingsHandleDefaultHeaderExcludeOrigin(t *t
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.include.0", "x-test"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.include.1", "x-test2"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.exclude_origin", "false"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.%", "3"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.*", "image/web"),
),
},
{
Expand All @@ -2618,6 +2628,9 @@ func TestAccCloudflareRuleset_CacheSettingsHandleDefaultHeaderExcludeOrigin(t *t
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.include.0", "x-test"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.include.1", "x-test2"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.exclude_origin", "true"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.%", "3"),
resource.TestCheckResourceAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "rules.0.action_parameters.0.cache_key.0.custom_key.0.header.0.contains.accept.*", "image/web"),
),
},
},
Expand Down Expand Up @@ -4110,6 +4123,11 @@ func testAccCloudflareRulesetCacheSettingsAllEnabled(rnd, accountID, zoneID stri
include = ["habc", "hdef"]
check_presence = ["habc_t", "hdef_t"]
exclude_origin = true
contains = {
"accept" = ["image/web", "image/png"]
"accept-encoding" = ["br", "zstd"]
"some-header" = ["some-value", "some-other-value"]
}
}
cookie {
include = ["cabc", "cdef"]
Expand Down Expand Up @@ -4786,6 +4804,11 @@ func testAccCloudflareRulesetCacheSettingsHandleDefaultHeaderExcludeOrigin(rnd,
header {
check_presence = ["x-forwarded-for"]
include = ["x-test", "x-test2"]
contains = {
"accept" = ["image/web", "image/png"]
"accept-encoding" = ["br", "zstd"]
"some-header" = ["some-value", "some-other-value"]
}
}
}
}
Expand Down Expand Up @@ -4820,6 +4843,11 @@ func testAccCloudflareRulesetCacheSettingsHandleHeaderExcludeOriginSet(rnd, zone
check_presence = ["x-forwarded-for"]
include = ["x-test", "x-test2"]
exclude_origin = true
contains = {
"accept" = ["image/web", "image/png"]
"accept-encoding" = ["br", "zstd"]
"some-header" = ["some-value", "some-other-value"]
}
}
}
}
Expand Down Expand Up @@ -4854,6 +4882,11 @@ func testAccCloudflareRulesetCacheSettingsHandleHeaderExcludeOriginFalse(rnd, zo
check_presence = ["x-forwarded-for"]
include = ["x-test", "x-test2"]
exclude_origin = false
contains = {
"accept" = ["image/web", "image/png"]
"accept-encoding" = ["br", "zstd"]
"some-header" = ["some-value", "some-other-value"]
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions internal/framework/service/rulesets/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,13 @@ func (r *RulesetResource) Schema(ctx context.Context, req resource.SchemaRequest
defaults.DefaultBool(false),
},
},
"contains": schema.MapAttribute{
ElementType: types.SetType{
ElemType: types.StringType,
},
Optional: true,
MarkdownDescription: "Dictionary of headers mapping to lists of values to check for presence in the custom key.",
},
},
},
Validators: []validator.List{
Expand Down
11 changes: 11 additions & 0 deletions internal/sdkv2provider/data_source_rulesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,17 @@ func resourceCloudflareRulesetSchema() map[string]*schema.Schema {
Optional: true,
Description: "Exclude the origin header from the custom key.",
},
"contains": {
Type: schema.TypeMap,
Optional: true,
Description: "Dictionary of headers mapping to lists of values to check for presence in the custom key.",
Elem: &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/sdkv2provider/resource_cloudflare_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func resourceCloudflareQueueCreate(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(fmt.Errorf("failed to find id in Create response; resource was empty"))
}

if d.Get("consumer") != nil {
consumers := d.Get("consumer").([]interface{})
if d.Get("consumers") != nil {
consumers := d.Get("consumers").([]interface{})
for _, consumer := range consumers {
consumerMap := consumer.(map[string]interface{})
settings := consumerMap["settings"].([]interface{})[0].(map[string]interface{})
Expand Down Expand Up @@ -115,7 +115,7 @@ func resourceCloudflareQueueRead(ctx context.Context, d *schema.ResourceData, me
}
consumers = append(consumers, consumerMap)
}
d.Set("consumer", consumers)
d.Set("consumers", consumers)
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions internal/sdkv2provider/resource_cloudflare_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,26 @@ func testAccCheckCloudflareQueueWConsumer(rnd, accountID, name string) string {
resource "cloudflare_queue" "%[1]s" {
account_id = "%[2]s"
name = "%[3]s"
consumer {
script_name = "%[1]s"
settings {
batch_size = 1
max_retries = 1
max_wait_time_ms = 1000
consumers = {
{
queue_name = "%[3]s"
script_name = "%[1]s"
}
}
depends_on = [cloudflare_workers_script.%[1]s]
}`, rnd, accountID, name, queueModuleContent)
return tf_content
}

// {
// script_name = "%[1]s"
// settings = {
// batch_size = 1
// max_retries = 1
// max_wait_time_ms = 1000
// }
// }

func testAccCheckCloudflareQueueExists(name string, queue *cloudflare.Queue) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*cloudflare.API)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func resourceCloudflareZoneSettingsOverrideStateUpgradeV1(
}

upgrade := func(state map[string]interface{}, name string) map[string]interface{} {
delete(state[name].([]interface{})[0].(map[string]interface{}), "mobile_redirect")
if val, ok := state[name]; ok && val != nil {
delete(state[name].([]interface{})[0].(map[string]interface{}), "mobile_redirect")
}
return state
}

Expand Down Expand Up @@ -89,7 +91,9 @@ func resourceCloudflareZoneSettingsOverrideStateUpgradeV2(
}

upgrade := func(state map[string]interface{}, name string) map[string]interface{} {
delete(state[name].([]interface{})[0].(map[string]interface{}), "minify")
if val, ok := state[name]; ok && val != nil {
delete(state[name].([]interface{})[0].(map[string]interface{}), "minify")
}
return state
}

Expand Down
Loading

0 comments on commit 552c41c

Please sign in to comment.