Skip to content

Commit b8bc40b

Browse files
author
mikatong
committed
update
1 parent 7c14482 commit b8bc40b

File tree

3 files changed

+129
-67
lines changed

3 files changed

+129
-67
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: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,37 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
701701

702702
// set backup plan
703703
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+
}
704735
request := postgresql.NewModifyBackupPlanRequest()
705736
request.DBInstanceId = &instanceId
706737
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
@@ -731,55 +762,6 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
731762
if err != nil {
732763
return err
733764
}
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-
}
783765
}
784766

785767
return resourceTencentCloudPostgresqlInstanceRead(d, meta)
@@ -1505,7 +1487,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
15051487

15061488
request1 := postgresql.NewModifyBackupPlanRequest()
15071489
request1.DBInstanceId = &instanceId
1508-
var isDeleted bool
1490+
var hasMonthlybackupPeriod bool
15091491
if v, ok := plan["min_backup_start_time"].(string); ok && v != "" {
15101492
request1.MinBackupStartTime = &v
15111493
}
@@ -1520,16 +1502,15 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
15201502

15211503
if v, ok := plan["monthly_backup_period"].([]interface{}); ok && len(v) > 0 {
15221504
request1.BackupPeriod = helper.InterfacesStringsPoint(v)
1523-
} else {
1524-
isDeleted = true
1505+
hasMonthlybackupPeriod = true
15251506
}
15261507

15271508
var monthlyPlanId string
1528-
if v, ok := plan["monthly_plan_id"].(string); ok {
1509+
if v, ok := plan["monthly_plan_id"].(string); ok && v != "" {
15291510
request1.PlanId = helper.String(v)
15301511
monthlyPlanId = v
15311512
}
1532-
if isDeleted && monthlyPlanId != "" {
1513+
if !hasMonthlybackupPeriod && monthlyPlanId != "" {
15331514
request0 := postgresql.NewDeleteBackupPlanRequest()
15341515
request0.DBInstanceId = &instanceId
15351516
request0.PlanId = &monthlyPlanId
@@ -1544,22 +1525,39 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
15441525
if err != nil {
15451526
return err
15461527
}
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+
}
15581544

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)
15611552
}
1553+
return nil
1554+
})
1555+
1556+
if err != nil {
1557+
return err
15621558
}
1559+
} else {
1560+
request1.PlanId = helper.String(monthlyPlanId)
15631561
err = postgresqlService.ModifyBackupPlan(ctx, request1)
15641562
if err != nil {
15651563
return err

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"

0 commit comments

Comments
 (0)