@@ -36,22 +36,8 @@ func ResourceStack() *schema.Resource {
3636 UpdateContext : UpdateStack ,
3737 DeleteContext : DeleteStack ,
3838 ReadContext : ReadStack ,
39-
40- // Import either by ID or slug
4139 Importer : & schema.ResourceImporter {
42- StateContext : func (c context.Context , rd * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
43- _ , err := strconv .ParseInt (rd .Id (), 10 , 64 )
44- if err != nil {
45- // If the ID is not a number, then it may be a slug
46- client := meta .(* common.Client ).GrafanaCloudAPI
47- stack , err := client .StackBySlug (rd .Id ())
48- if err != nil {
49- return nil , fmt .Errorf ("failed to find stack by ID or slug '%s': %w" , rd .Id (), err )
50- }
51- rd .SetId (strconv .FormatInt (stack .ID , 10 ))
52- }
53- return []* schema.ResourceData {rd }, nil
54- },
40+ StateContext : schema .ImportStatePassthroughContext ,
5541 },
5642
5743 Schema : map [string ]* schema.Schema {
@@ -308,8 +294,7 @@ func UpdateStack(ctx context.Context, d *schema.ResourceData, meta interface{})
308294
309295func DeleteStack (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
310296 client := meta .(* common.Client ).GrafanaCloudAPI
311- slug := d .Get ("slug" ).(string )
312- if err := client .DeleteStack (slug ); err != nil {
297+ if err := client .DeleteStack (d .Id ()); err != nil {
313298 return diag .FromErr (err )
314299 }
315300
@@ -318,14 +303,8 @@ func DeleteStack(ctx context.Context, d *schema.ResourceData, meta interface{})
318303
319304func ReadStack (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
320305 client := meta .(* common.Client ).GrafanaCloudAPI
306+ stack , err := getStackFromIDOrSlug (client , d .Id ())
321307
322- idStr := d .Id ()
323- id , err := strconv .ParseInt (idStr , 10 , 64 )
324- if err != nil {
325- return diag .Errorf ("Invalid id: %#v" , idStr )
326- }
327-
328- stack , err := client .StackByID (id )
329308 if err != nil {
330309 if strings .HasPrefix (err .Error (), "status: 404" ) {
331310 log .Printf ("[WARN] removing stack %s from state because it no longer exists in grafana" , d .Get ("name" ).(string ))
@@ -341,7 +320,7 @@ func ReadStack(ctx context.Context, d *schema.ResourceData, meta interface{}) di
341320 return nil
342321 }
343322
344- if err := FlattenStack (d , stack ); err != nil {
323+ if err := FlattenStack (d , * stack ); err != nil {
345324 return diag .FromErr (err )
346325 }
347326 // Always set the wait attribute to true after creation
@@ -353,7 +332,6 @@ func ReadStack(ctx context.Context, d *schema.ResourceData, meta interface{}) di
353332
354333func FlattenStack (d * schema.ResourceData , stack gapi.Stack ) error {
355334 id := strconv .FormatInt (stack .ID , 10 )
356-
357335 d .SetId (id )
358336 d .Set ("name" , stack .Name )
359337 d .Set ("slug" , stack .Slug )
@@ -404,6 +382,25 @@ func FlattenStack(d *schema.ResourceData, stack gapi.Stack) error {
404382 return nil
405383}
406384
385+ func getStackFromIDOrSlug (client * gapi.Client , id string ) (* gapi.Stack , error ) {
386+ numericalID , err := strconv .ParseInt (id , 10 , 64 )
387+ if err != nil {
388+ // If the ID is not a number, then it may be a slug
389+ stack , err := client .StackBySlug (id )
390+ if err != nil {
391+ return nil , fmt .Errorf ("failed to find stack by ID or slug '%s': %w" , id , err )
392+ }
393+ return & stack , nil
394+ }
395+
396+ stack , err := client .StackByID (numericalID )
397+ if err != nil {
398+ return nil , err
399+ }
400+
401+ return & stack , nil
402+ }
403+
407404// Append path to baseurl
408405func appendPath (baseURL , path string ) (string , error ) {
409406 bu , err := url .Parse (baseURL )
0 commit comments