From b9e508f3ca0cd7ceb90e1bcb5d3c55b15b78eb01 Mon Sep 17 00:00:00 2001 From: Zhu Sheng Li Date: Thu, 24 Oct 2024 23:40:15 +0900 Subject: [PATCH 1/3] fix: unexpected validation error when ttl is set with variable fixes: #4412 --- internal/framework/service/rulesets/validators.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/framework/service/rulesets/validators.go b/internal/framework/service/rulesets/validators.go index f18ced0fd7..acdec50e77 100644 --- a/internal/framework/service/rulesets/validators.go +++ b/internal/framework/service/rulesets/validators.go @@ -62,6 +62,9 @@ func (v EdgeTTLValidator) ValidateObject(ctx context.Context, req validator.Obje if resp.Diagnostics.HasError() { return } + if parameter.Default.IsUnknown() { + return + } if parameter.Mode.ValueString() == "override_origin" { if parameter.Default.ValueInt64() <= 0 { @@ -105,6 +108,9 @@ func (v BrowserTTLValidator) ValidateObject(ctx context.Context, req validator.O if resp.Diagnostics.HasError() { return } + if parameter.Default.IsUnknown() { + return + } if parameter.Mode.ValueString() == "override_origin" { if parameter.Default.ValueInt64() <= 0 { From 66b2525ada11af9690b6811dd34e6da8eb8da7c3 Mon Sep 17 00:00:00 2001 From: Zhu Sheng Li Date: Fri, 25 Oct 2024 03:01:26 +0900 Subject: [PATCH 2/3] add changelog --- .changelog/4414.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/4414.txt diff --git a/.changelog/4414.txt b/.changelog/4414.txt new file mode 100644 index 0000000000..ddee351811 --- /dev/null +++ b/.changelog/4414.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/cloudflare_ruleset: unexpected validation error when ttl default is set with variable +``` From c4a40be72c33f3547f1834dd3bf35ba2133be580 Mon Sep 17 00:00:00 2001 From: Zhu Sheng Li Date: Sat, 26 Oct 2024 12:29:52 +0900 Subject: [PATCH 3/3] adding unit tests --- .../service/rulesets/validators_test.go | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/internal/framework/service/rulesets/validators_test.go b/internal/framework/service/rulesets/validators_test.go index 38b2e68204..e7d1f33338 100644 --- a/internal/framework/service/rulesets/validators_test.go +++ b/internal/framework/service/rulesets/validators_test.go @@ -61,6 +61,30 @@ func TestEdgeTTLValidation(t *testing.T) { } }) + t.Run("passes when ttl is unknown", func(t *testing.T) { + t.Parallel() + + mode := "override_origin" + _, statusCodeAttr, listType := constructStatusTTLObject() + resp := &validator.ObjectResponse{} + req := validator.ObjectRequest{ + Path: path.Root("edge_ttl"), + ConfigValue: types.ObjectValueMust( + map[string]attr.Type{"mode": types.StringType, "status_code_ttl": listType, "default": types.Int64Type}, + map[string]attr.Value{"mode": types.StringValue(mode), "status_code_ttl": statusCodeAttr, "default": types.Int64Unknown()}, + ), + } + edgeValidator.ValidateObject(ctx, req, resp) + + expected := &validator.ObjectResponse{ + Diagnostics: nil, + } + + if diff := cmp.Diff(resp, expected); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + }) + t.Run("passes valid ttl values", func(t *testing.T) { t.Parallel() @@ -162,6 +186,29 @@ func TestBrowserTTLValidation(t *testing.T) { } }) + t.Run("passes when ttl is unknown", func(t *testing.T) { + t.Parallel() + + mode := "override_origin" + resp := &validator.ObjectResponse{} + req := validator.ObjectRequest{ + Path: path.Root("brower_ttl"), + ConfigValue: types.ObjectValueMust( + map[string]attr.Type{"mode": types.StringType, "default": types.Int64Type}, + map[string]attr.Value{"mode": types.StringValue(mode), "default": types.Int64Unknown()}, + ), + } + browserValidator.ValidateObject(ctx, req, resp) + + expected := &validator.ObjectResponse{ + Diagnostics: nil, + } + + if diff := cmp.Diff(resp, expected); diff != "" { + t.Errorf("unexpected difference: %s", diff) + } + }) + t.Run("errors when invalid ttl is passed", func(t *testing.T) { t.Parallel()