From 141c33dd13f0d247e83dc8f336073e0b5894854f Mon Sep 17 00:00:00 2001 From: Andrei Lukyanchyk <125263040+andrei-lukyanchyk@users.noreply.github.com> Date: Thu, 11 Jul 2024 12:05:41 +0200 Subject: [PATCH] CDI-790: add mutex to resolve race condition between cdn_origin_shielding and cdn_applied_preset resources (#117) --- gcore/provider.go | 2 ++ gcore/resource_gcore_cdn_applied_preset.go | 6 ++++++ gcore/resource_gcore_cdn_origin_shielding.go | 6 ++++++ gcore/utils.go | 2 ++ 4 files changed, 16 insertions(+) diff --git a/gcore/provider.go b/gcore/provider.go index 03b5705..84b25e3 100644 --- a/gcore/provider.go +++ b/gcore/provider.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "os" + "sync" dnssdk "github.com/G-Core/gcore-dns-sdk-go" storageSDK "github.com/G-Core/gcore-storage-sdk-go" @@ -283,6 +284,7 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{}, config := Config{ Provider: provider, CDNClient: cdnService, + CDNMutex: &sync.Mutex{}, } userAgent := fmt.Sprintf("terraform/%s", version.Version) diff --git a/gcore/resource_gcore_cdn_applied_preset.go b/gcore/resource_gcore_cdn_applied_preset.go index 1321d14..754135d 100644 --- a/gcore/resource_gcore_cdn_applied_preset.go +++ b/gcore/resource_gcore_cdn_applied_preset.go @@ -52,6 +52,9 @@ func resourceCDNPresetApply(ctx context.Context, d *schema.ResourceData, m inter config := m.(*Config) client := config.CDNClient + config.CDNMutex.Lock() + defer config.CDNMutex.Unlock() + var req presets.ApplyRequest req.ObjectID = d.Get("object_id").(int) @@ -96,6 +99,9 @@ func resourceCDNPresetUnapply(ctx context.Context, d *schema.ResourceData, m int config := m.(*Config) client := config.CDNClient + config.CDNMutex.Lock() + defer config.CDNMutex.Unlock() + if err := client.Presets().Unapply(ctx, d.Get("preset_id").(int), d.Get("object_id").(int)); err != nil { return diag.FromErr(err) } diff --git a/gcore/resource_gcore_cdn_origin_shielding.go b/gcore/resource_gcore_cdn_origin_shielding.go index 2dcaf09..e48d3fa 100644 --- a/gcore/resource_gcore_cdn_origin_shielding.go +++ b/gcore/resource_gcore_cdn_origin_shielding.go @@ -67,6 +67,9 @@ func resourceCDNOriginShieldingUpdate(ctx context.Context, d *schema.ResourceDat config := m.(*Config) client := config.CDNClient + config.CDNMutex.Lock() + defer config.CDNMutex.Unlock() + var req originshielding.UpdateRequest req.ShieldingPop = pointer.ToInt(d.Get("shielding_pop").(int)) @@ -87,6 +90,9 @@ func resourceCDNOriginShieldingDelete(ctx context.Context, d *schema.ResourceDat config := m.(*Config) client := config.CDNClient + config.CDNMutex.Lock() + defer config.CDNMutex.Unlock() + var req originshielding.UpdateRequest var intPointer *int req.ShieldingPop = intPointer diff --git a/gcore/utils.go b/gcore/utils.go index 23be329..74bdb35 100644 --- a/gcore/utils.go +++ b/gcore/utils.go @@ -13,6 +13,7 @@ import ( "reflect" "strconv" "strings" + "sync" dnssdk "github.com/G-Core/gcore-dns-sdk-go" storageSDK "github.com/G-Core/gcore-storage-sdk-go" @@ -52,6 +53,7 @@ const ( type Config struct { Provider *gcorecloud.ProviderClient CDNClient gcdn.ClientService + CDNMutex *sync.Mutex StorageClient *storageSDK.SDK DNSClient *dnssdk.Client }