@@ -3,6 +3,7 @@ package mongodb
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "strings"
78 "time"
89
@@ -48,12 +49,12 @@ func ResourceInstance() *schema.Resource {
4849 },
4950 "version" : {
5051 Type : schema .TypeString ,
51- Required : true ,
52+ Optional : true ,
5253 Description : "Mongodb version of the instance" ,
5354 },
5455 "node_number" : {
5556 Type : schema .TypeInt ,
56- Required : true ,
57+ Optional : true ,
5758 Description : "number of node in the instance" ,
5859 },
5960 "node_type" : {
@@ -64,13 +65,13 @@ func ResourceInstance() *schema.Resource {
6465 },
6566 "user_name" : {
6667 Type : schema .TypeString ,
67- Required : true ,
68+ Optional : true ,
6869 Description : "Name of the user created when the cluster is created" ,
6970 },
7071 "password" : {
7172 Type : schema .TypeString ,
7273 Sensitive : true ,
73- Required : true ,
74+ Optional : true ,
7475 Description : "Password of the user" ,
7576 },
7677 // volume
@@ -87,6 +88,12 @@ func ResourceInstance() *schema.Resource {
8788 Description : "Volume size (in GB)" ,
8889 ValidateFunc : validation .IntDivisibleBy (5 ),
8990 },
91+ "snapshot_id" : {
92+ Type : schema .TypeString ,
93+ Optional : true ,
94+ ForceNew : true ,
95+ Description : "Snapshot id" ,
96+ },
9097 //endpoint
9198 "private_network" : {
9299 Type : schema .TypeSet ,
@@ -189,14 +196,33 @@ func ResourceInstance() *schema.Resource {
189196 "updated_at" : {
190197 Type : schema .TypeString ,
191198 Computed : true ,
192- Description : "The date and time of the last update of the Redis cluster " ,
199+ Description : "The date and time of the last update of the Mongodb instance " ,
193200 },
194201 // Common
195202 "region" : regional .Schema (),
196203 "project_id" : account .ProjectIDSchema (),
197204 },
198205 CustomizeDiff : customdiff .All (
199206 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+ },
200226 ),
201227 }
202228}
@@ -209,52 +235,70 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
209235 }
210236
211237 nodeNumber := scw .Uint32Ptr (uint32 (d .Get ("node_number" ).(int )))
212- createReq := & mongodb.CreateInstanceRequest {
213- ProjectID : d .Get ("project_id" ).(string ),
214- Name : types .ExpandOrGenerateString (d .Get ("name" ), "mongodb" ),
215- Version : d .Get ("version" ).(string ),
216- NodeType : d .Get ("node_type" ).(string ),
217- NodeNumber : * nodeNumber ,
218- UserName : d .Get ("user_name" ).(string ),
219- Password : d .Get ("password" ).(string ),
220- }
221238
222- volumeRequestDetails := & mongodb.CreateInstanceRequestVolumeDetails {
223- VolumeType : mongodb .VolumeType (d .Get ("volume_type" ).(string )),
224- }
225- volumeSize , volumeSizeExist := d .GetOk ("volume_size_in_gb" )
226- if volumeSizeExist {
227- volumeRequestDetails .VolumeSize = scw .Size (uint64 (volumeSize .(int )) * uint64 (scw .GB ))
239+ snapshotID , exist := d .GetOk ("snapshot_id" )
240+ res := & mongodb.Instance {}
241+ if exist {
242+ volume := & mongodb.RestoreSnapshotRequestVolumeDetails {
243+ VolumeType : mongodb .VolumeType (d .Get ("volume_type" ).(string )),
244+ }
245+ restoreSnapshotRequest := & mongodb.RestoreSnapshotRequest {
246+ SnapshotID : snapshotID .(string ),
247+ InstanceName : types .ExpandOrGenerateString (d .Get ("name" ), "mongodb" ),
248+ NodeType : d .Get ("node_type" ).(string ),
249+ Volume : volume ,
250+ }
251+ res , err = mongodbAPI .RestoreSnapshot (restoreSnapshotRequest , scw .WithContext (ctx ))
252+ if err != nil {
253+ return diag .FromErr (err )
254+ }
228255 } else {
229- volumeRequestDetails .VolumeSize = scw .Size (defaultVolumeSize * uint64 (scw .GB ))
230- }
231- createReq .Volume = volumeRequestDetails
232256
233- tags , tagsExist := d .GetOk ("tags" )
234- if tagsExist {
235- createReq .Tags = types .ExpandStrings (tags )
236- }
257+ createReq := & mongodb.CreateInstanceRequest {
258+ ProjectID : d .Get ("project_id" ).(string ),
259+ Name : types .ExpandOrGenerateString (d .Get ("name" ), "mongodb" ),
260+ Version : d .Get ("version" ).(string ),
261+ NodeType : d .Get ("node_type" ).(string ),
262+ NodeNumber : * nodeNumber ,
263+ UserName : d .Get ("user_name" ).(string ),
264+ Password : d .Get ("password" ).(string ),
265+ }
266+
267+ volumeRequestDetails := & mongodb.CreateInstanceRequestVolumeDetails {
268+ VolumeType : mongodb .VolumeType (d .Get ("volume_type" ).(string )),
269+ }
270+ volumeSize , volumeSizeExist := d .GetOk ("volume_size_in_gb" )
271+ if volumeSizeExist {
272+ volumeRequestDetails .VolumeSize = scw .Size (uint64 (volumeSize .(int )) * uint64 (scw .GB ))
273+ } else {
274+ volumeRequestDetails .VolumeSize = scw .Size (defaultVolumeSize * uint64 (scw .GB ))
275+ }
276+ createReq .Volume = volumeRequestDetails
237277
238- pn , pnExists := d .GetOk ("private_network" )
239- if pnExists {
240- pnSpecs , err := expandPrivateNetwork (pn .(* schema.Set ).List ())
278+ tags , tagsExist := d .GetOk ("tags" )
279+ if tagsExist {
280+ createReq .Tags = types .ExpandStrings (tags )
281+ }
282+
283+ pn , pnExists := d .GetOk ("private_network" )
284+ if pnExists {
285+ pnSpecs , err := expandPrivateNetwork (pn .(* schema.Set ).List ())
286+ if err != nil {
287+ return diag .FromErr (err )
288+ }
289+ createReq .Endpoints = pnSpecs
290+ } else {
291+ epSpecs := make ([]* mongodb.EndpointSpec , 0 , 1 )
292+ spec := & mongodb.EndpointSpecPublicDetails {}
293+ createReq .Endpoints = append (epSpecs , & mongodb.EndpointSpec {Public : spec })
294+ }
295+
296+ res , err = mongodbAPI .CreateInstance (createReq , scw .WithContext (ctx ))
241297 if err != nil {
242298 return diag .FromErr (err )
243299 }
244- createReq .Endpoints = pnSpecs
245- } else {
246- epSpecs := make ([]* mongodb.EndpointSpec , 0 , 1 )
247- spec := & mongodb.EndpointSpecPublicDetails {}
248- createReq .Endpoints = append (epSpecs , & mongodb.EndpointSpec {Public : spec })
249300 }
250-
251- res , err := mongodbAPI .CreateInstance (createReq , scw .WithContext (ctx ))
252- if err != nil {
253- return diag .FromErr (err )
254- }
255-
256301 d .SetId (zonal .NewIDString (zone , res .ID ))
257-
258302 _ , err = waitForInstance (ctx , mongodbAPI , res .Region , res .ID , d .Timeout (schema .TimeoutCreate ))
259303 if err != nil {
260304 return diag .FromErr (err )
@@ -294,7 +338,7 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
294338
295339 if instance .Volume != nil {
296340 _ = d .Set ("volume_type" , instance .Volume .Type )
297- _ = d .Set ("volume_size_in_gb" , instance .Volume .Size )
341+ _ = d .Set ("volume_size_in_gb" , int ( instance .Volume .Size / scw . GB ) )
298342 }
299343
300344 privateNetworkEndpoints , privateNetworkExists := flattenPrivateNetwork (instance .Endpoints )
@@ -338,7 +382,7 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
338382 if newSize % 5 != 0 {
339383 return diag .FromErr (errors .New ("volume_size_in_gb must be a multiple of 5" ))
340384 }
341- size := scw .Size (newSize )
385+ size := scw .Size (newSize * uint64 ( scw . GB ) )
342386
343387 upgradeInstanceRequests := mongodb.UpgradeInstanceRequest {
344388 InstanceID : ID ,
@@ -350,6 +394,8 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
350394 if err != nil {
351395 return diag .FromErr (err )
352396 }
397+ _ , err = waitForInstance (ctx , mongodbAPI , region , ID , d .Timeout (schema .TimeoutUpdate ))
398+
353399 }
354400
355401 req := & mongodb.UpdateInstanceRequest {
0 commit comments