@@ -178,9 +178,13 @@ func (r *playlistResource) Create(ctx context.Context, req resource.CreateReques
178178 }
179179
180180 id := createResp .Payload .UID
181- if id == "" {
181+ if id == "" && createResp . Payload . ID != 0 {
182182 id = strconv .FormatInt (createResp .Payload .ID , 10 )
183183 }
184+ if id == "" {
185+ resp .Diagnostics .AddError ("Error creating playlist" , "API response did not include a playlist UID or numeric ID" )
186+ return
187+ }
184188 plan .ID = types .StringValue (MakeOrgResourceID (orgID , id ))
185189
186190 readData , diags := r .read (ctx , plan .ID .ValueString ())
@@ -212,25 +216,37 @@ func (r *playlistResource) Read(ctx context.Context, req resource.ReadRequest, r
212216 resp .Diagnostics .Append (resp .State .Set (ctx , readData )... )
213217}
214218
219+ // clientOrgAndPlaylistUID parses the Terraform resource id and returns a Grafana client
220+ // scoped to the resource org and the playlist UID.
221+ func (r * playlistResource ) clientOrgAndPlaylistUID (id string ) (* goapi.GrafanaHTTPAPI , int64 , string , diag.Diagnostics ) {
222+ var diags diag.Diagnostics
223+ client , orgID , split , parseErr := r .clientFromExistingOrgResource (resourcePlaylistID , id )
224+ if parseErr != nil {
225+ diags .AddError ("Failed to parse resource ID" , parseErr .Error ())
226+ return nil , 0 , "" , diags
227+ }
228+ if len (split ) == 0 {
229+ diags .AddError ("Invalid resource ID" , "Resource ID has no parts" )
230+ return nil , 0 , "" , diags
231+ }
232+ uid , ok := split [0 ].(string )
233+ if ! ok || uid == "" {
234+ diags .AddError ("Invalid resource ID" , "Playlist UID is missing or invalid" )
235+ return nil , 0 , "" , diags
236+ }
237+ return client , orgID , uid , diags
238+ }
239+
215240func (r * playlistResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
216241 var plan playlistResourceModel
217242 resp .Diagnostics .Append (req .Plan .Get (ctx , & plan )... )
218243 if resp .Diagnostics .HasError () {
219244 return
220245 }
221246
222- client , _ , split , parseErr := r .clientFromExistingOrgResource (resourcePlaylistID , plan .ID .ValueString ())
223- if parseErr != nil {
224- resp .Diagnostics .AddError ("Failed to parse resource ID" , parseErr .Error ())
225- return
226- }
227- if len (split ) == 0 {
228- resp .Diagnostics .AddError ("Invalid resource ID" , "Resource ID has no parts" )
229- return
230- }
231- uid , ok := split [0 ].(string )
232- if ! ok || uid == "" {
233- resp .Diagnostics .AddError ("Invalid resource ID" , "Playlist UID is missing or invalid" )
247+ client , _ , uid , diags := r .clientOrgAndPlaylistUID (plan .ID .ValueString ())
248+ resp .Diagnostics .Append (diags ... )
249+ if resp .Diagnostics .HasError () {
234250 return
235251 }
236252
@@ -268,18 +284,9 @@ func (r *playlistResource) Delete(ctx context.Context, req resource.DeleteReques
268284 return
269285 }
270286
271- client , _ , split , parseErr := r .clientFromExistingOrgResource (resourcePlaylistID , state .ID .ValueString ())
272- if parseErr != nil {
273- resp .Diagnostics .AddError ("Failed to parse resource ID" , parseErr .Error ())
274- return
275- }
276- if len (split ) == 0 {
277- resp .Diagnostics .AddError ("Invalid resource ID" , "Resource ID has no parts" )
278- return
279- }
280- uid , ok := split [0 ].(string )
281- if ! ok || uid == "" {
282- resp .Diagnostics .AddError ("Invalid resource ID" , "Playlist UID is missing or invalid" )
287+ client , _ , uid , diags := r .clientOrgAndPlaylistUID (state .ID .ValueString ())
288+ resp .Diagnostics .Append (diags ... )
289+ if resp .Diagnostics .HasError () {
283290 return
284291 }
285292
@@ -305,17 +312,10 @@ func (r *playlistResource) ImportState(ctx context.Context, req resource.ImportS
305312}
306313
307314func (r * playlistResource ) read (ctx context.Context , id string ) (* playlistResourceModel , diag.Diagnostics ) {
308- var diags diag.Diagnostics
309- client , orgID , split , err := r .clientFromExistingOrgResource (resourcePlaylistID , id )
310- if err != nil {
311- diags .AddError ("Failed to parse resource ID" , err .Error ())
312- return nil , diags
313- }
314- if len (split ) == 0 {
315- diags .AddError ("Invalid resource ID" , "Resource ID has no parts" )
315+ client , orgID , uid , diags := r .clientOrgAndPlaylistUID (id )
316+ if diags .HasError () {
316317 return nil , diags
317318 }
318- uid , _ := split [0 ].(string )
319319
320320 resp , err := client .Playlists .GetPlaylist (uid )
321321 // In Grafana 9.0+, if the playlist doesn't exist, the API returns an empty playlist but not a notfound error
0 commit comments