diff --git a/.changelog/2618.txt b/.changelog/2618.txt new file mode 100644 index 0000000000..eca0c0ed29 --- /dev/null +++ b/.changelog/2618.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_postgresql_instance: support params db_major_version create resource +``` diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go b/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go index 5989b42b4d..bfd212ca64 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_instance.go @@ -72,10 +72,9 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource { }, "engine_version": { Type: schema.TypeString, - ForceNew: true, Optional: true, - Default: "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, @@ -83,7 +82,7 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource { 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": { @@ -91,7 +90,7 @@ func ResourceTencentCloudPostgresqlInstance() *schema.Resource { 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": { @@ -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, @@ -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 = "" @@ -404,6 +402,10 @@ 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) @@ -411,10 +413,16 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i 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) { @@ -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 == "" { + 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 == "" { @@ -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) diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_instance.md b/tencentcloud/services/postgresql/resource_tc_postgresql_instance.md index 3bae843ab4..726faef795 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_instance.md +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_instance.md @@ -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 @@ -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" @@ -91,6 +94,7 @@ resource "tencentcloud_postgresql_instance" "foo" { role = "Primary" zone = var.availability_zone } + db_node_set { zone = var.standby_availability_zone } @@ -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 @@ -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" } } ``` @@ -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 ``` \ No newline at end of file diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_instance_test.go b/tencentcloud/services/postgresql/resource_tc_postgresql_instance_test.go index 14606d61ac..737588354d 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_instance_test.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_instance_test.go @@ -408,7 +408,7 @@ func testAccCheckPostgresqlInstanceExists(n string) resource.TestCheckFunc { } } -const testAccPostgresqlInstanceBasic = tcacctest.DefaultSecurityGroupData + ` +const testAccPostgresqlInstanceBasic = ` data "tencentcloud_availability_zones_by_product" "zone" { product = "postgres" } @@ -423,7 +423,7 @@ resource "tencentcloud_postgresql_instance" "test" { subnet_id = local.subnet_id engine_version = "13.3" root_password = "t1qaA2k1wgvfa3?ZZZ" - security_groups = [local.sg_id] + security_groups = ["sg-5275dorp"] charset = "LATIN1" project_id = 0 memory = 4 @@ -448,14 +448,6 @@ data "tencentcloud_availability_zones_by_product" "zone" { product = "postgres" } -data "tencentcloud_security_groups" "internal" { - name = "default" -} - -locals { - sg_id = data.tencentcloud_security_groups.internal.security_groups.0.security_group_id -} - resource "tencentcloud_postgresql_instance" "test" { name = "tf_postsql_postpaid" availability_zone = var.default_az @@ -465,7 +457,7 @@ resource "tencentcloud_postgresql_instance" "test" { subnet_id = local.subnet_id engine_version = "13.3" root_password = "t1qaA2k1wgvfa3?ZZZ" - security_groups = [local.sg_id] + security_groups = ["sg-5275dorp"] charset = "LATIN1" project_id = 0 memory = 2 @@ -477,14 +469,6 @@ data "tencentcloud_availability_zones_by_product" "zone" { product = "postgres" } -data "tencentcloud_security_groups" "internal" { - name = "default" -} - -locals { - sg_id = data.tencentcloud_security_groups.internal.security_groups.0.security_group_id -} - resource "tencentcloud_postgresql_instance" "test" { name = "tf_postsql_postpaid_updated_to_prepaid" availability_zone = var.default_az @@ -494,7 +478,7 @@ resource "tencentcloud_postgresql_instance" "test" { subnet_id = local.subnet_id engine_version = "13.3" root_password = "t1qaA2k1wgvfa3?ZZZ" - security_groups = [local.sg_id] + security_groups = ["sg-5275dorp"] charset = "LATIN1" project_id = 0 memory = 2 @@ -506,14 +490,6 @@ data "tencentcloud_availability_zones_by_product" "zone" { product = "postgres" } -data "tencentcloud_security_groups" "internal" { - name = "default" -} - -locals { - sg_id = data.tencentcloud_security_groups.internal.security_groups.0.security_group_id -} - resource "tencentcloud_postgresql_instance" "test" { name = "tf_postsql_pre" availability_zone = data.tencentcloud_availability_zones_by_product.zone.zones[5].name @@ -522,7 +498,7 @@ resource "tencentcloud_postgresql_instance" "test" { subnet_id = local.subnet_id engine_version = "13.3" root_password = "t1qaA2k1wgvfa3?ZZZ" - security_groups = [local.sg_id] + security_groups = ["sg-5275dorp"] charset = "LATIN1" project_id = 0 memory = 2 @@ -541,7 +517,7 @@ resource "tencentcloud_postgresql_instance" "test" { charset = "LATIN1" project_id = 0 public_access_switch = true - security_groups = [local.sg_id] + security_groups = ["sg-5275dorp"] memory = 4 storage = 250 backup_plan { @@ -591,7 +567,7 @@ resource "tencentcloud_postgresql_instance" "test" { charset = "LATIN1" project_id = 0 public_access_switch = false - security_groups = [local.sg_id] + security_groups = ["sg-5275dorp"] memory = 4 storage = 250 backup_plan { @@ -621,7 +597,7 @@ resource "tencentcloud_postgresql_instance" "test" { charset = "LATIN1" project_id = 0 public_access_switch = false - security_groups = [local.sg_id] + security_groups = ["sg-5275dorp"] memory = 4 storage = 250 backup_plan { diff --git a/website/docs/r/postgresql_instance.html.markdown b/website/docs/r/postgresql_instance.html.markdown index a9560336b6..ac51ba09a2 100644 --- a/website/docs/r/postgresql_instance.html.markdown +++ b/website/docs/r/postgresql_instance.html.markdown @@ -12,41 +12,44 @@ description: |- 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 @@ -69,27 +72,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" @@ -102,6 +105,7 @@ resource "tencentcloud_postgresql_instance" "foo" { role = "Primary" zone = var.availability_zone } + db_node_set { zone = var.standby_availability_zone } @@ -115,19 +119,23 @@ resource "tencentcloud_postgresql_instance" "foo" { ### create pgsql with kms key ```hcl -resource "tencentcloud_postgresql_instance" "pg" { +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 @@ -149,20 +157,25 @@ resource "tencentcloud_postgresql_instance" "pg" { ### upgrade kernel version ```hcl -resource "tencentcloud_postgresql_instance" "test" { - name = "tf_postsql_instance_update" - availability_zone = data.tencentcloud_availability_zones_by_product.zone.zones[5].name +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 = local.vpc_id - subnet_id = local.subnet_id + vpc_id = "vpc-86v957zb" + subnet_id = "subnet-enm92y0m" engine_version = "13.3" - root_password = "*" + root_password = "Root123$" charset = "LATIN1" project_id = 0 public_access_switch = false - security_groups = [local.sg_id] + security_groups = ["sg-cm7fbbf3"] memory = 4 storage = 250 + backup_plan { min_backup_start_time = "01:10:11" max_backup_start_time = "02:10:11" @@ -173,7 +186,7 @@ resource "tencentcloud_postgresql_instance" "test" { db_kernel_version = "v13.3_r1.4" # eg:from v13.3_r1.1 to v13.3_r1.4 tags = { - tf = "teest" + tf = "test" } } ``` @@ -196,10 +209,10 @@ The following arguments are supported: * `charset` - (Optional, String, ForceNew) Charset of the root account. Valid values are `UTF8`,`LATIN1`. * `cpu` - (Optional, Int) Number of CPU cores. Allowed value must be equal `cpu` that data source `tencentcloud_postgresql_specinfos` provides. * `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. -* `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. -* `db_major_vesion` - (Optional, String, **Deprecated**) `db_major_vesion` will be deprecated, use `db_major_version` instead. 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. +* `db_major_version` - (Optional, String) 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_vesion` - (Optional, String, **Deprecated**) `db_major_vesion` will be deprecated, use `db_major_version` instead. 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_node_set` - (Optional, Set) Specify instance node info for disaster migration. -* `engine_version` - (Optional, String, ForceNew) Version of the postgresql database engine. Valid values: `10.4`, `11.8`, `12.4`. +* `engine_version` - (Optional, String) 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`. * `kms_key_id` - (Optional, String) KeyId of the custom key. * `kms_region` - (Optional, String) Region of the custom key. * `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. @@ -243,6 +256,6 @@ In addition to all arguments above, the following attributes are exported: 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 ```