diff --git a/mgc/core/ref_resolver.go b/mgc/core/ref_resolver.go index f90b2d77f..06e0ee641 100644 --- a/mgc/core/ref_resolver.go +++ b/mgc/core/ref_resolver.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "strings" + "sync" "github.com/getkin/kin-openapi/openapi3" "github.com/go-openapi/jsonpointer" @@ -217,6 +218,7 @@ func (r *MultiRefPathResolver) Add(url string, docResolver RefPathResolver) erro type DocumentRefPathResolver struct { cache map[RefPath]any // JSON Pointer string => resolved value getRoot func() (any, error) + mutexl *sync.Mutex } var _ RefPathResolver = (*DocumentRefPathResolver)(nil) @@ -226,6 +228,7 @@ func NewDocumentRefPathResolver(getRoot utils.LoadWithError[any]) *DocumentRefPa return &DocumentRefPathResolver{ cache: map[RefPath]any{}, getRoot: getRoot, + mutexl: &sync.Mutex{}, } } @@ -251,9 +254,12 @@ func (r *DocumentRefPathResolver) ResolvePath(path RefPath) (result any, err err } var ok bool + r.mutexl.Lock() if result, ok = r.cache[path]; ok { + r.mutexl.Unlock() return } + r.mutexl.Unlock() url, _ := path.SplitUrl() if url != "" { @@ -274,11 +280,13 @@ func (r *DocumentRefPathResolver) ResolvePath(path RefPath) (result any, err err } if err == nil { + r.mutexl.Lock() r.cache[path] = result } else { err = &RefPathResolveError{path, err} } + r.mutexl.Unlock() return } diff --git a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_objectstorage_buckets.go b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_objectstorage_buckets.go index fa58c22d1..73e57b9ea 100644 --- a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_objectstorage_buckets.go +++ b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_objectstorage_buckets.go @@ -3,7 +3,6 @@ package provider import ( "context" "fmt" - "sync" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -15,10 +14,6 @@ import ( "magalu.cloud/sdk" ) -var ( - bucketResourceMutex = &sync.Mutex{} -) - type ObjectStorageBucket struct { Bucket types.String `tfsdk:"bucket"` BucketIsPrefix types.Bool `tfsdk:"bucket_is_prefix"` @@ -173,8 +168,6 @@ func (r *objectStorageBuckets) Schema(ctx context.Context, req resource.SchemaRe } func (r *objectStorageBuckets) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - bucketResourceMutex.Lock() - defer bucketResourceMutex.Unlock() var model ObjectStorageBucket diags := req.Plan.Get(ctx, &model) @@ -230,8 +223,6 @@ func convertGrants(grants []Grant) []sdkBuckets.CreateParametersGrantFullControl } func (r *objectStorageBuckets) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - bucketResourceMutex.Lock() - defer bucketResourceMutex.Unlock() var model ObjectStorageBucket diags := req.State.Get(ctx, &model) @@ -248,8 +239,6 @@ func (r *objectStorageBuckets) Update(ctx context.Context, req resource.UpdateRe } func (r *objectStorageBuckets) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - bucketResourceMutex.Lock() - defer bucketResourceMutex.Unlock() var model ObjectStorageBucket diags := req.State.Get(ctx, &model) diff --git a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_instances.go b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_instances.go index 37a58a62f..cff0e2cd4 100644 --- a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_instances.go +++ b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_instances.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "math/big" - "sync" "time" "github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator" @@ -28,9 +27,8 @@ import ( ) var ( - _ resource.Resource = &vmInstances{} - _ resource.ResourceWithConfigure = &vmInstances{} - vmResourceMutex = &sync.Mutex{} + _ resource.Resource = &vmInstances{} + _ resource.ResourceWithConfigure = &vmInstances{} ) func NewVirtualMachineInstancesResource() resource.Resource { @@ -312,8 +310,6 @@ func (r *vmInstances) ModifyPlanResponse(ctx context.Context, req resource.Modif } func (r *vmInstances) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - vmResourceMutex.Lock() - defer vmResourceMutex.Unlock() data := vmInstancesResourceModel{} resp.Diagnostics.Append(req.State.Get(ctx, &data)...) @@ -332,8 +328,6 @@ func (r *vmInstances) Read(ctx context.Context, req resource.ReadRequest, resp * } func (r *vmInstances) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - vmResourceMutex.Lock() - defer vmResourceMutex.Unlock() plan := vmInstancesResourceModel{} resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) @@ -422,8 +416,6 @@ func (r *vmInstances) Create(ctx context.Context, req resource.CreateRequest, re } func (r *vmInstances) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - vmResourceMutex.Lock() - defer vmResourceMutex.Unlock() data := vmInstancesResourceModel{} currState := &vmInstancesResourceModel{} req.State.Get(ctx, currState) @@ -470,8 +462,6 @@ func (r *vmInstances) Update(ctx context.Context, req resource.UpdateRequest, re } func (r *vmInstances) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - vmResourceMutex.Lock() - defer vmResourceMutex.Unlock() var data vmInstancesResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) diff --git a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_snapshots.go b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_snapshots.go index 8607c6511..7a20ae994 100644 --- a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_snapshots.go +++ b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_vm_snapshots.go @@ -3,7 +3,6 @@ package provider import ( "context" "fmt" - "sync" "time" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -19,9 +18,8 @@ import ( ) var ( - _ resource.Resource = &vmSnapshots{} - _ resource.ResourceWithConfigure = &vmSnapshots{} - snapshotsResourceMutex = &sync.Mutex{} + _ resource.Resource = &vmSnapshots{} + _ resource.ResourceWithConfigure = &vmSnapshots{} ) func NewVirtualMachineSnapshotsResource() resource.Resource { @@ -119,9 +117,6 @@ func (r *vmSnapshots) getVmSnapshot(id string) (sdkVmSnapshots.GetResult, error) } func (r *vmSnapshots) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - snapshotsResourceMutex.Lock() - defer snapshotsResourceMutex.Unlock() - data := &vmSnapshotsResourceModel{} resp.Diagnostics.Append(req.State.Get(ctx, &data)...) @@ -141,8 +136,6 @@ func (r *vmSnapshots) Read(ctx context.Context, req resource.ReadRequest, resp * } func (r *vmSnapshots) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - snapshotsResourceMutex.Lock() - defer snapshotsResourceMutex.Unlock() plan := &vmSnapshotsResourceModel{} diags := req.Plan.Get(ctx, &plan) @@ -182,8 +175,6 @@ func (r *vmSnapshots) Update(ctx context.Context, req resource.UpdateRequest, re } func (r *vmSnapshots) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - snapshotsResourceMutex.Lock() - defer snapshotsResourceMutex.Unlock() var data vmSnapshotsResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...) diff --git a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_volume_attach.go b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_volume_attach.go index 7bfc10f38..0224a58e8 100644 --- a/mgc/terraform-provider-mgc/internal/provider/mgc_resource_volume_attach.go +++ b/mgc/terraform-provider-mgc/internal/provider/mgc_resource_volume_attach.go @@ -3,7 +3,6 @@ package provider import ( "context" "fmt" - "sync" "time" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -21,10 +20,6 @@ const ( AttachVolumeCompletedStatus = "completed" ) -var ( - volumeAttachMutex = &sync.Mutex{} -) - type VolumeAttach struct { sdkClient *mgcSdk.Client blockStorageVolumes sdkVolumes.Service @@ -81,8 +76,6 @@ func (r *VolumeAttach) Schema(ctx context.Context, req resource.SchemaRequest, r } func (r *VolumeAttach) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { - volumeAttachMutex.Lock() - defer volumeAttachMutex.Unlock() var model VolumeAttachResourceModel diags := req.Plan.Get(ctx, &model) @@ -116,8 +109,6 @@ func (r *VolumeAttach) Create(ctx context.Context, req resource.CreateRequest, r } func (r *VolumeAttach) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { - volumeAttachMutex.Lock() - defer volumeAttachMutex.Unlock() var model VolumeAttachResourceModel diags := req.State.Get(ctx, &model) @@ -150,8 +141,6 @@ func (r *VolumeAttach) Read(ctx context.Context, req resource.ReadRequest, resp } func (r *VolumeAttach) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { - volumeAttachMutex.Lock() - defer volumeAttachMutex.Unlock() var model VolumeAttachResourceModel diags := req.Plan.Get(ctx, &model) @@ -185,8 +174,6 @@ func (r *VolumeAttach) Update(ctx context.Context, req resource.UpdateRequest, r } func (r *VolumeAttach) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - volumeAttachMutex.Lock() - defer volumeAttachMutex.Unlock() var model VolumeAttachResourceModel diags := req.State.Get(ctx, &model)