Skip to content

Commit c8f4656

Browse files
fix: introduce mutex for folder crud ops (#2350)
1 parent 224adea commit c8f4656

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

internal/common/client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Client struct {
5151
K6APIConfig *k6providerapi.K6APIConfig
5252

5353
alertingMutex sync.Mutex
54+
folderMutex sync.Mutex
5455
}
5556

5657
// WithAlertingMutex is a helper function that wraps a CRUD Terraform function with a mutex.
@@ -63,6 +64,16 @@ func WithAlertingMutex[T schema.CreateContextFunc | schema.ReadContextFunc | sch
6364
}
6465
}
6566

67+
// WithFolderMutex is a helper function that wraps a CRUD Terraform function with a mutex.
68+
func WithFolderMutex[T schema.CreateContextFunc | schema.ReadContextFunc | schema.UpdateContextFunc | schema.DeleteContextFunc](f T) T {
69+
return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
70+
lock := &meta.(*Client).folderMutex
71+
lock.Lock()
72+
defer lock.Unlock()
73+
return f(ctx, d, meta)
74+
}
75+
}
76+
6677
func (c *Client) GrafanaSubpath(path string) string {
6778
path = strings.TrimPrefix(path, c.GrafanaAPIURLParsed.Path)
6879
return c.GrafanaAPIURLParsed.JoinPath(path).String()

internal/resources/grafana/resource_folder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func resourceFolder() *common.Resource {
2929
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/folder/)
3030
`,
3131

32-
CreateContext: CreateFolder,
33-
DeleteContext: DeleteFolder,
32+
CreateContext: common.WithFolderMutex[schema.CreateContextFunc](CreateFolder),
33+
DeleteContext: common.WithFolderMutex[schema.DeleteContextFunc](DeleteFolder),
3434
ReadContext: ReadFolder,
35-
UpdateContext: UpdateFolder,
35+
UpdateContext: common.WithFolderMutex[schema.UpdateContextFunc](UpdateFolder),
3636
Importer: &schema.ResourceImporter{
3737
StateContext: schema.ImportStatePassthroughContext,
3838
},

0 commit comments

Comments
 (0)