Skip to content

Commit fa0eff2

Browse files
committed
adjust update logic for charge_type and period
1 parent fd92bb2 commit fa0eff2

8 files changed

+87
-83
lines changed

tencentcloud/resource_tc_postgresql_instance.go

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
Use this resource to create postgresql instance.
33
4+
-> **Note:** To update the charge type, please update the `charge_type` and specify the `period` for the charging period. It only supports updating from `POSTPAID_BY_HOUR` to `PREPAID`, and the `period` field only valid in that upgrading case.
5+
46
Example Usage
57
68
```hcl
@@ -211,12 +213,12 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
211213
Type: schema.TypeString,
212214
Optional: true,
213215
Default: COMMON_PAYTYPE_POSTPAID,
214-
Description: "Pay type of the postgresql instance. Values `POSTPAID_BY_HOUR` (Default), `PREPAID`. It support to update the type from `POSTPAID_BY_HOUR` to `PREPAID`.",
216+
Description: "Pay type of the postgresql instance. Values `POSTPAID_BY_HOUR` (Default), `PREPAID`. It only support to update the type from `POSTPAID_BY_HOUR` to `PREPAID`.",
215217
},
216218
"period": {
217219
Type: schema.TypeInt,
218220
Optional: true,
219-
Description: "Specify Prepaid period in month. Default `1`. Values: `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `24`, `36`.",
221+
Description: "Specify Prepaid period in month. Default `1`. Values: `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`, `24`, `36`. This field is valid only when creating a `PREPAID` type instance, or updating the charge type from `POSTPAID_BY_HOUR` to `PREPAID`.",
220222
},
221223
"auto_renew_flag": {
222224
Type: schema.TypeInt,
@@ -803,6 +805,10 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
803805
return err
804806
}
805807

808+
if d.HasChange("period") && !d.HasChange("charge_type") {
809+
return fmt.Errorf("The `period` field can be changed only when updating the charge type from `POSTPAID_BY_HOUR` to `PREPAID`.")
810+
}
811+
806812
if d.HasChange("charge_type") {
807813
var (
808814
chargeTypeOld string
@@ -821,56 +827,58 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
821827
chargeTypeNew = new.(string)
822828
}
823829

824-
// The real Update operation rather than the operation from create
825-
if chargeTypeOld != "" && chargeTypeOld != chargeTypeNew {
826-
if v, ok := d.GetOk("period"); ok {
827-
log.Printf("period set")
828-
period = v.(int)
829-
} else {
830-
log.Printf("period not set")
831-
}
830+
if chargeTypeOld != "POSTPAID_BY_HOUR" || chargeTypeNew != "PREPAID" {
831+
return fmt.Errorf("It only support to update the charge type from `POSTPAID_BY_HOUR` to `PREPAID`.")
832+
}
832833

833-
if v, ok := d.GetOk("auto_renew_flag"); ok {
834-
log.Printf("auto_renew_flag set")
835-
autoRenew = v.(int)
836-
} else {
837-
log.Printf("auto_renew_flag not set")
838-
}
834+
if v, ok := d.GetOk("period"); ok {
835+
log.Printf("period set")
836+
period = v.(int)
837+
} else {
838+
log.Printf("period not set")
839+
}
839840

840-
if v, ok := d.GetOk("auto_voucher"); ok {
841-
log.Printf("auto_voucher set")
842-
autoVoucher = v.(int)
843-
} else {
844-
log.Printf("auto_voucher not set")
845-
}
841+
if v, ok := d.GetOk("auto_renew_flag"); ok {
842+
log.Printf("auto_renew_flag set")
843+
autoRenew = v.(int)
844+
} else {
845+
log.Printf("auto_renew_flag not set")
846+
}
846847

847-
request.DBInstanceId = &instanceId
848-
request.InstanceChargeType = &chargeTypeNew
849-
request.Period = helper.IntInt64(period)
850-
request.AutoRenewFlag = helper.IntInt64(autoRenew)
851-
request.AutoVoucher = helper.IntInt64(autoVoucher)
852-
853-
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
854-
result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().ModifyDBInstanceChargeType(request)
855-
if e != nil {
856-
return retryError(e)
857-
} else {
858-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
859-
}
860-
return nil
861-
})
862-
if err != nil {
863-
log.Printf("[CRITAL]%s operate postgresql ModifyDbInstanceChargeType failed, reason:%+v", logId, err)
864-
return err
865-
}
848+
if v, ok := d.GetOk("auto_voucher"); ok {
849+
log.Printf("auto_voucher set")
850+
autoVoucher = v.(int)
851+
} else {
852+
log.Printf("auto_voucher not set")
853+
}
854+
855+
request.DBInstanceId = &instanceId
856+
request.InstanceChargeType = &chargeTypeNew
857+
request.Period = helper.IntInt64(period)
858+
request.AutoRenewFlag = helper.IntInt64(autoRenew)
859+
request.AutoVoucher = helper.IntInt64(autoVoucher)
866860

867-
// wait unit charge type changing operation of instance done
868-
service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn}
869-
conf := BuildStateChangeConf([]string{}, []string{"running"}, 2*readRetryTimeout, time.Second, service.PostgresqlDBInstanceStateRefreshFunc(instanceId, []string{}))
870-
if _, e := conf.WaitForState(); e != nil {
871-
return e
861+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
862+
result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().ModifyDBInstanceChargeType(request)
863+
if e != nil {
864+
return retryError(e)
865+
} else {
866+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
872867
}
868+
return nil
869+
})
870+
if err != nil {
871+
log.Printf("[CRITAL]%s operate postgresql ModifyDbInstanceChargeType failed, reason:%+v", logId, err)
872+
return err
873+
}
874+
875+
// wait unit charge type changing operation of instance done
876+
service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn}
877+
conf := BuildStateChangeConf([]string{}, []string{"running"}, 2*readRetryTimeout, time.Second, service.PostgresqlDBInstanceStateRefreshFunc(instanceId, []string{}))
878+
if _, e := conf.WaitForState(); e != nil {
879+
return e
873880
}
881+
874882
}
875883

876884
var outErr, inErr, checkErr error

tencentcloud/resource_tc_postgresql_modify_account_remark_operation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Example Usage
55
66
```hcl
77
resource "tencentcloud_postgresql_modify_account_remark_operation" "modify_account_remark_operation" {
8-
db_instance_id = ""
9-
user_name = ""
10-
remark = ""
8+
db_instance_id = local.pgsql_id
9+
user_name = "root"
10+
remark = "hello_world"
1111
}
1212
```
1313
*/

tencentcloud/resource_tc_postgresql_modify_switch_time_period_operation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Example Usage
55
66
```hcl
77
resource "tencentcloud_postgresql_modify_switch_time_period_operation" "modify_switch_time_period_operation" {
8-
db_instance_id = ""
9-
switch_tag =
8+
db_instance_id = local.pgsql_id
9+
switch_tag = 0
1010
}
1111
```
1212
*/

tencentcloud/resource_tc_postgresql_readonly_instance.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -467,30 +467,27 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData
467467
roGroupIdNew = new.(string)
468468
}
469469

470-
// The real Update operation rather than the operation from create
471-
if roGroupIdOld != "" && roGroupIdOld != roGroupIdNew {
472-
request.DBInstanceId = &instanceId
473-
request.ReadOnlyGroupId = &roGroupIdOld
474-
request.NewReadOnlyGroupId = &roGroupIdNew
475-
476-
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
477-
result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().ModifyDBInstanceReadOnlyGroup(request)
478-
if e != nil {
479-
return retryError(e)
480-
} else {
481-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
482-
}
483-
return nil
484-
})
485-
if err != nil {
486-
log.Printf("[CRITAL]%s operate postgresql ChangeDbInstanceReadOnlyGroupOperation failed, reason:%+v", logId, err)
487-
return err
470+
request.DBInstanceId = &instanceId
471+
request.ReadOnlyGroupId = &roGroupIdOld
472+
request.NewReadOnlyGroupId = &roGroupIdNew
473+
474+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
475+
result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().ModifyDBInstanceReadOnlyGroup(request)
476+
if e != nil {
477+
return retryError(e)
478+
} else {
479+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
488480
}
481+
return nil
482+
})
483+
if err != nil {
484+
log.Printf("[CRITAL]%s operate postgresql ChangeDbInstanceReadOnlyGroupOperation failed, reason:%+v", logId, err)
485+
return err
486+
}
489487

490-
conf := BuildStateChangeConf([]string{}, []string{"ok"}, 2*readRetryTimeout, time.Second, service.PostgresqlReadonlyGroupStateRefreshFunc(masterInstanceId, roGroupIdNew, []string{}))
491-
if _, e := conf.WaitForState(); e != nil {
492-
return e
493-
}
488+
conf := BuildStateChangeConf([]string{}, []string{"ok"}, 2*readRetryTimeout, time.Second, service.PostgresqlReadonlyGroupStateRefreshFunc(masterInstanceId, roGroupIdNew, []string{}))
489+
if _, e := conf.WaitForState(); e != nil {
490+
return e
494491
}
495492
}
496493

website/docs/r/postgresql_instance.html.markdown

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ description: |-
1111

1212
Use this resource to create postgresql instance.
1313

14+
-> **Note:** To update the charge type, please update the `charge_type` and specify the `period` for the charging period. It only supports updating from `POSTPAID_BY_HOUR` to `PREPAID`, and the `period` field only valid in that upgrading case.
15+
1416
## Example Usage
1517

1618
```hcl
@@ -189,7 +191,7 @@ The following arguments are supported:
189191
* `auto_renew_flag` - (Optional, Int) Auto renew flag, `1` for enabled. NOTES: Only support prepaid instance.
190192
* `auto_voucher` - (Optional, Int) Whether to use voucher, `1` for enabled.
191193
* `backup_plan` - (Optional, List) Specify DB backup plan.
192-
* `charge_type` - (Optional, String) Pay type of the postgresql instance. Values `POSTPAID_BY_HOUR` (Default), `PREPAID`. It support to update the type from `POSTPAID_BY_HOUR` to `PREPAID`.
194+
* `charge_type` - (Optional, String) Pay type of the postgresql instance. Values `POSTPAID_BY_HOUR` (Default), `PREPAID`. It only support to update the type from `POSTPAID_BY_HOUR` to `PREPAID`.
193195
* `charset` - (Optional, String, ForceNew) Charset of the root account. Valid values are `UTF8`,`LATIN1`.
194196
* `db_kernel_version` - (Optional, String) PostgreSQL kernel version number. If it is specified, an instance running kernel DBKernelVersion will be created. It supports updating the minor kernel version immediately.
195197
* `db_major_version` - (Optional, String) PostgreSQL major version number. Valid values: 10, 11, 12, 13. If it is specified, an instance running the latest kernel of PostgreSQL DBMajorVersion will be created.
@@ -201,7 +203,7 @@ The following arguments are supported:
201203
* `max_standby_archive_delay` - (Optional, Int) max_standby_archive_delay applies when WAL data is being read from WAL archive (and is therefore not current). Units are milliseconds if not specified.
202204
* `max_standby_streaming_delay` - (Optional, Int) max_standby_streaming_delay applies when WAL data is being received via streaming replication. Units are milliseconds if not specified.
203205
* `need_support_tde` - (Optional, Int) Whether to support data transparent encryption, 1: yes, 0: no (default).
204-
* `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`.
206+
* `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`. This field is valid only when creating a `PREPAID` type instance, or updating the charge type from `POSTPAID_BY_HOUR` to `PREPAID`.
205207
* `project_id` - (Optional, Int) Project id, default value is `0`.
206208
* `public_access_switch` - (Optional, Bool) Indicates whether to enable the access to an instance from public network or not.
207209
* `root_user` - (Optional, String, ForceNew) Instance root account name. This parameter is optional, Default value is `root`.

website/docs/r/postgresql_modify_account_remark_operation.html.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ Provides a resource to create a postgresql modify_account_remark_operation
1515

1616
```hcl
1717
resource "tencentcloud_postgresql_modify_account_remark_operation" "modify_account_remark_operation" {
18-
db_instance_id = ""
19-
user_name = ""
20-
remark = ""
18+
db_instance_id = local.pgsql_id
19+
user_name = "root"
20+
remark = "hello_world"
2121
}
2222
```
2323

website/docs/r/postgresql_modify_switch_time_period_operation.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Provides a resource to create a postgresql modify_switch_time_period_operation
1515

1616
```hcl
1717
resource "tencentcloud_postgresql_modify_switch_time_period_operation" "modify_switch_time_period_operation" {
18-
db_instance_id = ""
19-
switch_tag =
18+
db_instance_id = local.pgsql_id
19+
switch_tag = 0
2020
}
2121
```
2222

website/tencentcloud.erb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,9 +1834,6 @@
18341834
<li>
18351835
<a href="/docs/providers/tencentcloud/d/sqlserver_backup_upload_size.html">tencentcloud_sqlserver_backup_upload_size</a>
18361836
</li>
1837-
<li>
1838-
<a href="/docs/providers/tencentcloud/d/sqlserver_backups.html">tencentcloud_sqlserver_backups</a>
1839-
</li>
18401837
<li>
18411838
<a href="/docs/providers/tencentcloud/d/sqlserver_basic_instances.html">tencentcloud_sqlserver_basic_instances</a>
18421839
</li>

0 commit comments

Comments
 (0)