Skip to content

Commit e4835db

Browse files
committed
feat(mongodb): add snapshot scheduling to instances
1 parent 921a5b3 commit e4835db

File tree

4 files changed

+4250
-0
lines changed

4 files changed

+4250
-0
lines changed

internal/services/mongodb/instance.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,11 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m any)
367385
return diag.FromErr(err)
368386
}
369387

388+
err = configureSnapshotScheduleOnCreate(ctx, d, mongodbAPI, region, res.ID)
389+
if err != nil {
390+
return diag.FromErr(err)
391+
}
392+
370393
return ResourceInstanceRead(ctx, d, m)
371394
}
372395

@@ -401,6 +424,12 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m any) di
401424
_ = d.Set("created_at", instance.CreatedAt.Format(time.RFC3339))
402425
_ = d.Set("region", instance.Region.String())
403426

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+
404433
if instance.Volume != nil {
405434
_ = d.Set("volume_type", instance.Volume.Type)
406435
_ = 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)
553582
}
554583
}
555584

585+
if updateSnapshotScheduleFields(d, req) {
586+
shouldUpdateInstance = true
587+
}
588+
556589
if shouldUpdateInstance {
557590
_, err = mongodbAPI.UpdateInstance(req, scw.WithContext(ctx))
558591
if err != nil {
@@ -690,3 +723,62 @@ func ResourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m any)
690723

691724
return nil
692725
}
726+
727+
728+
func updateSnapshotScheduleFields(d *schema.ResourceData, req *mongodb.UpdateInstanceRequest) bool {
729+
hasUpdates := false
730+
731+
if d.HasChange("snapshot_schedule_frequency_hours") {
732+
req.SnapshotScheduleFrequencyHours = types.ExpandUint32Ptr(d.Get("snapshot_schedule_frequency_hours"))
733+
hasUpdates = true
734+
}
735+
736+
if d.HasChange("snapshot_schedule_retention_days") {
737+
req.SnapshotScheduleRetentionDays = types.ExpandUint32Ptr(d.Get("snapshot_schedule_retention_days"))
738+
hasUpdates = true
739+
}
740+
741+
if d.HasChange("is_snapshot_schedule_enabled") {
742+
req.IsSnapshotScheduleEnabled = types.ExpandBoolPtr(d.Get("is_snapshot_schedule_enabled"))
743+
hasUpdates = true
744+
}
745+
746+
return hasUpdates
747+
}
748+
749+
func configureSnapshotScheduleOnCreate(ctx context.Context, d *schema.ResourceData, mongodbAPI *mongodb.API, region scw.Region, instanceID string) error {
750+
mustUpdate := false
751+
updateReq := &mongodb.UpdateInstanceRequest{
752+
Region: region,
753+
InstanceID: instanceID,
754+
}
755+
756+
if snapshotFrequency, ok := d.GetOk("snapshot_schedule_frequency_hours"); ok {
757+
updateReq.SnapshotScheduleFrequencyHours = scw.Uint32Ptr(uint32(snapshotFrequency.(int)))
758+
mustUpdate = true
759+
}
760+
761+
if snapshotRetention, ok := d.GetOk("snapshot_schedule_retention_days"); ok {
762+
updateReq.SnapshotScheduleRetentionDays = scw.Uint32Ptr(uint32(snapshotRetention.(int)))
763+
mustUpdate = true
764+
}
765+
766+
if snapshotEnabled, ok := d.GetOk("is_snapshot_schedule_enabled"); ok {
767+
updateReq.IsSnapshotScheduleEnabled = scw.BoolPtr(snapshotEnabled.(bool))
768+
mustUpdate = true
769+
}
770+
771+
if mustUpdate {
772+
_, err := mongodbAPI.UpdateInstance(updateReq, scw.WithContext(ctx))
773+
if err != nil {
774+
return err
775+
}
776+
777+
_, err = waitForInstance(ctx, mongodbAPI, region, instanceID, d.Timeout(schema.TimeoutCreate))
778+
if err != nil {
779+
return err
780+
}
781+
}
782+
783+
return nil
784+
}

internal/services/mongodb/instance_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,61 @@ func TestAccMongoDBInstance_VolumeUpdate(t *testing.T) {
9595
})
9696
}
9797

98+
func TestAccMongoDBInstance_SnapshotSchedule(t *testing.T) {
99+
tt := acctest.NewTestTools(t)
100+
defer tt.Cleanup()
101+
102+
resource.ParallelTest(t, resource.TestCase{
103+
PreCheck: func() { acctest.PreCheck(t) },
104+
ProviderFactories: tt.ProviderFactories,
105+
CheckDestroy: IsInstanceDestroyed(tt),
106+
Steps: []resource.TestStep{
107+
{
108+
Config: `
109+
resource scaleway_mongodb_instance main {
110+
name = "test-mongodb-snapshot-schedule"
111+
version = "7.0.12"
112+
node_type = "MGDB-PLAY2-NANO"
113+
node_number = 1
114+
user_name = "my_initial_user"
115+
password = "thiZ_is_v&ry_s3cret"
116+
snapshot_schedule_frequency_hours = 24
117+
snapshot_schedule_retention_days = 7
118+
is_snapshot_schedule_enabled = true
119+
}
120+
`,
121+
Check: resource.ComposeTestCheckFunc(
122+
isMongoDBInstancePresent(tt, "scaleway_mongodb_instance.main"),
123+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_frequency_hours", "24"),
124+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_retention_days", "7"),
125+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "is_snapshot_schedule_enabled", "true"),
126+
),
127+
},
128+
{
129+
Config: `
130+
resource scaleway_mongodb_instance main {
131+
name = "test-mongodb-snapshot-schedule"
132+
version = "7.0.12"
133+
node_type = "MGDB-PLAY2-NANO"
134+
node_number = 1
135+
user_name = "my_initial_user"
136+
password = "thiZ_is_v&ry_s3cret"
137+
snapshot_schedule_frequency_hours = 12
138+
snapshot_schedule_retention_days = 14
139+
is_snapshot_schedule_enabled = false
140+
}
141+
`,
142+
Check: resource.ComposeTestCheckFunc(
143+
isMongoDBInstancePresent(tt, "scaleway_mongodb_instance.main"),
144+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_frequency_hours", "12"),
145+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_retention_days", "14"),
146+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "is_snapshot_schedule_enabled", "false"),
147+
),
148+
},
149+
},
150+
})
151+
}
152+
98153
func TestAccMongoDBInstance_UpdateNameTagsUser(t *testing.T) {
99154
tt := acctest.NewTestTools(t)
100155
defer tt.Cleanup()

0 commit comments

Comments
 (0)