Skip to content

Commit 4ac5d65

Browse files
Merge pull request #5873 from zakcutner/ruleset/logging
Reduce drift for `cloudflare_resource` rule logging attribute
2 parents ef97a83 + ee11229 commit 4ac5d65

File tree

13 files changed

+569
-29
lines changed

13 files changed

+569
-29
lines changed

internal/services/ruleset/model.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ type RulesetRulesModel struct {
3535
ID types.String `tfsdk:"id" json:"id,computed"`
3636
Action types.String `tfsdk:"action" json:"action,optional"`
3737
ActionParameters customfield.NestedObject[RulesetRulesActionParametersModel] `tfsdk:"action_parameters" json:"action_parameters,optional"`
38-
Categories customfield.List[types.String] `tfsdk:"categories" json:"categories,optional"`
3938
Description types.String `tfsdk:"description" json:"description,optional"`
4039
Enabled types.Bool `tfsdk:"enabled" json:"enabled,computed_optional"`
4140
ExposedCredentialCheck customfield.NestedObject[RulesetRulesExposedCredentialCheckModel] `tfsdk:"exposed_credential_check" json:"exposed_credential_check,optional"`
4241
Expression types.String `tfsdk:"expression" json:"expression,optional"`
43-
Logging customfield.NestedObject[RulesetRulesLoggingModel] `tfsdk:"logging" json:"logging,optional"`
42+
Logging customfield.NestedObject[RulesetRulesLoggingModel] `tfsdk:"logging" json:"logging,computed_optional"`
4443
Ratelimit customfield.NestedObject[RulesetRulesRatelimitModel] `tfsdk:"ratelimit" json:"ratelimit,optional"`
4544
Ref types.String `tfsdk:"ref" json:"ref,computed_optional"`
4645
}
@@ -303,7 +302,7 @@ type RulesetRulesExposedCredentialCheckModel struct {
303302
}
304303

305304
type RulesetRulesLoggingModel struct {
306-
Enabled types.Bool `tfsdk:"enabled" json:"enabled,required"`
305+
Enabled types.Bool `tfsdk:"enabled" json:"enabled,computed_optional"`
307306
}
308307

309308
type RulesetRulesRatelimitModel struct {

internal/services/ruleset/resource.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,17 @@ func (r *RulesetResource) ModifyPlan(ctx context.Context, req resource.ModifyPla
304304
return
305305
}
306306

307-
ruleIDsByRef := make(map[string]types.String)
307+
rulesByRef := make(map[string]*RulesetRulesModel)
308308

309309
stateRules, diags := state.Rules.AsStructSliceT(ctx)
310310
resp.Diagnostics.Append(diags...)
311311
if resp.Diagnostics.HasError() {
312312
return
313313
}
314314

315-
for _, rule := range stateRules {
316-
if ref := rule.Ref.ValueString(); ref != "" {
317-
ruleIDsByRef[ref] = rule.ID
315+
for i := range stateRules {
316+
if ref := stateRules[i].Ref.ValueString(); ref != "" {
317+
rulesByRef[ref] = &stateRules[i]
318318
}
319319
}
320320

@@ -324,17 +324,21 @@ func (r *RulesetResource) ModifyPlan(ctx context.Context, req resource.ModifyPla
324324
return
325325
}
326326

327-
for i, rule := range planRules {
328-
// Do nothing if the rule's ID is a known planned value.
329-
if !rule.ID.IsUnknown() {
330-
continue
331-
}
332-
333-
// If the rule's ref matches a rule in the state, populate the planned
334-
// value of its ID with the corresponding ID from the state.
335-
if ref := rule.Ref.ValueString(); ref != "" {
336-
if id, ok := ruleIDsByRef[ref]; ok {
337-
planRules[i].ID = id
327+
for i := range planRules {
328+
if ref := planRules[i].Ref.ValueString(); ref != "" {
329+
if stateRule, ok := rulesByRef[ref]; ok {
330+
// If the rule's ref matches a rule from the state, populate its
331+
// planned ID using the matching rule.
332+
if planRules[i].ID.IsUnknown() {
333+
planRules[i].ID = stateRule.ID
334+
}
335+
336+
// If the rule's action is unchanged, populate its planned
337+
// logging attribute using the matching rule from the state.
338+
if planRules[i].Logging.IsUnknown() &&
339+
stateRule.Action.Equal(planRules[i].Action) {
340+
planRules[i].Logging = stateRule.Logging
341+
}
338342
}
339343
}
340344
}

0 commit comments

Comments
 (0)