@@ -45,7 +45,6 @@ type DashboardResourceModel struct {
4545 ID types.String `tfsdk:"id"`
4646 UID types.String `tfsdk:"uid"`
4747 Dashboard types.String `tfsdk:"dashboard"`
48- Version types.Int64 `tfsdk:"version"`
4948 Overwrite types.Bool `tfsdk:"overwrite"`
5049}
5150
@@ -98,10 +97,6 @@ func (r *DashboardResource) Schema(_ context.Context, _ resource.SchemaRequest,
9897 Required : true ,
9998 MarkdownDescription : "The dashboard document as a JSON string (for example from `jsonencode(...)`)." ,
10099 },
101- "version" : schema.Int64Attribute {
102- Computed : true ,
103- MarkdownDescription : "Monotonic dashboard version used for optimistic updates." ,
104- },
105100 "overwrite" : schema.BoolAttribute {
106101 Optional : true ,
107102 Computed : true ,
@@ -232,8 +227,33 @@ func (r *DashboardResource) Update(ctx context.Context, req resource.UpdateReque
232227 return
233228 }
234229
235- stateVersion := state .Version .ValueInt64 ()
236- payload , uid , diags := dashboardUpsertPayloadFromModel (plan , dashboardUIDFromState (state ), stateVersion , false )
230+ uidFromState := dashboardUIDFromState (state )
231+ stateVersion := int64 (0 )
232+ if ! plan .Overwrite .IsNull () && ! plan .Overwrite .IsUnknown () && ! plan .Overwrite .ValueBool () {
233+ remote , err := r .client .Dashboards .GetRaw (ctx , uidFromState )
234+ if err != nil {
235+ if isNotFoundError (err ) {
236+ resp .Diagnostics .AddWarning (
237+ "Dashboard Not Found" ,
238+ fmt .Sprintf ("Dashboard with UID %s does not exist and will be recreated if still defined in the configuration." , uidFromState ),
239+ )
240+ resp .State .RemoveResource (ctx )
241+ return
242+ }
243+
244+ resp .Diagnostics .AddError ("Failed to update dashboard" , fmt .Sprintf ("Unable to read current dashboard version: %s" , err ))
245+ return
246+ }
247+
248+ remoteDashboard , err := decodeDashboardResource (remote )
249+ if err != nil {
250+ resp .Diagnostics .AddError ("Failed to update dashboard" , fmt .Sprintf ("Unable to decode current dashboard response: %s" , err ))
251+ return
252+ }
253+
254+ stateVersion = remoteDashboard .Version
255+ }
256+ payload , uid , diags := dashboardUpsertPayloadFromModel (plan , uidFromState , stateVersion , false )
237257 resp .Diagnostics .Append (diags ... )
238258 if resp .Diagnostics .HasError () {
239259 return
@@ -372,7 +392,6 @@ func flattenDashboardResource(in dashboardResourcePayload, overwrite types.Bool,
372392 ID : types .StringValue (uid ),
373393 UID : types .StringValue (uid ),
374394 Dashboard : types .StringValue (normalizedDashboard ),
375- Version : types .Int64Value (in .Version ),
376395 Overwrite : overwrite ,
377396 }, nil
378397}
0 commit comments