Skip to content

fix(postgresql): [122013764]Support window period switching #3167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/3167.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/tencentcloud_postgresql_instance: Support window period switching.
```

```release-note:enhancement
resource/tencentcloud_postgresql_readonly_instance: Support window period switching.
```
90 changes: 52 additions & 38 deletions tencentcloud/services/postgresql/resource_tc_postgresql_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
Default: false,
Description: "Whether to enable instance deletion protection. Default: false.",
},
"wait_switch": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY, POSTGRESQL_KERNEL_UPGRADE_MAINTAIN_WINDOW}),
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.",
},
// Computed values
"public_access_host": {
Type: schema.TypeString,
Expand Down Expand Up @@ -1008,6 +1014,11 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
return err
}

waitSwitch := POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY
if v, ok := d.GetOk("wait_switch"); ok {
waitSwitch = v.(int)
}

if d.HasChange("period") && !d.HasChange("charge_type") {
return fmt.Errorf("The `period` field can be changed only when updating the charge type from `POSTPAID_BY_HOUR` to `PREPAID`.")
}
Expand Down Expand Up @@ -1213,7 +1224,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
}

outErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu)
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu, waitSwitch)
if inErr != nil {
return tccommon.RetryError(inErr)
}
Expand All @@ -1225,25 +1236,27 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
return outErr
}

// Wait for status to processing
_ = resource.Retry(time.Second*10, func() *resource.RetryError {
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, instanceId)
if err != nil {
return tccommon.RetryError(err)
}
if waitSwitch == POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY {
// Wait for status to processing
_ = resource.Retry(time.Second*10, func() *resource.RetryError {
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, instanceId)
if err != nil {
return tccommon.RetryError(err)
}

if *instance.DBInstanceStatus == POSTGRESQL_STAUTS_RUNNING {
return resource.RetryableError(fmt.Errorf("waiting for upgrade status change"))
}
if *instance.DBInstanceStatus == POSTGRESQL_STAUTS_RUNNING {
return resource.RetryableError(fmt.Errorf("waiting for upgrade status change"))
}

return nil
})
return nil
})

time.Sleep(time.Second * 5)
// check update storage and memory done
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId, 60)
if checkErr != nil {
return checkErr
time.Sleep(time.Second * 5)
// check update storage and memory done
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId, 60)
if checkErr != nil {
return checkErr
}
}
}

Expand Down Expand Up @@ -1368,7 +1381,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
nodeSet := d.Get("db_node_set").(*schema.Set).List()
request := postgresql.NewModifyDBInstanceDeploymentRequest()
request.DBInstanceId = helper.String(d.Id())
request.SwitchTag = helper.Int64(0)
request.SwitchTag = helper.IntInt64(waitSwitch)
for i := range nodeSet {
var (
node = nodeSet[i].(map[string]interface{})
Expand Down Expand Up @@ -1402,21 +1415,23 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
return err
}

err = resource.Retry(tccommon.ReadRetryTimeout*10, func() *resource.RetryError {
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, d.Id())
if err != nil {
return tccommon.RetryError(err)
}
if waitSwitch == POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY {
err = resource.Retry(tccommon.ReadRetryTimeout*10, func() *resource.RetryError {
instance, _, err := postgresqlService.DescribePostgresqlInstanceById(ctx, d.Id())
if err != nil {
return tccommon.RetryError(err)
}

if tccommon.IsContains(POSTGRESQL_RETRYABLE_STATUS, *instance.DBInstanceStatus) {
return resource.RetryableError(fmt.Errorf("instance status is %s, retrying", *instance.DBInstanceStatus))
}
if tccommon.IsContains(POSTGRESQL_RETRYABLE_STATUS, *instance.DBInstanceStatus) {
return resource.RetryableError(fmt.Errorf("instance status is %s, retrying", *instance.DBInstanceStatus))
}

return nil
})
return nil
})

if err != nil {
return err
if err != nil {
return err
}
}
}

Expand All @@ -1431,10 +1446,7 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
// upgradeResponse:= postgresql.NewUpgradeDBInstanceKernelVersionResponse()
upgradeRequest.DBInstanceId = &instanceId
upgradeRequest.TargetDBKernelVersion = &upgradeVersion

// only support for the immediate upgrade policy
switchTag := POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY
upgradeRequest.SwitchTag = helper.IntUint64(switchTag)
upgradeRequest.SwitchTag = helper.IntUint64(waitSwitch)

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

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

if _, e := conf.WaitForState(); e != nil {
return e
if _, e := conf.WaitForState(); e != nil {
return e
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ func ResourceTencentCloudPostgresqlReadonlyInstance() *schema.Resource {
Optional: true,
Description: "Dedicated cluster ID.",
},
"wait_switch": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY, POSTGRESQL_KERNEL_UPGRADE_MAINTAIN_WINDOW}),
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.",
},
// Computed values
"create_time": {
Type: schema.TypeString,
Expand Down Expand Up @@ -453,6 +459,11 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData
return err
}

waitSwitch := POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY
if v, ok := d.GetOk("wait_switch"); ok {
waitSwitch = v.(int)
}

if d.HasChange("read_only_group_id") {
var (
masterInstanceId string
Expand Down Expand Up @@ -526,7 +537,7 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData
cpu = v.(int)
}
outErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu)
inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu, waitSwitch)
if inErr != nil {
return tccommon.RetryError(inErr)
}
Expand All @@ -535,13 +546,15 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData
if outErr != nil {
return outErr
}
time.Sleep(time.Second * 5)
// check update storage and memory done
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId)
if checkErr != nil {
return checkErr
}

if waitSwitch == POSTGRESQL_KERNEL_UPGRADE_IMMEDIATELY {
time.Sleep(time.Second * 5)
// check update storage and memory done
checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId)
if checkErr != nil {
return checkErr
}
}
}

// update project id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ func (me *PostgresqlService) ModifyPostgresqlInstanceName(ctx context.Context, i
return err
}

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

ratelimit.Check(request.GetAction())
_, err := me.client.UsePostgresqlClient().ModifyDBInstanceSpec(request)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/postgresql_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ The following arguments are supported:
* `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.
* `tags` - (Optional, Map) The available tags within this postgresql.
* `voucher_ids` - (Optional, List: [`String`]) Specify Voucher Ids if `auto_voucher` was `1`, only support using 1 vouchers for now.
* `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.

The `backup_plan` object supports the following:

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/postgresql_readonly_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ The following arguments are supported:
* `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`.
* `read_only_group_id` - (Optional, String) RO group ID.
* `voucher_ids` - (Optional, List: [`String`]) Specify Voucher Ids if `auto_voucher` was `1`, only support using 1 vouchers for now.
* `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.

## Attributes Reference

Expand Down
Loading