Skip to content

Commit 55d275b

Browse files
committed
fix(postgresql): [117252443] support params db_major_version create resource (#2618)
* add * add * add * add * add * add * add * add * add
1 parent ec17711 commit 55d275b

File tree

5 files changed

+123
-104
lines changed

5 files changed

+123
-104
lines changed

.changelog/2618.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_postgresql_instance: support params db_major_version create resource
3+
```

tencentcloud/services/postgresql/resource_tc_postgresql_instance.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,25 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
7272
},
7373
"engine_version": {
7474
Type: schema.TypeString,
75-
ForceNew: true,
7675
Optional: true,
77-
Default: "10.4",
78-
Description: "Version of the postgresql database engine. Valid values: `10.4`, `11.8`, `12.4`.",
76+
Computed: true,
77+
Description: "Version of the postgresql database engine. Valid values: `10.4`, `10.17`, `10.23`, `11.8`, `11.12`, `11.22`, `12.4`, `12.7`, `12.18`, `13.3`, `14.2`, `14.11`, `15.1`, `16.0`.",
7978
},
8079
"db_major_vesion": {
8180
Type: schema.TypeString,
8281
Optional: true,
8382
Computed: true,
8483
Deprecated: "`db_major_vesion` will be deprecated, use `db_major_version` instead.",
8584
ConflictsWith: []string{"db_major_version"},
86-
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13. " +
85+
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13, 14, 15, 16. " +
8786
"If it is specified, an instance running the latest kernel of PostgreSQL DBMajorVersion will be created.",
8887
},
8988
"db_major_version": {
9089
Type: schema.TypeString,
9190
Optional: true,
9291
Computed: true,
9392
ConflictsWith: []string{"db_major_vesion"},
94-
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13. " +
93+
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13, 14, 15, 16. " +
9594
"If it is specified, an instance running the latest kernel of PostgreSQL DBMajorVersion will be created.",
9695
},
9796
"db_kernel_version": {
@@ -101,7 +100,6 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
101100
Description: "PostgreSQL kernel version number. " +
102101
"If it is specified, an instance running kernel DBKernelVersion will be created. It supports updating the minor kernel version immediately.",
103102
},
104-
105103
"vpc_id": {
106104
Type: schema.TypeString,
107105
Required: true,
@@ -344,9 +342,9 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
344342

345343
// the sdk asks to set value with 1 when paytype is postpaid
346344

347-
var instanceId, specVersion, specCode string
345+
var instanceId, majorVersion, specVersion, specCode string
348346
var outErr, inErr error
349-
var allowVersion, allowSpec []string
347+
var allowMajorVersion, allowSpecVersion, allowSpec []string
350348

351349
var (
352350
dbMajorVersion = ""
@@ -404,17 +402,27 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
404402
requestSecurityGroup = append(requestSecurityGroup, v.(string))
405403
}
406404

405+
if dbVersion == "" && dbMajorVersion == "" && dbKernelVersion == "" {
406+
dbVersion = "10.4"
407+
}
408+
407409
// get specCode with engine_version and memory
408410
outErr = resource.Retry(tccommon.ReadRetryTimeout*5, func() *resource.RetryError {
409411
speccodes, inErr := postgresqlService.DescribeSpecinfos(ctx, zone)
410412
if inErr != nil {
411413
return tccommon.RetryError(inErr)
412414
}
413415
for _, info := range speccodes {
414-
if !tccommon.IsContains(allowVersion, *info.Version) {
415-
allowVersion = append(allowVersion, *info.Version)
416+
if !tccommon.IsContains(allowSpecVersion, *info.Version) {
417+
allowSpecVersion = append(allowSpecVersion, *info.Version)
418+
}
419+
420+
if !tccommon.IsContains(allowMajorVersion, *info.MajorVersion) {
421+
allowMajorVersion = append(allowMajorVersion, *info.MajorVersion)
416422
}
417-
if *info.Version == dbVersion {
423+
424+
if *info.MajorVersion == dbMajorVersion || *info.Version == dbVersion {
425+
majorVersion = *info.MajorVersion
418426
specVersion = *info.Version
419427
specString := fmt.Sprintf("(%d, %d)", int(*info.Memory)/1024, int(*info.Cpu))
420428
if !tccommon.IsContains(allowSpec, specString) {
@@ -438,8 +446,8 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
438446
return outErr
439447
}
440448

441-
if specVersion == "" {
442-
return fmt.Errorf(`The "engine_version" value: "%s" is invalid, Valid values are one of: "%s"`, dbVersion, strings.Join(allowVersion, `", "`))
449+
if majorVersion == "" && specVersion == "" {
450+
return fmt.Errorf(`The "db_major_version" value: "%s" is invalid, Valid values are one of: "%s", The "engine_version" value: "%s" is invalid, Valid values are one of: "%s"`, dbMajorVersion, strings.Join(allowMajorVersion, `", "`), dbVersion, strings.Join(allowSpecVersion, `", "`))
443451
}
444452

445453
if specCode == "" {
@@ -1126,13 +1134,19 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
11261134
paramEntrys["max_standby_streaming_delay"] = strconv.Itoa(v.(int))
11271135
}
11281136
}
1137+
11291138
if d.HasChange("db_major_vesion") || d.HasChange("db_major_version") {
11301139
return fmt.Errorf("Not support change db major version.")
11311140
}
11321141

1142+
if d.HasChange("engine_version") {
1143+
return fmt.Errorf("Not support change engine_version.")
1144+
}
1145+
11331146
if d.HasChange("need_support_tde") || d.HasChange("kms_key_id") || d.HasChange("kms_region") {
11341147
return fmt.Errorf("Not support change params contact with data transparent encryption.")
11351148
}
1149+
11361150
if len(paramEntrys) != 0 {
11371151
outErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
11381152
inErr := postgresqlService.ModifyPgParams(ctx, instanceId, paramEntrys)

tencentcloud/services/postgresql/resource_tc_postgresql_instance.md

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
Use this resource to create postgresql instance.
22

33
-> **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.
4+
-> **Note:** If no values are set for the two parameters: `db_major_version` and `engine_version`, then `engine_version` is set to `10.4` by default. Suggest using parameter `db_major_version` to create an instance
45

56
Example Usage
67

78
```hcl
89
variable "availability_zone" {
9-
default = "ap-guangzhou-1"
10+
default = "ap-guangzhou-3"
1011
}
1112
1213
# create vpc
1314
resource "tencentcloud_vpc" "vpc" {
14-
name = "guagua_vpc_instance_test"
15+
name = "vpc"
1516
cidr_block = "10.0.0.0/16"
1617
}
1718
1819
# create vpc subnet
1920
resource "tencentcloud_subnet" "subnet" {
2021
availability_zone = var.availability_zone
21-
name = "guagua_vpc_subnet_test"
22+
name = "subnet"
2223
vpc_id = tencentcloud_vpc.vpc.id
2324
cidr_block = "10.0.20.0/28"
2425
is_multicast = false
2526
}
2627
2728
# create postgresql
28-
resource "tencentcloud_postgresql_instance" "foo" {
29+
resource "tencentcloud_postgresql_instance" "example" {
2930
name = "example"
3031
availability_zone = var.availability_zone
3132
charge_type = "POSTPAID_BY_HOUR"
3233
vpc_id = tencentcloud_vpc.vpc.id
3334
subnet_id = tencentcloud_subnet.subnet.id
34-
engine_version = "10.4"
35+
db_major_version = "10"
36+
engine_version = "10.23"
3537
root_user = "root123"
3638
root_password = "Root123$"
3739
charset = "UTF8"
3840
project_id = 0
41+
cpu = 1
3942
memory = 2
4043
storage = 10
4144
@@ -58,27 +61,27 @@ variable "standby_availability_zone" {
5861
5962
# create vpc
6063
resource "tencentcloud_vpc" "vpc" {
61-
name = "guagua_vpc_instance_test"
64+
name = "vpc"
6265
cidr_block = "10.0.0.0/16"
6366
}
6467
6568
# create vpc subnet
6669
resource "tencentcloud_subnet" "subnet" {
6770
availability_zone = var.availability_zone
68-
name = "guagua_vpc_subnet_test"
71+
name = "subnet"
6972
vpc_id = tencentcloud_vpc.vpc.id
7073
cidr_block = "10.0.20.0/28"
7174
is_multicast = false
7275
}
7376
7477
# create postgresql
75-
resource "tencentcloud_postgresql_instance" "foo" {
78+
resource "tencentcloud_postgresql_instance" "example" {
7679
name = "example"
7780
availability_zone = var.availability_zone
7881
charge_type = "POSTPAID_BY_HOUR"
7982
vpc_id = tencentcloud_vpc.vpc.id
8083
subnet_id = tencentcloud_subnet.subnet.id
81-
engine_version = "10.4"
84+
db_major_version = "10"
8285
root_user = "root123"
8386
root_password = "Root123$"
8487
charset = "UTF8"
@@ -91,6 +94,7 @@ resource "tencentcloud_postgresql_instance" "foo" {
9194
role = "Primary"
9295
zone = var.availability_zone
9396
}
97+
9498
db_node_set {
9599
zone = var.standby_availability_zone
96100
}
@@ -102,20 +106,24 @@ resource "tencentcloud_postgresql_instance" "foo" {
102106
```
103107

104108
create pgsql with kms key
105-
```
106-
resource "tencentcloud_postgresql_instance" "pg" {
109+
```hcl
110+
variable "availability_zone" {
111+
default = "ap-guangzhou-6"
112+
}
113+
114+
resource "tencentcloud_postgresql_instance" "example" {
107115
name = "tf_postsql_instance"
108-
availability_zone = "ap-guangzhou-6"
116+
availability_zone = var.availability_zone
109117
charge_type = "POSTPAID_BY_HOUR"
110118
vpc_id = "vpc-86v957zb"
111119
subnet_id = "subnet-enm92y0m"
120+
db_major_version = "11"
112121
engine_version = "11.12"
113-
# db_major_vesion = "11"
114122
db_kernel_version = "v11.12_r1.3"
115123
need_support_tde = 1
116124
kms_key_id = "788c606a-c7b7-11ec-82d1-5254001e5c4e"
117125
kms_region = "ap-guangzhou"
118-
root_password = "xxxxxxxxxx"
126+
root_password = "Root123$"
119127
charset = "LATIN1"
120128
project_id = 0
121129
memory = 4
@@ -135,32 +143,37 @@ resource "tencentcloud_postgresql_instance" "pg" {
135143
```
136144

137145
upgrade kernel version
138-
```
139-
resource "tencentcloud_postgresql_instance" "test" {
140-
name = "tf_postsql_instance_update"
141-
availability_zone = data.tencentcloud_availability_zones_by_product.zone.zones[5].name
142-
charge_type = "POSTPAID_BY_HOUR"
143-
vpc_id = local.vpc_id
144-
subnet_id = local.subnet_id
145-
engine_version = "13.3"
146-
root_password = "*"
147-
charset = "LATIN1"
148-
project_id = 0
146+
```hcl
147+
variable "availability_zone" {
148+
default = "ap-guangzhou-6"
149+
}
150+
151+
resource "tencentcloud_postgresql_instance" "example" {
152+
name = "tf_postsql_instance_update_kernel"
153+
availability_zone = var.availability_zone
154+
charge_type = "POSTPAID_BY_HOUR"
155+
vpc_id = "vpc-86v957zb"
156+
subnet_id = "subnet-enm92y0m"
157+
engine_version = "13.3"
158+
root_password = "Root123$"
159+
charset = "LATIN1"
160+
project_id = 0
149161
public_access_switch = false
150-
security_groups = [local.sg_id]
151-
memory = 4
152-
storage = 250
162+
security_groups = ["sg-cm7fbbf3"]
163+
memory = 4
164+
storage = 250
165+
153166
backup_plan {
154-
min_backup_start_time = "01:10:11"
155-
max_backup_start_time = "02:10:11"
156-
base_backup_retention_period = 5
157-
backup_period = ["monday", "thursday", "sunday"]
167+
min_backup_start_time = "01:10:11"
168+
max_backup_start_time = "02:10:11"
169+
base_backup_retention_period = 5
170+
backup_period = ["monday", "thursday", "sunday"]
158171
}
159172
160173
db_kernel_version = "v13.3_r1.4" # eg:from v13.3_r1.1 to v13.3_r1.4
161174
162175
tags = {
163-
tf = "teest"
176+
tf = "test"
164177
}
165178
}
166179
```
@@ -170,5 +183,5 @@ Import
170183
postgresql instance can be imported using the id, e.g.
171184

172185
```
173-
$ terraform import tencentcloud_postgresql_instance.foo postgres-cda1iex1
186+
$ terraform import tencentcloud_postgresql_instance.example postgres-cda1iex1
174187
```

0 commit comments

Comments
 (0)