@@ -215,6 +215,24 @@ func ResourceInstance() *schema.Resource {
215215 },
216216 Description : "List of tags [\" tag1\" , \" tag2\" , ...] attached to a MongoDB instance" ,
217217 },
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+ },
218236 "settings" : {
219237 Type : schema .TypeMap ,
220238 Description : "Map of settings to define for the instance." ,
@@ -367,6 +385,12 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m any)
367385 return diag .FromErr (err )
368386 }
369387
388+ // Configure snapshot scheduling after instance creation
389+ err = configureSnapshotScheduleOnCreate (ctx , d , mongodbAPI , region , res .ID )
390+ if err != nil {
391+ return diag .FromErr (err )
392+ }
393+
370394 return ResourceInstanceRead (ctx , d , m )
371395}
372396
@@ -401,6 +425,12 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m any) di
401425 _ = d .Set ("created_at" , instance .CreatedAt .Format (time .RFC3339 ))
402426 _ = d .Set ("region" , instance .Region .String ())
403427
428+ if instance .SnapshotSchedule != nil {
429+ _ = d .Set ("snapshot_schedule_frequency_hours" , int (instance .SnapshotSchedule .FrequencyHours ))
430+ _ = d .Set ("snapshot_schedule_retention_days" , int (instance .SnapshotSchedule .RetentionDays ))
431+ _ = d .Set ("is_snapshot_schedule_enabled" , instance .SnapshotSchedule .Enabled )
432+ }
433+
404434 if instance .Volume != nil {
405435 _ = d .Set ("volume_type" , instance .Volume .Type )
406436 _ = d .Set ("volume_size_in_gb" , int (instance .Volume .SizeBytes / scw .GB ))
@@ -553,6 +583,10 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m any)
553583 }
554584 }
555585
586+ if updateSnapshotScheduleFields (d , req ) {
587+ shouldUpdateInstance = true
588+ }
589+
556590 if shouldUpdateInstance {
557591 _ , err = mongodbAPI .UpdateInstance (req , scw .WithContext (ctx ))
558592 if err != nil {
@@ -690,3 +724,64 @@ func ResourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m any)
690724
691725 return nil
692726}
727+
728+ // updateSnapshotScheduleFields updates snapshot scheduling fields in the UpdateInstanceRequest
729+ // Returns true if any field was updated
730+ func updateSnapshotScheduleFields (d * schema.ResourceData , req * mongodb.UpdateInstanceRequest ) bool {
731+ hasUpdates := false
732+
733+ if d .HasChange ("snapshot_schedule_frequency_hours" ) {
734+ req .SnapshotScheduleFrequencyHours = types .ExpandUint32Ptr (d .Get ("snapshot_schedule_frequency_hours" ))
735+ hasUpdates = true
736+ }
737+
738+ if d .HasChange ("snapshot_schedule_retention_days" ) {
739+ req .SnapshotScheduleRetentionDays = types .ExpandUint32Ptr (d .Get ("snapshot_schedule_retention_days" ))
740+ hasUpdates = true
741+ }
742+
743+ if d .HasChange ("is_snapshot_schedule_enabled" ) {
744+ req .IsSnapshotScheduleEnabled = types .ExpandBoolPtr (d .Get ("is_snapshot_schedule_enabled" ))
745+ hasUpdates = true
746+ }
747+
748+ return hasUpdates
749+ }
750+
751+ // configureSnapshotScheduleOnCreate configures snapshot scheduling after instance creation
752+ func configureSnapshotScheduleOnCreate (ctx context.Context , d * schema.ResourceData , mongodbAPI * mongodb.API , region scw.Region , instanceID string ) error {
753+ mustUpdate := false
754+ updateReq := & mongodb.UpdateInstanceRequest {
755+ Region : region ,
756+ InstanceID : instanceID ,
757+ }
758+
759+ if snapshotFrequency , ok := d .GetOk ("snapshot_schedule_frequency_hours" ); ok {
760+ updateReq .SnapshotScheduleFrequencyHours = scw .Uint32Ptr (uint32 (snapshotFrequency .(int )))
761+ mustUpdate = true
762+ }
763+
764+ if snapshotRetention , ok := d .GetOk ("snapshot_schedule_retention_days" ); ok {
765+ updateReq .SnapshotScheduleRetentionDays = scw .Uint32Ptr (uint32 (snapshotRetention .(int )))
766+ mustUpdate = true
767+ }
768+
769+ if snapshotEnabled , ok := d .GetOk ("is_snapshot_schedule_enabled" ); ok {
770+ updateReq .IsSnapshotScheduleEnabled = scw .BoolPtr (snapshotEnabled .(bool ))
771+ mustUpdate = true
772+ }
773+
774+ if mustUpdate {
775+ _ , err := mongodbAPI .UpdateInstance (updateReq , scw .WithContext (ctx ))
776+ if err != nil {
777+ return err
778+ }
779+
780+ _ , err = waitForInstance (ctx , mongodbAPI , region , instanceID , d .Timeout (schema .TimeoutCreate ))
781+ if err != nil {
782+ return err
783+ }
784+ }
785+
786+ return nil
787+ }
0 commit comments