|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "sort" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1" |
| 12 | + "github.com/scaleway/scaleway-sdk-go/api/redis/v1" |
| 13 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 14 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" |
| 15 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal" |
| 16 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" |
| 17 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" |
| 18 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + defaultRedisClusterTimeout = 15 * time.Minute |
| 23 | + defaultWaitRedisClusterRetryInterval = 5 * time.Second |
| 24 | +) |
| 25 | + |
| 26 | +func newAPI(m interface{}) *mongodb.API { |
| 27 | + return mongodb.NewAPI(meta.ExtractScwClient(m)) |
| 28 | +} |
| 29 | + |
| 30 | +// newAPIWithZone returns a new Redis API and the zone for a Create request |
| 31 | +func newAPIWithZone(d *schema.ResourceData, m interface{}) (*mongodb.API, scw.Zone, error) { |
| 32 | + zone, err := meta.ExtractZone(d, m) |
| 33 | + if err != nil { |
| 34 | + return nil, "", err |
| 35 | + } |
| 36 | + return newAPI(m), zone, nil |
| 37 | +} |
| 38 | + |
| 39 | +// NewAPIWithZoneAndID returns a Redis API with zone and ID extracted from the state |
| 40 | +func NewAPIWithZoneAndID(m interface{}, id string) (*redis.API, scw.Zone, string, error) { |
| 41 | + zone, ID, err := zonal.ParseID(id) |
| 42 | + if err != nil { |
| 43 | + return nil, "", "", err |
| 44 | + } |
| 45 | + return newAPI(m), zone, ID, nil |
| 46 | +} |
| 47 | + |
| 48 | +func waitForInstance(ctx context.Context, api *mongodb.API, zone scw.Zone, id string, timeout time.Duration) (*redis.Cluster, error) { |
| 49 | + retryInterval := defaultWaitRedisClusterRetryInterval |
| 50 | + if transport.DefaultWaitRetryInterval != nil { |
| 51 | + retryInterval = *transport.DefaultWaitRetryInterval |
| 52 | + } |
| 53 | + |
| 54 | + return api.WaitForInstance() |
| 55 | +} |
| 56 | + |
| 57 | +func waitForCluster(ctx context.Context, api *redis.API, zone scw.Zone, id string, timeout time.Duration) (*redis.Cluster, error) { |
| 58 | + retryInterval := defaultWaitRedisClusterRetryInterval |
| 59 | + if transport.DefaultWaitRetryInterval != nil { |
| 60 | + retryInterval = *transport.DefaultWaitRetryInterval |
| 61 | + } |
| 62 | + |
| 63 | + return api.WaitForCluster(&redis.WaitForClusterRequest{ |
| 64 | + Zone: zone, |
| 65 | + Timeout: scw.TimeDurationPtr(timeout), |
| 66 | + ClusterID: id, |
| 67 | + RetryInterval: &retryInterval, |
| 68 | + }, scw.WithContext(ctx)) |
| 69 | +} |
| 70 | + |
| 71 | +func privateNetworkSetHash(v interface{}) int { |
| 72 | + var buf bytes.Buffer |
| 73 | + |
| 74 | + m := v.(map[string]interface{}) |
| 75 | + if pnID, ok := m["id"]; ok { |
| 76 | + buf.WriteString(locality.ExpandID(pnID)) |
| 77 | + } |
| 78 | + |
| 79 | + if serviceIPs, ok := m["service_ips"]; ok { |
| 80 | + // Sort the service IPs before generating the hash. |
| 81 | + ips := serviceIPs.([]interface{}) |
| 82 | + sort.Slice(ips, func(i, j int) bool { |
| 83 | + return ips[i].(string) < ips[j].(string) |
| 84 | + }) |
| 85 | + |
| 86 | + for i, item := range ips { |
| 87 | + buf.WriteString(fmt.Sprintf("%d-%s-", i, item.(string))) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return types.StringHashcode(buf.String()) |
| 92 | +} |
0 commit comments