Skip to content

Commit

Permalink
CDI-790: add mutex to resolve race condition between cdn_origin_shiel…
Browse files Browse the repository at this point in the history
…ding and cdn_applied_preset resources (#117)
  • Loading branch information
andrei-lukyanchyk authored Jul 11, 2024
1 parent d8ee7e2 commit 141c33d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gcore/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions gcore/resource_gcore_cdn_applied_preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions gcore/resource_gcore_cdn_origin_shielding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions gcore/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -52,6 +53,7 @@ const (
type Config struct {
Provider *gcorecloud.ProviderClient
CDNClient gcdn.ClientService
CDNMutex *sync.Mutex
StorageClient *storageSDK.SDK
DNSClient *dnssdk.Client
}
Expand Down

0 comments on commit 141c33d

Please sign in to comment.