@@ -3,17 +3,14 @@ package mongodb
33import (
44 "context"
55 "errors"
6- "fmt"
76 "strings"
87 "time"
98
109 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1210 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1311 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1412 mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
1513 "github.com/scaleway/scaleway-sdk-go/scw"
16- "github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
1714 "github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1815 "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1916 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
@@ -45,17 +42,21 @@ func ResourceInstance() *schema.Resource {
4542 Type : schema .TypeString ,
4643 Optional : true ,
4744 Computed : true ,
48- Description : "Name of the mongoDB cluster" ,
45+ Description : "Name of the MongoDB cluster" ,
4946 },
5047 "version" : {
5148 Type : schema .TypeString ,
5249 Optional : true ,
53- Description : "Mongodb version of the instance" ,
50+ Computed : true ,
51+ Description : "MongoDB version of the instance" ,
52+ ConflictsWith : []string {
53+ "snapshot_id" ,
54+ },
5455 },
5556 "node_number" : {
5657 Type : schema .TypeInt ,
57- Optional : true ,
58- Description : "number of node in the instance" ,
58+ Required : true ,
59+ Description : "Number of nodes in the instance" ,
5960 },
6061 "node_type" : {
6162 Type : schema .TypeString ,
@@ -67,19 +68,25 @@ func ResourceInstance() *schema.Resource {
6768 Type : schema .TypeString ,
6869 Optional : true ,
6970 Description : "Name of the user created when the cluster is created" ,
71+ ConflictsWith : []string {
72+ "snapshot_id" ,
73+ },
7074 },
7175 "password" : {
7276 Type : schema .TypeString ,
7377 Sensitive : true ,
7478 Optional : true ,
7579 Description : "Password of the user" ,
80+ ConflictsWith : []string {
81+ "snapshot_id" ,
82+ },
7683 },
7784 // volume
7885 "volume_type" : {
7986 Type : schema .TypeString ,
8087 Default : mongodb .VolumeTypeSbs5k ,
8188 Optional : true ,
82- Description : "Volume size of instance. " ,
89+ Description : "Volume type of the instance" ,
8390 },
8491 "volume_size_in_gb" : {
8592 Type : schema .TypeInt ,
@@ -92,7 +99,12 @@ func ResourceInstance() *schema.Resource {
9299 Type : schema .TypeString ,
93100 Optional : true ,
94101 ForceNew : true ,
95- Description : "Snapshot id" ,
102+ Description : "Snapshot ID to restore the MongoDB instance from" ,
103+ ConflictsWith : []string {
104+ "user_name" ,
105+ "password" ,
106+ "version" ,
107+ },
96108 },
97109 //endpoint
98110 "private_network" : {
@@ -134,7 +146,7 @@ func ResourceInstance() *schema.Resource {
134146 "dns_records" : {
135147 Type : schema .TypeString ,
136148 Computed : true ,
137- Description : "The dns_record of your endpoint" ,
149+ Description : "The DNS record of your endpoint" ,
138150 },
139151 // computed
140152 "endpoint_id" : {
@@ -167,7 +179,7 @@ func ResourceInstance() *schema.Resource {
167179 "dns_record" : {
168180 Type : schema .TypeString ,
169181 Computed : true ,
170- Description : "The dns_record of your endpoint" ,
182+ Description : "The DNS record of your endpoint" ,
171183 },
172184 },
173185 },
@@ -178,7 +190,7 @@ func ResourceInstance() *schema.Resource {
178190 Elem : & schema.Schema {
179191 Type : schema .TypeString ,
180192 },
181- Description : "List of tags [\" tag1\" , \" tag2\" , ...] attached to a Mongodb instance" ,
193+ Description : "List of tags [\" tag1\" , \" tag2\" , ...] attached to a MongoDB instance" ,
182194 },
183195 "settings" : {
184196 Type : schema .TypeMap ,
@@ -191,39 +203,17 @@ func ResourceInstance() *schema.Resource {
191203 "created_at" : {
192204 Type : schema .TypeString ,
193205 Computed : true ,
194- Description : "The date and time of the creation of the Mongodb instance" ,
206+ Description : "The date and time of the creation of the MongoDB instance" ,
195207 },
196208 "updated_at" : {
197209 Type : schema .TypeString ,
198210 Computed : true ,
199- Description : "The date and time of the last update of the Mongodb instance" ,
211+ Description : "The date and time of the last update of the MongoDB instance" ,
200212 },
201213 // Common
202214 "region" : regional .Schema (),
203215 "project_id" : account .ProjectIDSchema (),
204216 },
205- CustomizeDiff : customdiff .All (
206- cdf .LocalityCheck ("private_network.#.id" ),
207- func (ctx context.Context , diff * schema.ResourceDiff , v interface {}) error {
208- snapshotID := diff .Get ("snapshot_id" )
209-
210- if snapshotID == nil || snapshotID == "" {
211- if diff .Get ("user_name" ) == nil || diff .Get ("user_name" ) == "" {
212- return fmt .Errorf ("`user_name` must be provided when `snapshot_id` is not set" )
213- }
214- if diff .Get ("password" ) == nil || diff .Get ("password" ) == "" {
215- return fmt .Errorf ("`password` must be provided when `snapshot_id` is not set" )
216- }
217- if diff .Get ("version" ) == nil || diff .Get ("version" ) == "" {
218- return fmt .Errorf ("`version` must be provided when `snapshot_id` is not set" )
219- }
220- if diff .Get ("node_number" ) == nil || diff .Get ("node_number" ) == "" {
221- return fmt .Errorf ("`node_number` must be provided when `snapshot_id` is not set" )
222- }
223- }
224- return nil
225- },
226- ),
227217 }
228218}
229219
@@ -242,9 +232,11 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
242232 volume := & mongodb.RestoreSnapshotRequestVolumeDetails {
243233 VolumeType : mongodb .VolumeType (d .Get ("volume_type" ).(string )),
244234 }
235+ id := regional .ExpandID (snapshotID .(string ))
245236 restoreSnapshotRequest := & mongodb.RestoreSnapshotRequest {
246- SnapshotID : snapshotID .( string ) ,
237+ SnapshotID : id . ID ,
247238 InstanceName : types .ExpandOrGenerateString (d .Get ("name" ), "mongodb" ),
239+ NodeNumber : * nodeNumber ,
248240 NodeType : d .Get ("node_type" ).(string ),
249241 Volume : volume ,
250242 }
0 commit comments