Skip to content

Commit 7d9386f

Browse files
author
mikatong
committed
fix backup_plan
1 parent a734973 commit 7d9386f

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

tencentcloud/services/postgresql/resource_tc_postgresql_instance.go

Lines changed: 141 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
},
@@ -715,6 +731,55 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
715731
if err != nil {
716732
return err
717733
}
734+
735+
if v, ok := plan["monthly_backup_period"].([]interface{}); ok && len(v) > 0 {
736+
request0 := postgresql.NewCreateBaseBackupRequest()
737+
request0.DBInstanceId = &instanceId
738+
739+
var baseBackupId *string
740+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
741+
resp, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().CreateBaseBackup(request0)
742+
if e != nil {
743+
return tccommon.RetryError(err, postgresql.OPERATIONDENIED_INSTANCESTATUSLIMITOPERROR)
744+
}
745+
baseBackupId = resp.Response.BaseBackupId
746+
return nil
747+
})
748+
749+
if err != nil {
750+
return err
751+
}
752+
753+
request1 := postgresql.NewModifyBackupPlanRequest()
754+
request1.DBInstanceId = &instanceId
755+
request1.PlanId = baseBackupId
756+
request1.BackupPeriod = helper.InterfacesStringsPoint(v)
757+
758+
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
759+
request1.MinBackupStartTime = &v
760+
}
761+
762+
if v, ok := plan["max_backup_start_time"].(string); ok && v != "" {
763+
request1.MaxBackupStartTime = &v
764+
}
765+
766+
if v, ok := plan["monthly_backup_retention_period"].(int); ok && v != 0 {
767+
request1.BaseBackupRetentionPeriod = helper.IntUint64(v)
768+
}
769+
770+
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
771+
err := postgresqlService.ModifyBackupPlan(ctx, request1)
772+
if err != nil {
773+
return tccommon.RetryError(err, postgresql.OPERATIONDENIED_INSTANCESTATUSLIMITOPERROR)
774+
}
775+
776+
return nil
777+
})
778+
779+
if err != nil {
780+
return err
781+
}
782+
}
718783
}
719784

720785
return resourceTencentCloudPostgresqlInstanceRead(d, meta)
@@ -923,9 +988,19 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
923988
return err
924989
}
925990

926-
var backupPlan *postgresql.BackupPlan
991+
var backupPlan, monthlyBackupPlan *postgresql.BackupPlan
927992
if len(bkpResponse) > 0 {
928993
backupPlan = bkpResponse[0]
994+
for _, plan := range bkpResponse {
995+
if plan != nil && plan.BackupPeriodType != nil {
996+
if *plan.BackupPeriodType == "month" {
997+
monthlyBackupPlan = plan
998+
}
999+
if *plan.BackupPeriodType == "week" {
1000+
backupPlan = plan
1001+
}
1002+
}
1003+
}
9291004
}
9301005

9311006
if backupPlan != nil {
@@ -953,6 +1028,22 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
9531028
planMap["backup_period"] = strSlice
9541029
}
9551030

1031+
if monthlyBackupPlan != nil && monthlyBackupPlan.PlanId != nil {
1032+
planMap["monthly_plan_id"] = monthlyBackupPlan.PlanId
1033+
}
1034+
if monthlyBackupPlan != nil && monthlyBackupPlan.BackupPeriod != nil {
1035+
strSlice := []string{}
1036+
err := json.Unmarshal([]byte(*monthlyBackupPlan.BackupPeriod), &strSlice)
1037+
if err != nil {
1038+
return fmt.Errorf("BackupPeriod:[%s] has invalid format,Unmarshal failed! error: %v", *backupPlan.BackupPeriod, err.Error())
1039+
}
1040+
1041+
planMap["monthly_backup_period"] = strSlice
1042+
}
1043+
if monthlyBackupPlan != nil && monthlyBackupPlan.BaseBackupRetentionPeriod != nil {
1044+
planMap["monthly_backup_retention_period"] = monthlyBackupPlan.BaseBackupRetentionPeriod
1045+
}
1046+
9561047
_ = d.Set("backup_plan", []interface{}{planMap})
9571048
}
9581049

@@ -1411,6 +1502,55 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
14111502
if err != nil {
14121503
return err
14131504
}
1505+
1506+
request1 := postgresql.NewModifyBackupPlanRequest()
1507+
request1.DBInstanceId = &instanceId
1508+
var isDeleted bool
1509+
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
1510+
request1.MinBackupStartTime = &v
1511+
}
1512+
1513+
if v, ok := plan["max_backup_start_time"].(string); ok && v != "" {
1514+
request1.MaxBackupStartTime = &v
1515+
}
1516+
1517+
if v, ok := plan["monthly_backup_retention_period"].(int); ok && v != 0 {
1518+
request1.BaseBackupRetentionPeriod = helper.IntUint64(v)
1519+
}
1520+
1521+
if v, ok := plan["monthly_backup_period"].([]interface{}); ok && len(v) > 0 {
1522+
request1.BackupPeriod = helper.InterfacesStringsPoint(v)
1523+
} else {
1524+
isDeleted = true
1525+
}
1526+
1527+
var monthlyPlanId string
1528+
if v, ok := plan["monthly_plan_id"].(string); ok {
1529+
request1.PlanId = helper.String(v)
1530+
monthlyPlanId = v
1531+
}
1532+
if isDeleted && monthlyPlanId != "" {
1533+
request0 := postgresql.NewDeleteBackupPlanRequest()
1534+
request0.DBInstanceId = &instanceId
1535+
request0.PlanId = &monthlyPlanId
1536+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1537+
_, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().DeleteBackupPlan(request0)
1538+
if e != nil {
1539+
return tccommon.RetryError(e)
1540+
}
1541+
return nil
1542+
})
1543+
1544+
if err != nil {
1545+
return err
1546+
}
1547+
} else {
1548+
err = postgresqlService.ModifyBackupPlan(ctx, request1)
1549+
if err != nil {
1550+
return err
1551+
}
1552+
}
1553+
14141554
}
14151555
}
14161556

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)