Skip to content

Commit 87b20bc

Browse files
committed
Normalize the JSON during planning so the plan matches the state after apply
1 parent 347dce5 commit 87b20bc

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

internal/resources/grafana/resource_dashboard.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ func (m folderDashboardPlanModifier) PlanModifyString(ctx context.Context, req p
8888
}
8989
}
9090

91+
// configJSONPlanModifier normalizes config_json during planning so the plan value
92+
// matches the stored form (compact JSON, id/version/panel-ids removed). Without this,
93+
// the Plugin Framework's post-apply consistency check fails when the user provides
94+
// pretty-printed or un-normalized JSON.
95+
type configJSONPlanModifier struct{}
96+
97+
func (configJSONPlanModifier) Description(_ context.Context) string {
98+
return "Normalizes config_json to compact JSON with id and version removed."
99+
}
100+
101+
func (m configJSONPlanModifier) MarkdownDescription(ctx context.Context) string {
102+
return m.Description(ctx)
103+
}
104+
105+
func (configJSONPlanModifier) PlanModifyString(_ context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
106+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
107+
return
108+
}
109+
normalized := NormalizeDashboardConfigJSON(req.ConfigValue.ValueString())
110+
resp.PlanValue = types.StringValue(normalized)
111+
}
112+
91113
// jsonStringValidator validates that a string is valid JSON.
92114
type jsonStringValidator struct{}
93115

@@ -170,9 +192,11 @@ Manages Grafana dashboards.
170192
},
171193
},
172194
"config_json": frameworkSchema.StringAttribute{
173-
Required: true,
174-
Description: "The complete dashboard model JSON.",
175-
Validators: []validator.String{jsonStringValidator{}},
195+
Required: true,
196+
Computed: true,
197+
Description: "The complete dashboard model JSON.",
198+
Validators: []validator.String{jsonStringValidator{}},
199+
PlanModifiers: []planmodifier.String{configJSONPlanModifier{}},
176200
},
177201
"overwrite": frameworkSchema.BoolAttribute{
178202
Optional: true,

0 commit comments

Comments
 (0)