@@ -215,6 +215,24 @@ func ResourceInstance() *schema.Resource {
215
215
},
216
216
Description : "List of tags [\" tag1\" , \" tag2\" , ...] attached to a MongoDB instance" ,
217
217
},
218
+ "snapshot_schedule_frequency_hours" : {
219
+ Type : schema .TypeInt ,
220
+ Optional : true ,
221
+ Computed : true ,
222
+ Description : "Snapshot schedule frequency in hours" ,
223
+ },
224
+ "snapshot_schedule_retention_days" : {
225
+ Type : schema .TypeInt ,
226
+ Optional : true ,
227
+ Computed : true ,
228
+ Description : "Snapshot schedule retention in days" ,
229
+ },
230
+ "is_snapshot_schedule_enabled" : {
231
+ Type : schema .TypeBool ,
232
+ Optional : true ,
233
+ Computed : true ,
234
+ Description : "Enable or disable automatic snapshot scheduling" ,
235
+ },
218
236
"settings" : {
219
237
Type : schema .TypeMap ,
220
238
Description : "Map of settings to define for the instance." ,
@@ -367,6 +385,11 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m any)
367
385
return diag .FromErr (err )
368
386
}
369
387
388
+ err = configureSnapshotScheduleOnCreate (ctx , d , mongodbAPI , region , res .ID )
389
+ if err != nil {
390
+ return diag .FromErr (err )
391
+ }
392
+
370
393
return ResourceInstanceRead (ctx , d , m )
371
394
}
372
395
@@ -401,6 +424,12 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m any) di
401
424
_ = d .Set ("created_at" , instance .CreatedAt .Format (time .RFC3339 ))
402
425
_ = d .Set ("region" , instance .Region .String ())
403
426
427
+ if instance .SnapshotSchedule != nil {
428
+ _ = d .Set ("snapshot_schedule_frequency_hours" , int (instance .SnapshotSchedule .FrequencyHours ))
429
+ _ = d .Set ("snapshot_schedule_retention_days" , int (instance .SnapshotSchedule .RetentionDays ))
430
+ _ = d .Set ("is_snapshot_schedule_enabled" , instance .SnapshotSchedule .Enabled )
431
+ }
432
+
404
433
if instance .Volume != nil {
405
434
_ = d .Set ("volume_type" , instance .Volume .Type )
406
435
_ = d .Set ("volume_size_in_gb" , int (instance .Volume .SizeBytes / scw .GB ))
@@ -553,6 +582,10 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m any)
553
582
}
554
583
}
555
584
585
+ if updateSnapshotScheduleFields (d , req ) {
586
+ shouldUpdateInstance = true
587
+ }
588
+
556
589
if shouldUpdateInstance {
557
590
_ , err = mongodbAPI .UpdateInstance (req , scw .WithContext (ctx ))
558
591
if err != nil {
@@ -690,3 +723,61 @@ func ResourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m any)
690
723
691
724
return nil
692
725
}
726
+
727
+ func updateSnapshotScheduleFields (d * schema.ResourceData , req * mongodb.UpdateInstanceRequest ) bool {
728
+ hasUpdates := false
729
+
730
+ if d .HasChange ("snapshot_schedule_frequency_hours" ) {
731
+ req .SnapshotScheduleFrequencyHours = types .ExpandUint32Ptr (d .Get ("snapshot_schedule_frequency_hours" ))
732
+ hasUpdates = true
733
+ }
734
+
735
+ if d .HasChange ("snapshot_schedule_retention_days" ) {
736
+ req .SnapshotScheduleRetentionDays = types .ExpandUint32Ptr (d .Get ("snapshot_schedule_retention_days" ))
737
+ hasUpdates = true
738
+ }
739
+
740
+ if d .HasChange ("is_snapshot_schedule_enabled" ) {
741
+ req .IsSnapshotScheduleEnabled = types .ExpandBoolPtr (d .Get ("is_snapshot_schedule_enabled" ))
742
+ hasUpdates = true
743
+ }
744
+
745
+ return hasUpdates
746
+ }
747
+
748
+ func configureSnapshotScheduleOnCreate (ctx context.Context , d * schema.ResourceData , mongodbAPI * mongodb.API , region scw.Region , instanceID string ) error {
749
+ mustUpdate := false
750
+ updateReq := & mongodb.UpdateInstanceRequest {
751
+ Region : region ,
752
+ InstanceID : instanceID ,
753
+ }
754
+
755
+ if snapshotFrequency , ok := d .GetOk ("snapshot_schedule_frequency_hours" ); ok {
756
+ updateReq .SnapshotScheduleFrequencyHours = scw .Uint32Ptr (uint32 (snapshotFrequency .(int )))
757
+ mustUpdate = true
758
+ }
759
+
760
+ if snapshotRetention , ok := d .GetOk ("snapshot_schedule_retention_days" ); ok {
761
+ updateReq .SnapshotScheduleRetentionDays = scw .Uint32Ptr (uint32 (snapshotRetention .(int )))
762
+ mustUpdate = true
763
+ }
764
+
765
+ if snapshotEnabled , ok := d .GetOk ("is_snapshot_schedule_enabled" ); ok {
766
+ updateReq .IsSnapshotScheduleEnabled = scw .BoolPtr (snapshotEnabled .(bool ))
767
+ mustUpdate = true
768
+ }
769
+
770
+ if mustUpdate {
771
+ _ , err := mongodbAPI .UpdateInstance (updateReq , scw .WithContext (ctx ))
772
+ if err != nil {
773
+ return err
774
+ }
775
+
776
+ _ , err = waitForInstance (ctx , mongodbAPI , region , instanceID , d .Timeout (schema .TimeoutCreate ))
777
+ if err != nil {
778
+ return err
779
+ }
780
+ }
781
+
782
+ return nil
783
+ }
0 commit comments