Skip to content

Commit fb2e487

Browse files
tongyimingmikatong
and
mikatong
authored
fix(pg): [122697475] backup_plan (#3281)
* fix backup_plan * update * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 8a11ad7 commit fb2e487

File tree

4 files changed

+220
-1
lines changed

4 files changed

+220
-1
lines changed

.changelog/3281.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_postgresql_instance: support `monthly_backup_retention_period`, `monthly_backup_period` and `monthly_plan_id`
3+
```

tencentcloud/services/postgresql/resource_tc_postgresql_instance.go

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
259259
Description: "List of backup period per week, available values: `monday`, `tuesday`, `wednesday`, `thursday`, `friday`, `saturday`, `sunday`. NOTE: At least specify two days.",
260260
Elem: &schema.Schema{Type: schema.TypeString},
261261
},
262+
"monthly_backup_retention_period": {
263+
Type: schema.TypeInt,
264+
Optional: true,
265+
Description: "Specify days of the retention.",
266+
},
267+
"monthly_backup_period": {
268+
Type: schema.TypeList,
269+
Optional: true,
270+
Description: "If it is in monthly dimension, the format is numeric characters, such as [\"1\",\"2\"].",
271+
Elem: &schema.Schema{Type: schema.TypeString},
272+
},
273+
"monthly_plan_id": {
274+
Type: schema.TypeString,
275+
Computed: true,
276+
Description: "Monthly plan id.",
277+
},
262278
},
263279
},
264280
},
@@ -685,6 +701,37 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
685701

686702
// set backup plan
687703
if plan, ok := helper.InterfacesHeadMap(d, "backup_plan"); ok {
704+
if v, ok := plan["monthly_backup_period"].([]interface{}); ok && len(v) > 0 {
705+
request0 := postgresql.NewCreateBackupPlanRequest()
706+
request0.DBInstanceId = &instanceId
707+
request0.BackupPeriodType = helper.String("month")
708+
request0.PlanName = helper.String("custom_month")
709+
request0.BackupPeriod = helper.InterfacesStringsPoint(v)
710+
711+
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
712+
request0.MinBackupStartTime = &v
713+
}
714+
715+
if v, ok := plan["max_backup_start_time"].(string); ok && v != "" {
716+
request0.MaxBackupStartTime = &v
717+
}
718+
719+
if v, ok := plan["monthly_backup_retention_period"].(int); ok && v != 0 {
720+
request0.BaseBackupRetentionPeriod = helper.IntUint64(v)
721+
}
722+
723+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
724+
_, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().CreateBackupPlan(request0)
725+
if e != nil {
726+
return tccommon.RetryError(err, postgresql.OPERATIONDENIED_INSTANCESTATUSLIMITOPERROR)
727+
}
728+
return nil
729+
})
730+
731+
if err != nil {
732+
return err
733+
}
734+
}
688735
request := postgresql.NewModifyBackupPlanRequest()
689736
request.DBInstanceId = &instanceId
690737
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
@@ -923,9 +970,19 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
923970
return err
924971
}
925972

926-
var backupPlan *postgresql.BackupPlan
973+
var backupPlan, monthlyBackupPlan *postgresql.BackupPlan
927974
if len(bkpResponse) > 0 {
928975
backupPlan = bkpResponse[0]
976+
for _, plan := range bkpResponse {
977+
if plan != nil && plan.BackupPeriodType != nil {
978+
if *plan.BackupPeriodType == "month" {
979+
monthlyBackupPlan = plan
980+
}
981+
if *plan.BackupPeriodType == "week" {
982+
backupPlan = plan
983+
}
984+
}
985+
}
929986
}
930987

931988
if backupPlan != nil {
@@ -953,6 +1010,22 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
9531010
planMap["backup_period"] = strSlice
9541011
}
9551012

1013+
if monthlyBackupPlan != nil && monthlyBackupPlan.PlanId != nil {
1014+
planMap["monthly_plan_id"] = monthlyBackupPlan.PlanId
1015+
}
1016+
if monthlyBackupPlan != nil && monthlyBackupPlan.BackupPeriod != nil {
1017+
strSlice := []string{}
1018+
err := json.Unmarshal([]byte(*monthlyBackupPlan.BackupPeriod), &strSlice)
1019+
if err != nil {
1020+
return fmt.Errorf("BackupPeriod:[%s] has invalid format,Unmarshal failed! error: %v", *backupPlan.BackupPeriod, err.Error())
1021+
}
1022+
1023+
planMap["monthly_backup_period"] = strSlice
1024+
}
1025+
if monthlyBackupPlan != nil && monthlyBackupPlan.BaseBackupRetentionPeriod != nil {
1026+
planMap["monthly_backup_retention_period"] = monthlyBackupPlan.BaseBackupRetentionPeriod
1027+
}
1028+
9561029
_ = d.Set("backup_plan", []interface{}{planMap})
9571030
}
9581031

@@ -1411,6 +1484,86 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
14111484
if err != nil {
14121485
return err
14131486
}
1487+
1488+
request1 := postgresql.NewModifyBackupPlanRequest()
1489+
request1.DBInstanceId = &instanceId
1490+
var hasMonthlybackupPeriod bool
1491+
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
1492+
request1.MinBackupStartTime = &v
1493+
}
1494+
1495+
if v, ok := plan["max_backup_start_time"].(string); ok && v != "" {
1496+
request1.MaxBackupStartTime = &v
1497+
}
1498+
1499+
if v, ok := plan["monthly_backup_retention_period"].(int); ok && v != 0 {
1500+
request1.BaseBackupRetentionPeriod = helper.IntUint64(v)
1501+
}
1502+
1503+
if v, ok := plan["monthly_backup_period"].([]interface{}); ok && len(v) > 0 {
1504+
request1.BackupPeriod = helper.InterfacesStringsPoint(v)
1505+
hasMonthlybackupPeriod = true
1506+
}
1507+
1508+
var monthlyPlanId string
1509+
if v, ok := plan["monthly_plan_id"].(string); ok && v != "" {
1510+
request1.PlanId = helper.String(v)
1511+
monthlyPlanId = v
1512+
}
1513+
if !hasMonthlybackupPeriod && monthlyPlanId != "" {
1514+
request0 := postgresql.NewDeleteBackupPlanRequest()
1515+
request0.DBInstanceId = &instanceId
1516+
request0.PlanId = &monthlyPlanId
1517+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1518+
_, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().DeleteBackupPlan(request0)
1519+
if e != nil {
1520+
return tccommon.RetryError(e)
1521+
}
1522+
return nil
1523+
})
1524+
1525+
if err != nil {
1526+
return err
1527+
}
1528+
} else if hasMonthlybackupPeriod && monthlyPlanId == "" {
1529+
request00 := postgresql.NewCreateBackupPlanRequest()
1530+
request00.DBInstanceId = &instanceId
1531+
request00.BackupPeriodType = helper.String("month")
1532+
request00.PlanName = helper.String("custom_month")
1533+
if v, ok := plan["monthly_backup_period"].([]interface{}); ok && len(v) > 0 {
1534+
request00.BackupPeriod = helper.InterfacesStringsPoint(v)
1535+
}
1536+
1537+
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
1538+
request00.MinBackupStartTime = &v
1539+
}
1540+
1541+
if v, ok := plan["max_backup_start_time"].(string); ok && v != "" {
1542+
request00.MaxBackupStartTime = &v
1543+
}
1544+
1545+
if v, ok := plan["monthly_backup_retention_period"].(int); ok && v != 0 {
1546+
request00.BaseBackupRetentionPeriod = helper.IntUint64(v)
1547+
}
1548+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1549+
_, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().CreateBackupPlan(request00)
1550+
if e != nil {
1551+
return tccommon.RetryError(e)
1552+
}
1553+
return nil
1554+
})
1555+
1556+
if err != nil {
1557+
return err
1558+
}
1559+
} else {
1560+
request1.PlanId = helper.String(monthlyPlanId)
1561+
err = postgresqlService.ModifyBackupPlan(ctx, request1)
1562+
if err != nil {
1563+
return err
1564+
}
1565+
}
1566+
14141567
}
14151568
}
14161569

tencentcloud/services/postgresql/resource_tc_postgresql_instance_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,39 @@ func TestAccTencentCloudPostgresqlInstanceResource_basic(t *testing.T) {
234234
})
235235
}
236236

237+
func TestAccTencentCloudPostgresqlInstanceResource_backupPlan(t *testing.T) {
238+
// t.Parallel()
239+
resource.Test(t, resource.TestCase{
240+
PreCheck: func() {
241+
tcacctest.AccPreCheck(t)
242+
tcacctest.AccStepSetRegion(t, "ap-guangzhou")
243+
},
244+
Providers: tcacctest.AccProviders,
245+
CheckDestroy: testAccCheckPostgresqlInstanceDestroy,
246+
Steps: []resource.TestStep{
247+
{
248+
PreConfig: func() { tcacctest.AccPreCheck(t) },
249+
Config: testAccPostgresqlInstanceBackupPlan,
250+
Check: resource.ComposeTestCheckFunc(
251+
testAccCheckPostgresqlInstanceExists(testPostgresqlInstanceResourceKey),
252+
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "id"),
253+
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "backup_plan.0.min_backup_start_time"),
254+
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "backup_plan.0.max_backup_start_time"),
255+
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "backup_plan.0.base_backup_retention_period"),
256+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "backup_plan.0.backup_period.#", "2"),
257+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "backup_plan.0.monthly_backup_period.#", "1"),
258+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "backup_plan.0.monthly_backup_retention_period", "8"),
259+
),
260+
},
261+
{
262+
ResourceName: testPostgresqlInstanceResourceKey,
263+
ImportState: true,
264+
ImportStateVerifyIgnore: []string{"root_password", "spec_code", "public_access_switch", "charset", "backup_plan"},
265+
},
266+
},
267+
})
268+
}
269+
237270
func TestAccTencentCloudPostgresqlInstanceResource_prepaid(t *testing.T) {
238271
resource.Test(t, resource.TestCase{
239272
PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
@@ -460,6 +493,34 @@ resource "tencentcloud_postgresql_instance" "test" {
460493
}
461494
}
462495
`
496+
497+
const testAccPostgresqlInstanceBackupPlan string = testAccPostgresqlInstanceBasic + tcacctest.DefaultVpcSubnets + `
498+
resource "tencentcloud_postgresql_instance" "test" {
499+
name = "tf_postsql_instance"
500+
availability_zone = data.tencentcloud_availability_zones_by_product.zone.zones[5].name
501+
charge_type = "POSTPAID_BY_HOUR"
502+
vpc_id = local.vpc_id
503+
subnet_id = local.subnet_id
504+
db_major_version = "17"
505+
engine_version = "17.0"
506+
root_password = "t1qaA2k1wgvfa3?ZZZ"
507+
security_groups = ["sg-5275dorp"]
508+
charset = "LATIN1"
509+
project_id = 0
510+
memory = 4
511+
storage = 100
512+
513+
backup_plan {
514+
min_backup_start_time = "00:10:11"
515+
max_backup_start_time = "01:10:11"
516+
base_backup_retention_period = 7
517+
backup_period = ["monday", "wednesday"]
518+
monthly_backup_period = ["1"]
519+
monthly_backup_retention_period = 8
520+
}
521+
}
522+
`
523+
463524
const testAccPostgresqlInstancePostpaid = tcacctest.DefaultVpcSubnets + `
464525
data "tencentcloud_availability_zones_by_product" "zone" {
465526
product = "postgres"

website/docs/r/postgresql_instance.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ The `backup_plan` object supports the following:
342342
* `base_backup_retention_period` - (Optional, Int) Specify days of the retention.
343343
* `max_backup_start_time` - (Optional, String) Specify latest backup start time, format `hh:mm:ss`.
344344
* `min_backup_start_time` - (Optional, String) Specify earliest backup start time, format `hh:mm:ss`.
345+
* `monthly_backup_period` - (Optional, List) If it is in monthly dimension, the format is numeric characters, such as ["1","2"].
346+
* `monthly_backup_retention_period` - (Optional, Int) Specify days of the retention.
345347

346348
The `db_node_set` object supports the following:
347349

0 commit comments

Comments
 (0)