Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions internal/services/mongodb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ func ResourceInstance() *schema.Resource {
},
Description: "List of tags [\"tag1\", \"tag2\", ...] attached to a MongoDB instance",
},
"snapshot_schedule_frequency_hours": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Snapshot schedule frequency in hours",
},
"snapshot_schedule_retention_days": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Snapshot schedule retention in days",
},
"is_snapshot_schedule_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Enable or disable automatic snapshot scheduling",
},
"settings": {
Type: schema.TypeMap,
Description: "Map of settings to define for the instance.",
Expand Down Expand Up @@ -367,6 +385,11 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m any)
return diag.FromErr(err)
}

err = configureSnapshotScheduleOnCreate(ctx, d, mongodbAPI, region, res.ID)
if err != nil {
return diag.FromErr(err)
}

return ResourceInstanceRead(ctx, d, m)
}

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

if instance.SnapshotSchedule != nil {
_ = d.Set("snapshot_schedule_frequency_hours", int(instance.SnapshotSchedule.FrequencyHours))
_ = d.Set("snapshot_schedule_retention_days", int(instance.SnapshotSchedule.RetentionDays))
_ = d.Set("is_snapshot_schedule_enabled", instance.SnapshotSchedule.Enabled)
}

if instance.Volume != nil {
_ = d.Set("volume_type", instance.Volume.Type)
_ = d.Set("volume_size_in_gb", int(instance.Volume.SizeBytes/scw.GB))
Expand Down Expand Up @@ -553,6 +582,10 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m any)
}
}

if updateSnapshotScheduleFields(d, req) {
shouldUpdateInstance = true
}

if shouldUpdateInstance {
_, err = mongodbAPI.UpdateInstance(req, scw.WithContext(ctx))
if err != nil {
Expand Down Expand Up @@ -690,3 +723,61 @@ func ResourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m any)

return nil
}

func updateSnapshotScheduleFields(d *schema.ResourceData, req *mongodb.UpdateInstanceRequest) bool {
hasUpdates := false

if d.HasChange("snapshot_schedule_frequency_hours") {
req.SnapshotScheduleFrequencyHours = types.ExpandUint32Ptr(d.Get("snapshot_schedule_frequency_hours"))
hasUpdates = true
}

if d.HasChange("snapshot_schedule_retention_days") {
req.SnapshotScheduleRetentionDays = types.ExpandUint32Ptr(d.Get("snapshot_schedule_retention_days"))
hasUpdates = true
}

if d.HasChange("is_snapshot_schedule_enabled") {
req.IsSnapshotScheduleEnabled = types.ExpandBoolPtr(d.Get("is_snapshot_schedule_enabled"))
hasUpdates = true
}

return hasUpdates
}

func configureSnapshotScheduleOnCreate(ctx context.Context, d *schema.ResourceData, mongodbAPI *mongodb.API, region scw.Region, instanceID string) error {
mustUpdate := false
updateReq := &mongodb.UpdateInstanceRequest{
Region: region,
InstanceID: instanceID,
}

if snapshotFrequency, ok := d.GetOk("snapshot_schedule_frequency_hours"); ok {
updateReq.SnapshotScheduleFrequencyHours = scw.Uint32Ptr(uint32(snapshotFrequency.(int)))
mustUpdate = true
}

if snapshotRetention, ok := d.GetOk("snapshot_schedule_retention_days"); ok {
updateReq.SnapshotScheduleRetentionDays = scw.Uint32Ptr(uint32(snapshotRetention.(int)))
mustUpdate = true
}

if snapshotEnabled, ok := d.GetOk("is_snapshot_schedule_enabled"); ok {
updateReq.IsSnapshotScheduleEnabled = scw.BoolPtr(snapshotEnabled.(bool))
mustUpdate = true
}

if mustUpdate {
_, err := mongodbAPI.UpdateInstance(updateReq, scw.WithContext(ctx))
if err != nil {
return err
}

_, err = waitForInstance(ctx, mongodbAPI, region, instanceID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
}

return nil
}
55 changes: 55 additions & 0 deletions internal/services/mongodb/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,61 @@ func TestAccMongoDBInstance_VolumeUpdate(t *testing.T) {
})
}

func TestAccMongoDBInstance_SnapshotSchedule(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: IsInstanceDestroyed(tt),
Steps: []resource.TestStep{
{
Config: `
resource scaleway_mongodb_instance main {
name = "test-mongodb-snapshot-schedule"
version = "7.0.12"
node_type = "MGDB-PLAY2-NANO"
node_number = 1
user_name = "my_initial_user"
password = "thiZ_is_v&ry_s3cret"
snapshot_schedule_frequency_hours = 24
snapshot_schedule_retention_days = 7
is_snapshot_schedule_enabled = true
}
`,
Check: resource.ComposeTestCheckFunc(
isMongoDBInstancePresent(tt, "scaleway_mongodb_instance.main"),
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_frequency_hours", "24"),
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_retention_days", "7"),
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "is_snapshot_schedule_enabled", "true"),
),
},
{
Config: `
resource scaleway_mongodb_instance main {
name = "test-mongodb-snapshot-schedule"
version = "7.0.12"
node_type = "MGDB-PLAY2-NANO"
node_number = 1
user_name = "my_initial_user"
password = "thiZ_is_v&ry_s3cret"
snapshot_schedule_frequency_hours = 12
snapshot_schedule_retention_days = 14
is_snapshot_schedule_enabled = false
}
`,
Check: resource.ComposeTestCheckFunc(
isMongoDBInstancePresent(tt, "scaleway_mongodb_instance.main"),
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_frequency_hours", "12"),
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "snapshot_schedule_retention_days", "14"),
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "is_snapshot_schedule_enabled", "false"),
),
},
},
})
}

func TestAccMongoDBInstance_UpdateNameTagsUser(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
Expand Down
Loading
Loading