Skip to content

feat(rdb): add log policy in instance #2536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 18, 2024
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
56 changes: 48 additions & 8 deletions internal/services/rdb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,30 @@ func ResourceInstance() *schema.Resource {
},
},
},
"logs_policy": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: "Logs policy configuration",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Computed
"max_age_retention": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The max age (in days) of remote logs to keep on the Database Instance",
},
"total_disk_retention": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The max disk size of remote logs to keep on the Database Instance.",
},
},
},
},
// Common
"region": regional.Schema(),
"organization_id": account.OrganizationIDSchema(),
Expand Down Expand Up @@ -349,29 +373,36 @@ func ResourceRdbInstanceCreate(ctx context.Context, d *schema.ResourceData, m in

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

mustUpdate := false
updateReq := &rdb.UpdateInstanceRequest{
Region: region,
InstanceID: res.ID,
}
// Configure Schedule Backup
// BackupScheduleFrequency and BackupScheduleRetention can only configure after instance creation
if !d.Get("disable_backup").(bool) {
updateReq := &rdb.UpdateInstanceRequest{
Region: region,
InstanceID: res.ID,
}

updateReq.BackupSameRegion = types.ExpandBoolPtr(d.Get("backup_same_region"))

updateReq.IsBackupScheduleDisabled = scw.BoolPtr(d.Get("disable_backup").(bool))
if backupScheduleFrequency, okFrequency := d.GetOk("backup_schedule_frequency"); okFrequency {
updateReq.BackupScheduleFrequency = scw.Uint32Ptr(uint32(backupScheduleFrequency.(int)))
}
if backupScheduleRetention, okRetention := d.GetOk("backup_schedule_retention"); okRetention {
updateReq.BackupScheduleRetention = scw.Uint32Ptr(uint32(backupScheduleRetention.(int)))
}
mustUpdate = true
}

policyRaw, exist := d.GetOk("logs_policy")
if exist {
updateReq.LogsPolicy = expandInstanceLogsPolicy(policyRaw)
mustUpdate = true
}

if mustUpdate {
_, err = waitForRDBInstance(ctx, rdbAPI, region, res.ID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}

_, err = rdbAPI.UpdateInstance(updateReq, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -412,7 +443,6 @@ func ResourceRdbInstanceRead(ctx context.Context, d *schema.ResourceData, m inte
}
return diag.FromErr(err)
}

_ = d.Set("name", res.Name)
_ = d.Set("node_type", res.NodeType)
_ = d.Set("engine", res.Engine)
Expand Down Expand Up @@ -476,6 +506,9 @@ func ResourceRdbInstanceRead(ctx context.Context, d *schema.ResourceData, m inte
_ = d.Set("settings", flattenInstanceSettings(res.Settings))
_ = d.Set("init_settings", flattenInstanceSettings(res.InitSettings))

// set logs policy
_ = d.Set("logs_policy", flattenInstanceLogsPolicy(res.LogsPolicy))

// set endpoints
enableIpam, err := getIPAMConfigRead(res, m)
if err != nil {
Expand Down Expand Up @@ -651,6 +684,13 @@ func ResourceRdbInstanceUpdate(ctx context.Context, d *schema.ResourceData, m in
req.Tags = types.ExpandUpdatedStringsPtr(d.Get("tags"))
}

if d.HasChange("logs_policy") {
policyRaw, exist := d.GetOk("logs_policy")
if exist {
req.LogsPolicy = expandInstanceLogsPolicy(policyRaw)
}
}

_, err = waitForRDBInstance(ctx, rdbAPI, region, ID, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.FromErr(err)
Expand Down
62 changes: 62 additions & 0 deletions internal/services/rdb/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func TestAccInstance_Basic(t *testing.T) {
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "load_balancer.0.ip"),
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "load_balancer.0.endpoint_id"),
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "load_balancer.0.port"),
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "logs_policy.0.max_age_retention"),
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.main", "logs_policy.0.total_disk_retention"),
),
},
{
Expand Down Expand Up @@ -139,6 +141,66 @@ func TestAccInstance_WithCluster(t *testing.T) {
})
}

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

latestEngineVersion := rdbchecks.GetLatestEngineVersion(tt, postgreSQLEngineName)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: rdbchecks.IsInstanceDestroyed(tt),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource scaleway_rdb_instance main {
name = "test-rdb-log-policy"
node_type = "db-dev-s"
engine = %q
is_ha_cluster = true
disable_backup = false
user_name = "my_initial_user"
password = "thiZ_is_v&ry_s8cret"
tags = [ "terraform-test", "scaleway_rdb_instance", "minimal" ]
logs_policy {
max_age_retention = 30
total_disk_retention = 100000000
}
}
`, latestEngineVersion),
Check: resource.ComposeTestCheckFunc(
isInstancePresent(tt, "scaleway_rdb_instance.main"),
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.max_age_retention", "30"),
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.total_disk_retention", "100000000"),
),
},
{
Config: fmt.Sprintf(`
resource scaleway_rdb_instance main {
name = "test-rdb-log-policy"
node_type = "db-dev-s"
engine = %q
is_ha_cluster = true
disable_backup = false
user_name = "my_initial_user"
password = "thiZ_is_v&ry_s8cret"
tags = [ "terraform-test", "scaleway_rdb_instance", "minimal" ]
logs_policy {
max_age_retention = 10
total_disk_retention = 200000000
}
}
`, latestEngineVersion),
Check: resource.ComposeTestCheckFunc(
isInstancePresent(tt, "scaleway_rdb_instance.main"),
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.max_age_retention", "10"),
resource.TestCheckResourceAttr("scaleway_rdb_instance.main", "logs_policy.0.total_disk_retention", "200000000"),
),
},
},
})
}

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