Skip to content

Commit a12bf2e

Browse files
authored
fix(postgresql): [122013764]Support window period switching (#3167)
* fix(postgresql): [122013764]Support window period switching * feat: add changelog
1 parent b2104a6 commit a12bf2e

6 files changed

+83
-46
lines changed

.changelog/3167.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_postgresql_instance: Support window period switching.
3+
```
4+
5+
```release-note:enhancement
6+
resource/tencentcloud_postgresql_readonly_instance: Support window period switching.
7+
```

tencentcloud/services/postgresql/resource_tc_postgresql_instance.go

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
293293
Default: false,
294294
Description: "Whether to enable instance deletion protection. Default: false.",
295295
},
296+
"wait_switch": {
297+
Type: schema.TypeInt,
298+
Optional: true,
299+
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY, POSTGRESQL_KERNEL_UPGRADE_MAINTAIN_WINDOW}),
300+
Description: "Switch time after instance configurations are modified. `0`: Switch immediately; `2`: Switch during maintenance time window. Default: `0`. Note: This only takes effect when updating the `memory`, `storage`, `cpu`, `db_node_set`, `db_kernel_version` fields.",
301+
},
296302
// Computed values
297303
"public_access_host": {
298304
Type: schema.TypeString,
@@ -1008,6 +1014,11 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
10081014
return err
10091015
}
10101016

1017+
waitSwitch := POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY
1018+
if v, ok := d.GetOk("wait_switch"); ok {
1019+
waitSwitch = v.(int)
1020+
}
1021+
10111022
if d.HasChange("period") && !d.HasChange("charge_type") {
10121023
return fmt.Errorf("The `period` field can be changed only when updating the charge type from `POSTPAID_BY_HOUR` to `PREPAID`.")
10131024
}
@@ -1213,7 +1224,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
12131224
}
12141225

12151226
outErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1216-
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu)
1227+
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu, waitSwitch)
12171228
if inErr != nil {
12181229
return tccommon.RetryError(inErr)
12191230
}
@@ -1225,25 +1236,27 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
12251236
return outErr
12261237
}
12271238

1228-
// Wait for status to processing
1229-
_ = resource.Retry(time.Second*10, func() *resource.RetryError {
1230-
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, instanceId)
1231-
if err != nil {
1232-
return tccommon.RetryError(err)
1233-
}
1239+
if waitSwitch == POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY {
1240+
// Wait for status to processing
1241+
_ = resource.Retry(time.Second*10, func() *resource.RetryError {
1242+
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, instanceId)
1243+
if err != nil {
1244+
return tccommon.RetryError(err)
1245+
}
12341246

1235-
if *instance.DBInstanceStatus == POSTGRESQL_STAUTS_RUNNING {
1236-
return resource.RetryableError(fmt.Errorf("waiting for upgrade status change"))
1237-
}
1247+
if *instance.DBInstanceStatus == POSTGRESQL_STAUTS_RUNNING {
1248+
return resource.RetryableError(fmt.Errorf("waiting for upgrade status change"))
1249+
}
12381250

1239-
return nil
1240-
})
1251+
return nil
1252+
})
12411253

1242-
time.Sleep(time.Second * 5)
1243-
// check update storage and memory done
1244-
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId, 60)
1245-
if checkErr != nil {
1246-
return checkErr
1254+
time.Sleep(time.Second * 5)
1255+
// check update storage and memory done
1256+
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId, 60)
1257+
if checkErr != nil {
1258+
return checkErr
1259+
}
12471260
}
12481261
}
12491262

@@ -1368,7 +1381,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
13681381
nodeSet := d.Get("db_node_set").(*schema.Set).List()
13691382
request := postgresql.NewModifyDBInstanceDeploymentRequest()
13701383
request.DBInstanceId = helper.String(d.Id())
1371-
request.SwitchTag = helper.Int64(0)
1384+
request.SwitchTag = helper.IntInt64(waitSwitch)
13721385
for i := range nodeSet {
13731386
var (
13741387
node = nodeSet[i].(map[string]interface{})
@@ -1402,21 +1415,23 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
14021415
return err
14031416
}
14041417

1405-
err = resource.Retry(tccommon.ReadRetryTimeout*10, func() *resource.RetryError {
1406-
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, d.Id())
1407-
if err != nil {
1408-
return tccommon.RetryError(err)
1409-
}
1418+
if waitSwitch == POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY {
1419+
err = resource.Retry(tccommon.ReadRetryTimeout*10, func() *resource.RetryError {
1420+
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, d.Id())
1421+
if err != nil {
1422+
return tccommon.RetryError(err)
1423+
}
14101424

1411-
if tccommon.IsContains(POSTGRESQL_RETRYABLE_STATUS, *instance.DBInstanceStatus) {
1412-
return resource.RetryableError(fmt.Errorf("instance status is %s, retrying", *instance.DBInstanceStatus))
1413-
}
1425+
if tccommon.IsContains(POSTGRESQL_RETRYABLE_STATUS, *instance.DBInstanceStatus) {
1426+
return resource.RetryableError(fmt.Errorf("instance status is %s, retrying", *instance.DBInstanceStatus))
1427+
}
14141428

1415-
return nil
1416-
})
1429+
return nil
1430+
})
14171431

1418-
if err != nil {
1419-
return err
1432+
if err != nil {
1433+
return err
1434+
}
14201435
}
14211436
}
14221437

@@ -1431,10 +1446,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
14311446
// upgradeResponse:= postgresql.NewUpgradeDBInstanceKernelVersionResponse()
14321447
upgradeRequest.DBInstanceId = &instanceId
14331448
upgradeRequest.TargetDBKernelVersion = &upgradeVersion
1434-
1435-
// only support for the immediate upgrade policy
1436-
switchTag := POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY
1437-
upgradeRequest.SwitchTag = helper.IntUint64(switchTag)
1449+
upgradeRequest.SwitchTag = helper.IntUint64(waitSwitch)
14381450

14391451
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
14401452
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().UpgradeDBInstanceKernelVersion(upgradeRequest)
@@ -1458,11 +1470,13 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
14581470
return err
14591471
}
14601472

1461-
// only wait for immediately upgrade mode
1462-
conf := tccommon.BuildStateChangeConf([]string{}, []string{"running", "isolated", "offline"}, 10*tccommon.ReadRetryTimeout, time.Second, postgresqlService.PostgresqlUpgradeKernelVersionRefreshFunc(d.Id(), []string{}))
1473+
if waitSwitch == POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY {
1474+
// only wait for immediately upgrade mode
1475+
conf := tccommon.BuildStateChangeConf([]string{}, []string{"running", "isolated", "offline"}, 10*tccommon.ReadRetryTimeout, time.Second, postgresqlService.PostgresqlUpgradeKernelVersionRefreshFunc(d.Id(), []string{}))
14631476

1464-
if _, e := conf.WaitForState(); e != nil {
1465-
return e
1477+
if _, e := conf.WaitForState(); e != nil {
1478+
return e
1479+
}
14661480
}
14671481
}
14681482

tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ func ResourceTencentCloudPostgresqlReadonlyInstance() *schema.Resource {
147147
Optional: true,
148148
Description: "Dedicated cluster ID.",
149149
},
150+
"wait_switch": {
151+
Type: schema.TypeInt,
152+
Optional: true,
153+
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY, POSTGRESQL_KERNEL_UPGRADE_MAINTAIN_WINDOW}),
154+
Description: "Switch time after instance configurations are modified. `0`: Switch immediately; `2`: Switch during maintenance time window. Default: `0`. Note: This only takes effect when updating the `memory`, `storage`, `cpu` fields.",
155+
},
150156
// Computed values
151157
"create_time": {
152158
Type: schema.TypeString,
@@ -453,6 +459,11 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData
453459
return err
454460
}
455461

462+
waitSwitch := POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY
463+
if v, ok := d.GetOk("wait_switch"); ok {
464+
waitSwitch = v.(int)
465+
}
466+
456467
if d.HasChange("read_only_group_id") {
457468
var (
458469
masterInstanceId string
@@ -526,7 +537,7 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData
526537
cpu = v.(int)
527538
}
528539
outErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
529-
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu)
540+
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu, waitSwitch)
530541
if inErr != nil {
531542
return tccommon.RetryError(inErr)
532543
}
@@ -535,13 +546,15 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData
535546
if outErr != nil {
536547
return outErr
537548
}
538-
time.Sleep(time.Second * 5)
539-
// check update storage and memory done
540-
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId)
541-
if checkErr != nil {
542-
return checkErr
543-
}
544549

550+
if waitSwitch == POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY {
551+
time.Sleep(time.Second * 5)
552+
// check update storage and memory done
553+
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId)
554+
if checkErr != nil {
555+
return checkErr
556+
}
557+
}
545558
}
546559

547560
// update project id

tencentcloud/services/postgresql/service_tencentcloud_postgresql.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ func (me *PostgresqlService) ModifyPostgresqlInstanceName(ctx context.Context, i
693693
return err
694694
}
695695

696-
func (me *PostgresqlService) UpgradePostgresqlInstance(ctx context.Context, instanceId string, memory int, storage int, cpu int) (errRet error) {
696+
func (me *PostgresqlService) UpgradePostgresqlInstance(ctx context.Context, instanceId string, memory int, storage int, cpu int, waitSwitch int) (errRet error) {
697697
logId := tccommon.GetLogId(ctx)
698698
request := postgresql.NewModifyDBInstanceSpecRequest()
699699
defer func() {
@@ -707,6 +707,7 @@ func (me *PostgresqlService) UpgradePostgresqlInstance(ctx context.Context, inst
707707
if cpu != 0 {
708708
request.Cpu = helper.IntUint64(cpu)
709709
}
710+
request.SwitchTag = helper.IntUint64(waitSwitch)
710711

711712
ratelimit.Check(request.GetAction())
712713
_, err := me.client.UsePostgresqlClient().ModifyDBInstanceSpec(request)

website/docs/r/postgresql_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ The following arguments are supported:
330330
* `security_groups` - (Optional, Set: [`String`]) ID of security group. If both vpc_id and subnet_id are not set, this argument should not be set either.
331331
* `tags` - (Optional, Map) The available tags within this postgresql.
332332
* `voucher_ids` - (Optional, List: [`String`]) Specify Voucher Ids if `auto_voucher` was `1`, only support using 1 vouchers for now.
333+
* `wait_switch` - (Optional, Int) Switch time after instance configurations are modified. `0`: Switch immediately; `2`: Switch during maintenance time window. Default: `0`. Note: This only takes effect when updating the `memory`, `storage`, `cpu`, `db_node_set`, `db_kernel_version` fields.
333334

334335
The `backup_plan` object supports the following:
335336

website/docs/r/postgresql_readonly_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ The following arguments are supported:
228228
* `period` - (Optional, Int) Specify Prepaid period in month. Default `1`. Values: `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `24`, `36`.
229229
* `read_only_group_id` - (Optional, String) RO group ID.
230230
* `voucher_ids` - (Optional, List: [`String`]) Specify Voucher Ids if `auto_voucher` was `1`, only support using 1 vouchers for now.
231+
* `wait_switch` - (Optional, Int) Switch time after instance configurations are modified. `0`: Switch immediately; `2`: Switch during maintenance time window. Default: `0`. Note: This only takes effect when updating the `memory`, `storage`, `cpu` fields.
231232

232233
## Attributes Reference
233234

0 commit comments

Comments
 (0)