diff --git a/.changelog/3167.txt b/.changelog/3167.txt new file mode 100644 index 0000000000..c0d88fd59f --- /dev/null +++ b/.changelog/3167.txt @@ -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. +``` diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go b/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go index f97a123036..c30b11b86e 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go @@ -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, @@ -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`.") } @@ -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) } @@ -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 + } } } @@ -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{}) @@ -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 + } } } @@ -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) @@ -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 + } } } diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go index 3462b10578..a7d8a28069 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go @@ -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, @@ -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 @@ -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) } @@ -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 diff --git a/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go b/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go index 7231643c11..c9e8560ce9 100644 --- a/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go +++ b/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go @@ -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() { @@ -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) diff --git a/website/docs/r/postgresql_instance.html.markdown b/website/docs/r/postgresql_instance.html.markdown index ebbf777477..80c6eade35 100644 --- a/website/docs/r/postgresql_instance.html.markdown +++ b/website/docs/r/postgresql_instance.html.markdown @@ -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: diff --git a/website/docs/r/postgresql_readonly_instance.html.markdown b/website/docs/r/postgresql_readonly_instance.html.markdown index 5c0f2210e5..40fbf0057e 100644 --- a/website/docs/r/postgresql_readonly_instance.html.markdown +++ b/website/docs/r/postgresql_readonly_instance.html.markdown @@ -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