Skip to content

Commit c1101f8

Browse files
authored
Merge pull request #5877 from zakcutner/ruleset/validation
Improve validation for `cloudflare_ruleset` resource
2 parents 4ac5d65 + 2516aae commit c1101f8

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

internal/services/ruleset/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func (m RulesetModel) MarshalJSONForUpdate(state RulesetModel) (data []byte, err
3333

3434
type RulesetRulesModel struct {
3535
ID types.String `tfsdk:"id" json:"id,computed"`
36-
Action types.String `tfsdk:"action" json:"action,optional"`
36+
Action types.String `tfsdk:"action" json:"action,required"`
3737
ActionParameters customfield.NestedObject[RulesetRulesActionParametersModel] `tfsdk:"action_parameters" json:"action_parameters,optional"`
3838
Description types.String `tfsdk:"description" json:"description,optional"`
3939
Enabled types.Bool `tfsdk:"enabled" json:"enabled,computed_optional"`
4040
ExposedCredentialCheck customfield.NestedObject[RulesetRulesExposedCredentialCheckModel] `tfsdk:"exposed_credential_check" json:"exposed_credential_check,optional"`
41-
Expression types.String `tfsdk:"expression" json:"expression,optional"`
41+
Expression types.String `tfsdk:"expression" json:"expression,required"`
4242
Logging customfield.NestedObject[RulesetRulesLoggingModel] `tfsdk:"logging" json:"logging,computed_optional"`
4343
Ratelimit customfield.NestedObject[RulesetRulesRatelimitModel] `tfsdk:"ratelimit" json:"ratelimit,optional"`
4444
Ref types.String `tfsdk:"ref" json:"ref,computed_optional"`

internal/services/ruleset/schema.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
1111
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1212
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
13+
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
1314
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
15+
"github.com/hashicorp/terraform-plugin-framework/path"
1416
"github.com/hashicorp/terraform-plugin-framework/resource"
1517
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
@@ -109,7 +111,7 @@ func ResourceSchema(ctx context.Context) schema.Schema {
109111
},
110112
"action": schema.StringAttribute{
111113
Description: "The action to perform when the rule matches.\nAvailable values: \"block\", \"challenge\", \"compress_response\", \"execute\", \"js_challenge\", \"log\", \"managed_challenge\", \"redirect\", \"rewrite\", \"route\", \"score\", \"serve_error\", \"set_config\", \"skip\", \"set_cache_settings\", \"log_custom_field\", \"ddos_dynamic\", \"force_connection_close\".",
112-
Optional: true,
114+
Required: true,
113115
Validators: []validator.String{
114116
stringvalidator.OneOfCaseInsensitive(
115117
"block",
@@ -1045,13 +1047,16 @@ func ResourceSchema(ctx context.Context) schema.Schema {
10451047
},
10461048
"expression": schema.StringAttribute{
10471049
Description: "The expression defining which traffic will match the rule.",
1048-
Optional: true,
1050+
Required: true,
10491051
},
10501052
"logging": schema.SingleNestedAttribute{
10511053
Description: "An object configuring the rule's logging behavior.",
10521054
Computed: true,
10531055
Optional: true,
1054-
CustomType: customfield.NewNestedObjectType[RulesetRulesLoggingModel](ctx),
1056+
Validators: []validator.Object{
1057+
objectvalidator.AlsoRequires(path.MatchRelative().AtName("enabled")),
1058+
},
1059+
CustomType: customfield.NewNestedObjectType[RulesetRulesLoggingModel](ctx),
10551060
Attributes: map[string]schema.Attribute{
10561061
"enabled": schema.BoolAttribute{
10571062
Description: "Whether to generate a log when the rule matches.",

0 commit comments

Comments
 (0)