Skip to content

Commit 608f3c8

Browse files
authored
feat(rdb): add log policy in instance (scaleway#2536)
* feat/add log policy in rdb instance * feat(rdb): update cassette and fix test * feat(rdb): update cassette log policy * feat(rdb): fix unused var
1 parent 01de721 commit 608f3c8

35 files changed

+94853
-71678
lines changed

internal/services/rdb/instance.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,30 @@ func ResourceInstance() *schema.Resource {
282282
},
283283
},
284284
},
285+
"logs_policy": {
286+
Type: schema.TypeList,
287+
Optional: true,
288+
Computed: true,
289+
Description: "Logs policy configuration",
290+
MaxItems: 1,
291+
Elem: &schema.Resource{
292+
Schema: map[string]*schema.Schema{
293+
// Computed
294+
"max_age_retention": {
295+
Type: schema.TypeInt,
296+
Optional: true,
297+
Computed: true,
298+
Description: "The max age (in days) of remote logs to keep on the Database Instance",
299+
},
300+
"total_disk_retention": {
301+
Type: schema.TypeInt,
302+
Optional: true,
303+
Computed: true,
304+
Description: "The max disk size of remote logs to keep on the Database Instance.",
305+
},
306+
},
307+
},
308+
},
285309
// Common
286310
"region": regional.Schema(),
287311
"organization_id": account.OrganizationIDSchema(),
@@ -349,29 +373,36 @@ func ResourceRdbInstanceCreate(ctx context.Context, d *schema.ResourceData, m in
349373

350374
d.SetId(regional.NewIDString(region, res.ID))
351375

376+
mustUpdate := false
377+
updateReq := &rdb.UpdateInstanceRequest{
378+
Region: region,
379+
InstanceID: res.ID,
380+
}
352381
// Configure Schedule Backup
353382
// BackupScheduleFrequency and BackupScheduleRetention can only configure after instance creation
354383
if !d.Get("disable_backup").(bool) {
355-
updateReq := &rdb.UpdateInstanceRequest{
356-
Region: region,
357-
InstanceID: res.ID,
358-
}
359-
360384
updateReq.BackupSameRegion = types.ExpandBoolPtr(d.Get("backup_same_region"))
361-
362385
updateReq.IsBackupScheduleDisabled = scw.BoolPtr(d.Get("disable_backup").(bool))
363386
if backupScheduleFrequency, okFrequency := d.GetOk("backup_schedule_frequency"); okFrequency {
364387
updateReq.BackupScheduleFrequency = scw.Uint32Ptr(uint32(backupScheduleFrequency.(int)))
365388
}
366389
if backupScheduleRetention, okRetention := d.GetOk("backup_schedule_retention"); okRetention {
367390
updateReq.BackupScheduleRetention = scw.Uint32Ptr(uint32(backupScheduleRetention.(int)))
368391
}
392+
mustUpdate = true
393+
}
369394

395+
policyRaw, exist := d.GetOk("logs_policy")
396+
if exist {
397+
updateReq.LogsPolicy = expandInstanceLogsPolicy(policyRaw)
398+
mustUpdate = true
399+
}
400+
401+
if mustUpdate {
370402
_, err = waitForRDBInstance(ctx, rdbAPI, region, res.ID, d.Timeout(schema.TimeoutCreate))
371403
if err != nil {
372404
return diag.FromErr(err)
373405
}
374-
375406
_, err = rdbAPI.UpdateInstance(updateReq, scw.WithContext(ctx))
376407
if err != nil {
377408
return diag.FromErr(err)
@@ -412,7 +443,6 @@ func ResourceRdbInstanceRead(ctx context.Context, d *schema.ResourceData, m inte
412443
}
413444
return diag.FromErr(err)
414445
}
415-
416446
_ = d.Set("name", res.Name)
417447
_ = d.Set("node_type", res.NodeType)
418448
_ = d.Set("engine", res.Engine)
@@ -476,6 +506,9 @@ func ResourceRdbInstanceRead(ctx context.Context, d *schema.ResourceData, m inte
476506
_ = d.Set("settings", flattenInstanceSettings(res.Settings))
477507
_ = d.Set("init_settings", flattenInstanceSettings(res.InitSettings))
478508

509+
// set logs policy
510+
_ = d.Set("logs_policy", flattenInstanceLogsPolicy(res.LogsPolicy))
511+
479512
// set endpoints
480513
enableIpam, err := getIPAMConfigRead(res, m)
481514
if err != nil {
@@ -651,6 +684,13 @@ func ResourceRdbInstanceUpdate(ctx context.Context, d *schema.ResourceData, m in
651684
req.Tags = types.ExpandUpdatedStringsPtr(d.Get("tags"))
652685
}
653686

687+
if d.HasChange("logs_policy") {
688+
policyRaw, exist := d.GetOk("logs_policy")
689+
if exist {
690+
req.LogsPolicy = expandInstanceLogsPolicy(policyRaw)
691+
}
692+
}
693+
654694
_, err = waitForRDBInstance(ctx, rdbAPI, region, ID, d.Timeout(schema.TimeoutUpdate))
655695
if err != nil {
656696
return diag.FromErr(err)

internal/services/rdb/instance_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func TestAccInstance_Basic(t *testing.T) {
6060
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "load_balancer.0.ip"),
6161
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "load_balancer.0.endpoint_id"),
6262
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "load_balancer.0.port"),
63+
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "logs_policy.0.max_age_retention"),
64+
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "logs_policy.0.total_disk_retention"),
6365
),
6466
},
6567
{
@@ -139,6 +141,66 @@ func TestAccInstance_WithCluster(t *testing.T) {
139141
})
140142
}
141143

144+
func TestAccInstance_LogsPolicy(t *testing.T) {
145+
tt := acctest.NewTestTools(t)
146+
defer tt.Cleanup()
147+
148+
latestEngineVersion := rdbchecks.GetLatestEngineVersion(tt, postgreSQLEngineName)
149+
resource.ParallelTest(t, resource.TestCase{
150+
PreCheck: func() { acctest.PreCheck(t) },
151+
ProviderFactories: tt.ProviderFactories,
152+
CheckDestroy: rdbchecks.IsInstanceDestroyed(tt),
153+
Steps: []resource.TestStep{
154+
{
155+
Config: fmt.Sprintf(`
156+
resource scaleway_rdb_instance main {
157+
name = "test-rdb-log-policy"
158+
node_type = "db-dev-s"
159+
engine = %q
160+
is_ha_cluster = true
161+
disable_backup = false
162+
user_name = "my_initial_user"
163+
password = "thiZ_is_v&ry_s8cret"
164+
tags = [ "terraform-test", "scaleway_rdb_instance", "minimal" ]
165+
logs_policy {
166+
max_age_retention = 30
167+
total_disk_retention = 100000000
168+
}
169+
}
170+
`, latestEngineVersion),
171+
Check: resource.ComposeTestCheckFunc(
172+
isInstancePresent(tt, "scaleway_rdb_instance.main"),
173+
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.max_age_retention", "30"),
174+
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.total_disk_retention", "100000000"),
175+
),
176+
},
177+
{
178+
Config: fmt.Sprintf(`
179+
resource scaleway_rdb_instance main {
180+
name = "test-rdb-log-policy"
181+
node_type = "db-dev-s"
182+
engine = %q
183+
is_ha_cluster = true
184+
disable_backup = false
185+
user_name = "my_initial_user"
186+
password = "thiZ_is_v&ry_s8cret"
187+
tags = [ "terraform-test", "scaleway_rdb_instance", "minimal" ]
188+
logs_policy {
189+
max_age_retention = 10
190+
total_disk_retention = 200000000
191+
}
192+
}
193+
`, latestEngineVersion),
194+
Check: resource.ComposeTestCheckFunc(
195+
isInstancePresent(tt, "scaleway_rdb_instance.main"),
196+
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.max_age_retention", "10"),
197+
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.total_disk_retention", "200000000"),
198+
),
199+
},
200+
},
201+
})
202+
}
203+
142204
func TestAccInstance_Settings(t *testing.T) {
143205
tt := acctest.NewTestTools(t)
144206
defer tt.Cleanup()

0 commit comments

Comments
 (0)