Skip to content

fix(postgresql): [117252443] support params db_major_version create resource #2618

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 9 commits into from
Jun 5, 2024
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
3 changes: 3 additions & 0 deletions .changelog/2618.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_postgresql_instance: support params db_major_version create resource
```
40 changes: 27 additions & 13 deletions tencentcloud/services/postgresql/resource_tc_postgresql_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,25 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
},
"engine_version": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: "10.4",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里去掉 default 会有不兼容变更问题吗

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果用户engine_version和db_major_version都不填写 会在代码中手动设置engine_version:10.4

Description: "Version of the postgresql database engine. Valid values: `10.4`, `11.8`, `12.4`.",
Computed: true,
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`.",
},
"db_major_vesion": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Deprecated: "`db_major_vesion` will be deprecated, use `db_major_version` instead.",
ConflictsWith: []string{"db_major_version"},
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13. " +
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13, 14, 15, 16. " +
"If it is specified, an instance running the latest kernel of PostgreSQL DBMajorVersion will be created.",
},
"db_major_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"db_major_vesion"},
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13. " +
Description: "PostgreSQL major version number. Valid values: 10, 11, 12, 13, 14, 15, 16. " +
"If it is specified, an instance running the latest kernel of PostgreSQL DBMajorVersion will be created.",
},
"db_kernel_version": {
Expand All @@ -101,7 +100,6 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource {
Description: "PostgreSQL kernel version number. " +
"If it is specified, an instance running kernel DBKernelVersion will be created. It supports updating the minor kernel version immediately.",
},

"vpc_id": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -344,9 +342,9 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i

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

var instanceId, specVersion, specCode string
var instanceId, majorVersion, specVersion, specCode string
var outErr, inErr error
var allowVersion, allowSpec []string
var allowMajorVersion, allowSpecVersion, allowSpec []string

var (
dbMajorVersion = ""
Expand Down Expand Up @@ -404,17 +402,27 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
requestSecurityGroup = append(requestSecurityGroup, v.(string))
}

if dbVersion == "" && dbMajorVersion == "" && dbKernelVersion == "" {
dbVersion = "10.4"
}

// get specCode with engine_version and memory
outErr = resource.Retry(tccommon.ReadRetryTimeout*5, func() *resource.RetryError {
speccodes, inErr := postgresqlService.DescribeSpecinfos(ctx, zone)
if inErr != nil {
return tccommon.RetryError(inErr)
}
for _, info := range speccodes {
if !tccommon.IsContains(allowVersion, *info.Version) {
allowVersion = append(allowVersion, *info.Version)
if !tccommon.IsContains(allowSpecVersion, *info.Version) {
allowSpecVersion = append(allowSpecVersion, *info.Version)
}

if !tccommon.IsContains(allowMajorVersion, *info.MajorVersion) {
allowMajorVersion = append(allowMajorVersion, *info.MajorVersion)
}
if *info.Version == dbVersion {

if *info.MajorVersion == dbMajorVersion || *info.Version == dbVersion {
majorVersion = *info.MajorVersion
specVersion = *info.Version
specString := fmt.Sprintf("(%d, %d)", int(*info.Memory)/1024, int(*info.Cpu))
if !tccommon.IsContains(allowSpec, specString) {
Expand All @@ -438,8 +446,8 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
return outErr
}

if specVersion == "" {
return fmt.Errorf(`The "engine_version" value: "%s" is invalid, Valid values are one of: "%s"`, dbVersion, strings.Join(allowVersion, `", "`))
if majorVersion == "" && specVersion == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

majorVersion 和 specVersion 参数必须传一个是吗

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必须传一个或者都传入

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

majorVersion和specVersion参数主要对应的是客户传入的engine_version和db_major_version

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, `", "`))
}

if specCode == "" {
Expand Down Expand Up @@ -1126,13 +1134,19 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
paramEntrys["max_standby_streaming_delay"] = strconv.Itoa(v.(int))
}
}

if d.HasChange("db_major_vesion") || d.HasChange("db_major_version") {
return fmt.Errorf("Not support change db major version.")
}

if d.HasChange("engine_version") {
return fmt.Errorf("Not support change engine_version.")
}

if d.HasChange("need_support_tde") || d.HasChange("kms_key_id") || d.HasChange("kms_region") {
return fmt.Errorf("Not support change params contact with data transparent encryption.")
}

if len(paramEntrys) != 0 {
outErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
inErr := postgresqlService.ModifyPgParams(ctx, instanceId, paramEntrys)
Expand Down
81 changes: 47 additions & 34 deletions tencentcloud/services/postgresql/resource_tc_postgresql_instance.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
Use this resource to create postgresql instance.

-> **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.
-> **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

Example Usage

```hcl
variable "availability_zone" {
default = "ap-guangzhou-1"
default = "ap-guangzhou-3"
}

# create vpc
resource "tencentcloud_vpc" "vpc" {
name = "guagua_vpc_instance_test"
name = "vpc"
cidr_block = "10.0.0.0/16"
}

# create vpc subnet
resource "tencentcloud_subnet" "subnet" {
availability_zone = var.availability_zone
name = "guagua_vpc_subnet_test"
name = "subnet"
vpc_id = tencentcloud_vpc.vpc.id
cidr_block = "10.0.20.0/28"
is_multicast = false
}

# create postgresql
resource "tencentcloud_postgresql_instance" "foo" {
resource "tencentcloud_postgresql_instance" "example" {
name = "example"
availability_zone = var.availability_zone
charge_type = "POSTPAID_BY_HOUR"
vpc_id = tencentcloud_vpc.vpc.id
subnet_id = tencentcloud_subnet.subnet.id
engine_version = "10.4"
db_major_version = "10"
engine_version = "10.23"
root_user = "root123"
root_password = "Root123$"
charset = "UTF8"
project_id = 0
cpu = 1
memory = 2
storage = 10

Expand All @@ -58,27 +61,27 @@ variable "standby_availability_zone" {

# create vpc
resource "tencentcloud_vpc" "vpc" {
name = "guagua_vpc_instance_test"
name = "vpc"
cidr_block = "10.0.0.0/16"
}

# create vpc subnet
resource "tencentcloud_subnet" "subnet" {
availability_zone = var.availability_zone
name = "guagua_vpc_subnet_test"
name = "subnet"
vpc_id = tencentcloud_vpc.vpc.id
cidr_block = "10.0.20.0/28"
is_multicast = false
}

# create postgresql
resource "tencentcloud_postgresql_instance" "foo" {
resource "tencentcloud_postgresql_instance" "example" {
name = "example"
availability_zone = var.availability_zone
charge_type = "POSTPAID_BY_HOUR"
vpc_id = tencentcloud_vpc.vpc.id
subnet_id = tencentcloud_subnet.subnet.id
engine_version = "10.4"
db_major_version = "10"
root_user = "root123"
root_password = "Root123$"
charset = "UTF8"
Expand All @@ -91,6 +94,7 @@ resource "tencentcloud_postgresql_instance" "foo" {
role = "Primary"
zone = var.availability_zone
}

db_node_set {
zone = var.standby_availability_zone
}
Expand All @@ -102,20 +106,24 @@ resource "tencentcloud_postgresql_instance" "foo" {
```

create pgsql with kms key
```
resource "tencentcloud_postgresql_instance" "pg" {
```hcl
variable "availability_zone" {
default = "ap-guangzhou-6"
}

resource "tencentcloud_postgresql_instance" "example" {
name = "tf_postsql_instance"
availability_zone = "ap-guangzhou-6"
availability_zone = var.availability_zone
charge_type = "POSTPAID_BY_HOUR"
vpc_id = "vpc-86v957zb"
subnet_id = "subnet-enm92y0m"
db_major_version = "11"
engine_version = "11.12"
# db_major_vesion = "11"
db_kernel_version = "v11.12_r1.3"
need_support_tde = 1
kms_key_id = "788c606a-c7b7-11ec-82d1-5254001e5c4e"
kms_region = "ap-guangzhou"
root_password = "xxxxxxxxxx"
root_password = "Root123$"
charset = "LATIN1"
project_id = 0
memory = 4
Expand All @@ -135,32 +143,37 @@ resource "tencentcloud_postgresql_instance" "pg" {
```

upgrade kernel version
```
resource "tencentcloud_postgresql_instance" "test" {
name = "tf_postsql_instance_update"
availability_zone = data.tencentcloud_availability_zones_by_product.zone.zones[5].name
charge_type = "POSTPAID_BY_HOUR"
vpc_id = local.vpc_id
subnet_id = local.subnet_id
engine_version = "13.3"
root_password = "*"
charset = "LATIN1"
project_id = 0
```hcl
variable "availability_zone" {
default = "ap-guangzhou-6"
}

resource "tencentcloud_postgresql_instance" "example" {
name = "tf_postsql_instance_update_kernel"
availability_zone = var.availability_zone
charge_type = "POSTPAID_BY_HOUR"
vpc_id = "vpc-86v957zb"
subnet_id = "subnet-enm92y0m"
engine_version = "13.3"
root_password = "Root123$"
charset = "LATIN1"
project_id = 0
public_access_switch = false
security_groups = [local.sg_id]
memory = 4
storage = 250
security_groups = ["sg-cm7fbbf3"]
memory = 4
storage = 250

backup_plan {
min_backup_start_time = "01:10:11"
max_backup_start_time = "02:10:11"
base_backup_retention_period = 5
backup_period = ["monday", "thursday", "sunday"]
min_backup_start_time = "01:10:11"
max_backup_start_time = "02:10:11"
base_backup_retention_period = 5
backup_period = ["monday", "thursday", "sunday"]
}

db_kernel_version = "v13.3_r1.4" # eg:from v13.3_r1.1 to v13.3_r1.4

tags = {
tf = "teest"
tf = "test"
}
}
```
Expand All @@ -170,5 +183,5 @@ Import
postgresql instance can be imported using the id, e.g.

```
$ terraform import tencentcloud_postgresql_instance.foo postgres-cda1iex1
$ terraform import tencentcloud_postgresql_instance.example postgres-cda1iex1
```
Loading
Loading