Skip to content

Commit e1d39a7

Browse files
authored
Merge pull request #1837 from tencentcloudstack/feat/support-postgres-operation
support postgresql: operation resource
2 parents 7f0249c + d8e9838 commit e1d39a7

17 files changed

+1282
-430
lines changed

.changelog/1837.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_postgresql_instance: support to update `charge_type` and `period`
3+
```
4+
5+
```release-note:enhancement
6+
resource/tencentcloud_postgresql_readonly_instance: support the `read_only_group_id` field
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_postgresql_modify_account_remark_operation
11+
```
12+
13+
```release-note:new-resource
14+
tencentcloud_postgresql_modify_switch_time_period_operation
15+
```

tencentcloud/basic_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,18 @@ data "tencentcloud_postgresql_instances" "foo" {
467467
name = "` + defaultPGOperationName + `"
468468
}
469469
470+
data "tencentcloud_postgresql_readonly_groups" "ro_groups" {
471+
filters {
472+
name = "db-master-instance-id"
473+
values = [data.tencentcloud_postgresql_instances.foo.instance_list.0.id]
474+
}
475+
order_by = "CreateTime"
476+
order_by_type = "asc"
477+
}
478+
470479
locals {
471480
pgsql_id = data.tencentcloud_postgresql_instances.foo.instance_list.0.id
481+
pgrogroup_id = data.tencentcloud_postgresql_readonly_groups.ro_groups.read_only_group_list.0.read_only_group_id
472482
}
473483
`
474484
const defaultPGSQLName = "keep-postgresql"

tencentcloud/provider.go

Lines changed: 402 additions & 399 deletions
Large diffs are not rendered by default.

tencentcloud/resource_tc_postgresql_instance.go

Lines changed: 84 additions & 7 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,13 +213,12 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
211213
Type: schema.TypeString,
212214
Optional: true,
213215
Default: COMMON_PAYTYPE_POSTPAID,
214-
ForceNew: true,
215-
Description: "Pay type of the postgresql instance. Values `POSTPAID_BY_HOUR` (Default), `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`.",
216217
},
217218
"period": {
218219
Type: schema.TypeInt,
219220
Optional: true,
220-
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`.",
221222
},
222223
"auto_renew_flag": {
223224
Type: schema.TypeInt,
@@ -795,15 +796,91 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
795796
d.Partial(true)
796797

797798
if err := helper.ImmutableArgsChek(d,
798-
"charge_type",
799-
"period",
800-
"auto_renew_flag",
801-
"auto_voucher",
799+
// "charge_type",
800+
// "period",
801+
// "auto_renew_flag",
802+
// "auto_voucher",
802803
"voucher_ids",
803804
); err != nil {
804805
return err
805806
}
806807

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+
812+
if d.HasChange("charge_type") {
813+
var (
814+
chargeTypeOld string
815+
chargeTypeNew string
816+
period = 1
817+
autoRenew = 0
818+
autoVoucher = 0
819+
request = postgresql.NewModifyDBInstanceChargeTypeRequest()
820+
)
821+
822+
old, new := d.GetChange("charge_type")
823+
if old != nil {
824+
chargeTypeOld = old.(string)
825+
}
826+
if new != nil {
827+
chargeTypeNew = new.(string)
828+
}
829+
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+
}
833+
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+
}
840+
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+
}
847+
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)
860+
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())
867+
}
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
880+
}
881+
882+
}
883+
807884
var outErr, inErr, checkErr error
808885
// update vpc and subnet
809886
if d.HasChange("vpc_id") || d.HasChange("subnet_id") {

tencentcloud/resource_tc_postgresql_instance_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,44 @@ func TestAccTencentCloudPostgresqlInstanceResource_prepaid(t *testing.T) {
234234
})
235235
}
236236

237+
func TestAccTencentCloudPostgresqlInstanceResource_postpaid_to_prepaid(t *testing.T) {
238+
resource.Test(t, resource.TestCase{
239+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
240+
Providers: testAccProviders,
241+
CheckDestroy: testAccCheckPostgresqlInstanceDestroy,
242+
Steps: []resource.TestStep{
243+
{
244+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_PREPAY) },
245+
Config: testAccPostgresqlInstancePostpaid,
246+
Check: resource.ComposeTestCheckFunc(
247+
testAccCheckPostgresqlInstanceExists(testPostgresqlInstanceResourceKey),
248+
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "id"),
249+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "name", "tf_postsql_postpaid"),
250+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "charge_type", "POSTPAID_BY_HOUR"),
251+
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "period"),
252+
),
253+
},
254+
{
255+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_PREPAY) },
256+
Config: testAccPostgresqlInstancePostpaid_to_Prepaid,
257+
Check: resource.ComposeTestCheckFunc(
258+
testAccCheckPostgresqlInstanceExists(testPostgresqlInstanceResourceKey),
259+
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "id"),
260+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "name", "tf_postsql_postpaid_updated_to_prepaid"),
261+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "charge_type", "PREPAID"),
262+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "period", "2"),
263+
),
264+
},
265+
{
266+
ResourceName: testPostgresqlInstanceResourceKey,
267+
ImportState: true,
268+
ImportStateVerify: true,
269+
ImportStateVerifyIgnore: []string{"root_password", "spec_code", "public_access_switch", "charset", "backup_plan", "period"},
270+
},
271+
},
272+
})
273+
}
274+
237275
func TestAccTencentCloudPostgresqlInstanceResource_MAZ(t *testing.T) {
238276
t.Parallel()
239277
resource.Test(t, resource.TestCase{
@@ -363,6 +401,63 @@ resource "tencentcloud_postgresql_instance" "test" {
363401
}
364402
}
365403
`
404+
const testAccPostgresqlInstancePostpaid = defaultVpcSubnets + `
405+
data "tencentcloud_availability_zones_by_product" "zone" {
406+
product = "postgres"
407+
}
408+
409+
data "tencentcloud_security_groups" "internal" {
410+
name = "default"
411+
}
412+
413+
locals {
414+
sg_id = data.tencentcloud_security_groups.internal.security_groups.0.security_group_id
415+
}
416+
417+
resource "tencentcloud_postgresql_instance" "test" {
418+
name = "tf_postsql_postpaid"
419+
availability_zone = var.default_az
420+
charge_type = "POSTPAID_BY_HOUR"
421+
period = 1
422+
vpc_id = local.vpc_id
423+
subnet_id = local.subnet_id
424+
engine_version = "13.3"
425+
root_password = "t1qaA2k1wgvfa3?ZZZ"
426+
security_groups = [local.sg_id]
427+
charset = "LATIN1"
428+
project_id = 0
429+
memory = 2
430+
storage = 20
431+
}`
432+
433+
const testAccPostgresqlInstancePostpaid_to_Prepaid = defaultVpcSubnets + `
434+
data "tencentcloud_availability_zones_by_product" "zone" {
435+
product = "postgres"
436+
}
437+
438+
data "tencentcloud_security_groups" "internal" {
439+
name = "default"
440+
}
441+
442+
locals {
443+
sg_id = data.tencentcloud_security_groups.internal.security_groups.0.security_group_id
444+
}
445+
446+
resource "tencentcloud_postgresql_instance" "test" {
447+
name = "tf_postsql_postpaid_updated_to_prepaid"
448+
availability_zone = var.default_az
449+
charge_type = "PREPAID"
450+
period = 2
451+
vpc_id = local.vpc_id
452+
subnet_id = local.subnet_id
453+
engine_version = "13.3"
454+
root_password = "t1qaA2k1wgvfa3?ZZZ"
455+
security_groups = [local.sg_id]
456+
charset = "LATIN1"
457+
project_id = 0
458+
memory = 2
459+
storage = 20
460+
}`
366461

367462
const testAccPostgresqlInstancePrepaid = defaultVpcSubnets + `
368463
data "tencentcloud_availability_zones_by_product" "zone" {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Provides a resource to create a postgresql modify_account_remark_operation
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_postgresql_modify_account_remark_operation" "modify_account_remark_operation" {
8+
db_instance_id = local.pgsql_id
9+
user_name = "root"
10+
remark = "hello_world"
11+
}
12+
```
13+
*/
14+
package tencentcloud
15+
16+
import (
17+
"log"
18+
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21+
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
22+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
)
24+
25+
func resourceTencentCloudPostgresqlModifyAccountRemarkOperation() *schema.Resource {
26+
return &schema.Resource{
27+
Create: resourceTencentCloudPostgresqlModifyAccountRemarkOperationCreate,
28+
Read: resourceTencentCloudPostgresqlModifyAccountRemarkOperationRead,
29+
Delete: resourceTencentCloudPostgresqlModifyAccountRemarkOperationDelete,
30+
Schema: map[string]*schema.Schema{
31+
"db_instance_id": {
32+
Required: true,
33+
ForceNew: true,
34+
Type: schema.TypeString,
35+
Description: "Instance ID in the format of postgres-4wdeb0zv.",
36+
},
37+
38+
"user_name": {
39+
Required: true,
40+
ForceNew: true,
41+
Type: schema.TypeString,
42+
Description: "Instance username.",
43+
},
44+
45+
"remark": {
46+
Required: true,
47+
ForceNew: true,
48+
Type: schema.TypeString,
49+
Description: "New remarks corresponding to user `UserName`.",
50+
},
51+
},
52+
}
53+
}
54+
55+
func resourceTencentCloudPostgresqlModifyAccountRemarkOperationCreate(d *schema.ResourceData, meta interface{}) error {
56+
defer logElapsed("resource.tencentcloud_postgresql_modify_account_remark_operation.create")()
57+
defer inconsistentCheck(d, meta)()
58+
59+
logId := getLogId(contextNil)
60+
61+
var (
62+
request = postgresql.NewModifyAccountRemarkRequest()
63+
dBInstanceId string
64+
)
65+
if v, ok := d.GetOk("db_instance_id"); ok {
66+
request.DBInstanceId = helper.String(v.(string))
67+
dBInstanceId = v.(string)
68+
}
69+
70+
if v, ok := d.GetOk("user_name"); ok {
71+
request.UserName = helper.String(v.(string))
72+
}
73+
74+
if v, ok := d.GetOk("remark"); ok {
75+
request.Remark = helper.String(v.(string))
76+
}
77+
78+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
79+
result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().ModifyAccountRemark(request)
80+
if e != nil {
81+
return retryError(e)
82+
} else {
83+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
84+
}
85+
return nil
86+
})
87+
if err != nil {
88+
log.Printf("[CRITAL]%s operate postgresql ModifyAccountRemarkOperation failed, reason:%+v", logId, err)
89+
return err
90+
}
91+
92+
d.SetId(dBInstanceId)
93+
94+
return resourceTencentCloudPostgresqlModifyAccountRemarkOperationRead(d, meta)
95+
}
96+
97+
func resourceTencentCloudPostgresqlModifyAccountRemarkOperationRead(d *schema.ResourceData, meta interface{}) error {
98+
defer logElapsed("resource.tencentcloud_postgresql_modify_account_remark_operation.read")()
99+
defer inconsistentCheck(d, meta)()
100+
101+
return nil
102+
}
103+
104+
func resourceTencentCloudPostgresqlModifyAccountRemarkOperationDelete(d *schema.ResourceData, meta interface{}) error {
105+
defer logElapsed("resource.tencentcloud_postgresql_modify_account_remark_operation.delete")()
106+
defer inconsistentCheck(d, meta)()
107+
108+
return nil
109+
}

0 commit comments

Comments
 (0)