@@ -701,6 +701,37 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
701
701
702
702
// set backup plan
703
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
+ }
704
735
request := postgresql .NewModifyBackupPlanRequest ()
705
736
request .DBInstanceId = & instanceId
706
737
if v , ok := plan ["min_backup_start_time" ].(string ); ok && v != "" {
@@ -731,55 +762,6 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
731
762
if err != nil {
732
763
return err
733
764
}
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
- }
783
765
}
784
766
785
767
return resourceTencentCloudPostgresqlInstanceRead (d , meta )
@@ -1505,7 +1487,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
1505
1487
1506
1488
request1 := postgresql .NewModifyBackupPlanRequest ()
1507
1489
request1 .DBInstanceId = & instanceId
1508
- var isDeleted bool
1490
+ var hasMonthlybackupPeriod bool
1509
1491
if v , ok := plan ["min_backup_start_time" ].(string ); ok && v != "" {
1510
1492
request1 .MinBackupStartTime = & v
1511
1493
}
@@ -1520,16 +1502,15 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
1520
1502
1521
1503
if v , ok := plan ["monthly_backup_period" ].([]interface {}); ok && len (v ) > 0 {
1522
1504
request1 .BackupPeriod = helper .InterfacesStringsPoint (v )
1523
- } else {
1524
- isDeleted = true
1505
+ hasMonthlybackupPeriod = true
1525
1506
}
1526
1507
1527
1508
var monthlyPlanId string
1528
- if v , ok := plan ["monthly_plan_id" ].(string ); ok {
1509
+ if v , ok := plan ["monthly_plan_id" ].(string ); ok && v != "" {
1529
1510
request1 .PlanId = helper .String (v )
1530
1511
monthlyPlanId = v
1531
1512
}
1532
- if isDeleted && monthlyPlanId != "" {
1513
+ if ! hasMonthlybackupPeriod && monthlyPlanId != "" {
1533
1514
request0 := postgresql .NewDeleteBackupPlanRequest ()
1534
1515
request0 .DBInstanceId = & instanceId
1535
1516
request0 .PlanId = & monthlyPlanId
@@ -1544,22 +1525,39 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
1544
1525
if err != nil {
1545
1526
return err
1546
1527
}
1547
- } else {
1548
- if monthlyPlanId == "" {
1549
- request00 := postgresql .NewCreateBaseBackupRequest ()
1550
- request00 .DBInstanceId = & instanceId
1551
- err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
1552
- _ , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlClient ().CreateBaseBackup (request00 )
1553
- if e != nil {
1554
- return tccommon .RetryError (e )
1555
- }
1556
- return nil
1557
- })
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
+ }
1558
1544
1559
- if err != nil {
1560
- return err
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 )
1561
1552
}
1553
+ return nil
1554
+ })
1555
+
1556
+ if err != nil {
1557
+ return err
1562
1558
}
1559
+ } else {
1560
+ request1 .PlanId = helper .String (monthlyPlanId )
1563
1561
err = postgresqlService .ModifyBackupPlan (ctx , request1 )
1564
1562
if err != nil {
1565
1563
return err
0 commit comments