@@ -259,6 +259,22 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
259
259
Description : "List of backup period per week, available values: `monday`, `tuesday`, `wednesday`, `thursday`, `friday`, `saturday`, `sunday`. NOTE: At least specify two days." ,
260
260
Elem : & schema.Schema {Type : schema .TypeString },
261
261
},
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
+ },
262
278
},
263
279
},
264
280
},
@@ -685,6 +701,37 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
685
701
686
702
// set backup plan
687
703
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
+ }
688
735
request := postgresql .NewModifyBackupPlanRequest ()
689
736
request .DBInstanceId = & instanceId
690
737
if v , ok := plan ["min_backup_start_time" ].(string ); ok && v != "" {
@@ -923,9 +970,19 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
923
970
return err
924
971
}
925
972
926
- var backupPlan * postgresql.BackupPlan
973
+ var backupPlan , monthlyBackupPlan * postgresql.BackupPlan
927
974
if len (bkpResponse ) > 0 {
928
975
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
+ }
929
986
}
930
987
931
988
if backupPlan != nil {
@@ -953,6 +1010,22 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
953
1010
planMap ["backup_period" ] = strSlice
954
1011
}
955
1012
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
+
956
1029
_ = d .Set ("backup_plan" , []interface {}{planMap })
957
1030
}
958
1031
@@ -1411,6 +1484,86 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
1411
1484
if err != nil {
1412
1485
return err
1413
1486
}
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
+
1414
1567
}
1415
1568
}
1416
1569
0 commit comments