@@ -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
0 commit comments