From 878f1611501ad4aa6120d277df93c76c589c30bc Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 25 Jun 2024 17:38:37 +0800 Subject: [PATCH 1/5] add --- ...esource_tc_postgresql_readonly_instance.go | 168 ++++++---- ...esource_tc_postgresql_readonly_instance.md | 18 +- ...ce_tc_postgresql_readonly_instance_test.go | 308 ++++++++---------- ...postgresql_readonly_instance.html.markdown | 7 +- 4 files changed, 260 insertions(+), 241 deletions(-) diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go index ea7197d8c2..4b831aa122 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go @@ -7,10 +7,10 @@ import ( "strings" "time" - tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" - svccrs "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/crs" + svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag" postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -132,11 +132,11 @@ func ResourceTencentCloudPostgresqlReadonlyInstance() *schema.Resource { ForceNew: true, Description: "Whether to support IPv6 address access. Valid values: 1 (yes), 0 (no).", }, - //"tag_list": { - // Type: schema.TypeMap, - // Optional: true, - // Description: "The information of tags to be associated with instances. This parameter is left empty by default..", - //}, + "tags": { + Type: schema.TypeMap, + Optional: true, + Description: "The information of tags to be associated with instances. This parameter is left empty by default.", + }, "read_only_group_id": { Type: schema.TypeString, Optional: true, @@ -165,10 +165,9 @@ func ResourceTencentCloudPostgresqlReadonlyInstance() *schema.Resource { func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_postgresql_readonly_instance.create")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) request = postgresql.NewCreateReadOnlyDBInstanceRequest() response *postgresql.CreateReadOnlyDBInstanceResponse postgresqlService = PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} @@ -182,55 +181,72 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData dbVersion = v.(string) request.DBVersion = helper.String(dbVersion) } + if v, ok := d.GetOk("storage"); ok { request.Storage = helper.IntUint64(v.(int)) } + if v, ok := d.GetOk("memory"); ok { memory = v.(int) } + if v, ok := d.GetOkExists("cpu"); ok { cpu = v.(int) } + if v, ok := d.GetOk("master_db_instance_id"); ok { request.MasterDBInstanceId = helper.String(v.(string)) } + if v, ok := d.GetOk("zone"); ok { zone = v.(string) request.Zone = helper.String(zone) } + if v, ok := d.GetOk("project_id"); ok { request.ProjectId = helper.IntUint64(v.(int)) } + if v, ok := d.GetOk("instance_charge_type"); ok { request.InstanceChargeType = helper.String(v.(string)) } + if v, ok := d.GetOk("auto_renew_flag"); ok { request.AutoRenewFlag = helper.IntInt64(v.(int)) } + if v, ok := d.Get("period").(int); ok && v > 0 { request.Period = helper.IntUint64(v) } + if v, ok := d.Get("auto_voucher").(int); ok && v > 0 { request.AutoVoucher = helper.IntUint64(v) } + if v, ok := d.GetOk("voucher_ids"); ok { request.VoucherIds = helper.InterfacesStringsPoint(v.([]interface{})) } + if v, ok := d.GetOk("vpc_id"); ok { request.VpcId = helper.String(v.(string)) } + if v, ok := d.GetOk("subnet_id"); ok { request.SubnetId = helper.String(v.(string)) } + if v, ok := d.GetOk("name"); ok { request.Name = helper.String(v.(string)) } + if v, ok := d.GetOk("need_support_ipv6"); ok { request.NeedSupportIpv6 = helper.IntUint64(v.(int)) } + if v, ok := d.GetOk("read_only_group_id"); ok { request.ReadOnlyGroupId = helper.String(v.(string)) } + if v, ok := d.GetOk("security_groups_ids"); ok { securityGroupsIds := v.(*schema.Set).List() request.SecurityGroupIds = make([]*string, 0, len(securityGroupsIds)) @@ -238,14 +254,6 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData request.SecurityGroupIds = append(request.SecurityGroupIds, helper.String(item.(string))) } } - //if tags := helper.GetTags(d, "tag_list"); len(tags) > 0 { - // for k, v := range tags { - // request.TagList = &postgresql.Tag{ - // TagKey: &k, - // TagValue: &v, - // } - // } - //} // get specCode with db_version and memory var allowVersion, allowSpec []string @@ -280,21 +288,23 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData } return nil }) + if err != nil { return err } + if specVersion == "" { return fmt.Errorf(`The "db_version" value: "%s" is invalid, Valid values are one of: "%s"`, dbVersion, strings.Join(allowVersion, `", "`)) } + if specCode == "" { return fmt.Errorf(`The "memory" value: %d or the "cpu" value: %d is invalid, Valid combine values are one of: %s .`, memory, cpu, strings.Join(allowSpec, `; `)) } - request.SpecCode = helper.String(specCode) + request.SpecCode = helper.String(specCode) request.InstanceCount = helper.IntUint64(1) request.Period = helper.IntUint64(1) - err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlClient().CreateReadOnlyDBInstance(request) if e != nil { @@ -303,18 +313,20 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) } + response = result return nil - }) + if err != nil { return err } + instanceId := *response.Response.DBInstanceIdSet[0] d.SetId(instanceId) // check creation done - err = resource.Retry(5*tccommon.ReadRetryTimeout, func() *resource.RetryError { + err = resource.Retry(10*tccommon.ReadRetryTimeout, func() *resource.RetryError { instance, has, err := postgresqlService.DescribePostgresqlInstanceById(ctx, instanceId) if err != nil { return tccommon.RetryError(err) @@ -331,21 +343,35 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData return err } + // create tag + client := meta.(tccommon.ProviderMeta).GetAPIV3Conn() + tagService := svctag.NewTagService(client) + region := client.Region + if tags := helper.GetTags(d, "tags"); len(tags) > 0 { + resourceName := tccommon.BuildTagResourceName("postgres", "DBInstanceId", region, instanceId) + if err = tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil { + return err + } + } + return resourceTencentCloudPostgresqlReadOnlyInstanceRead(d, meta) } func resourceTencentCloudPostgresqlReadOnlyInstanceRead(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_postgresql_readonly_instance.read")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + postgresqlService = PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + instanceId = d.Id() + ) - instanceId := d.Id() - postgresqlService := PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} instance, has, err := postgresqlService.DescribePostgresqlInstanceById(ctx, instanceId) if err != nil { return err } + if !has { d.SetId("") return nil @@ -375,21 +401,24 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceRead(d *schema.ResourceData, // _ = d.Set("read_only_group_id", readonlyGroup.Id) // security groups - // Only redis service support modify Generic DB instance security groups - redisService := svccrs.NewRedisService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) - sg, err := redisService.DescribeDBSecurityGroups(ctx, "postgres", d.Id()) + sg, err := postgresqlService.DescribeDBInstanceSecurityGroupsById(ctx, d.Id()) if err != nil { return err } + if len(sg) > 0 { _ = d.Set("security_groups_ids", sg) } - //tags := make(map[string]string, len(instance.TagList)) - //for _, tag := range instance.TagList { - // tags[*tag.TagKey] = *tag.TagValue - //} - //_ = d.Set("tag_list", tags) + // get tag + client := meta.(tccommon.ProviderMeta).GetAPIV3Conn() + tagService := svctag.NewTagService(client) + region := client.Region + tags, err := tagService.DescribeResourceTags(ctx, "postgres", "DBInstanceId", region, d.Id()) + if err != nil { + return err + } + _ = d.Set("tags", tags) // computed _ = d.Set("create_time", instance.CreateTime) @@ -410,11 +439,13 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceRead(d *schema.ResourceData, func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_postgresql_readonly_instance.update")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + postgresqlService = PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + instanceId = d.Id() + ) - postgresqlService := PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} - instanceId := d.Id() d.Partial(true) if err := helper.ImmutableArgsChek(d, @@ -456,8 +487,10 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData } else { log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) } + return nil }) + if err != nil { log.Printf("[CRITAL]%s operate postgresql ChangeDbInstanceReadOnlyGroupOperation failed, reason:%+v", logId, err) return err @@ -480,15 +513,16 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData } return nil }) + if outErr != nil { return outErr } + // check update name done checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId) if checkErr != nil { return checkErr } - } // upgrade storage and memory size @@ -499,23 +533,26 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData if v, ok := d.GetOkExists("cpu"); ok { cpu = v.(int) } + outErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { inErr = postgresqlService.UpgradePostgresqlInstance(ctx, instanceId, memory, storage, cpu) if inErr != nil { return tccommon.RetryError(inErr) } + return nil }) + if outErr != nil { return outErr } + time.Sleep(time.Second * 5) // check update storage and memory done checkErr = postgresqlService.CheckDBInstanceStatus(ctx, instanceId) if checkErr != nil { return checkErr } - } // update project id @@ -526,8 +563,10 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData if inErr != nil { return tccommon.RetryError(inErr) } + return nil }) + if outErr != nil { return outErr } @@ -537,39 +576,33 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData if checkErr != nil { return checkErr } - } if d.HasChange("security_groups_ids") { - - // Only redis service support modify Generic DB instance security groups - service := svccrs.NewRedisService(meta.(tccommon.ProviderMeta).GetAPIV3Conn()) ids := d.Get("security_groups_ids").(*schema.Set).List() var sgIds []*string for _, id := range ids { sgIds = append(sgIds, helper.String(id.(string))) } - err := service.ModifyDBInstanceSecurityGroups(ctx, "postgres", d.Id(), sgIds) + + err := postgresqlService.ModifyDBInstanceSecurityGroupsById(ctx, d.Id(), sgIds) if err != nil { return err } - } - //if d.HasChange("tags") { - // - // oldValue, newValue := d.GetChange("tags") - // replaceTags, deleteTags := diffTags(oldValue.(map[string]interface{}), newValue.(map[string]interface{})) - // - // tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn() - // tagService := svctag.NewTagService(tcClient) - // resourceName := tccommon.BuildTagResourceName("postgres", "DBInstanceId", tcClient.Region, d.Id()) - // err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags) - // if err != nil { - // return err - // } - // - //} + if d.HasChange("tags") { + oldValue, newValue := d.GetChange("tags") + replaceTags, deleteTags := svctag.DiffTags(oldValue.(map[string]interface{}), newValue.(map[string]interface{})) + client := meta.(tccommon.ProviderMeta).GetAPIV3Conn() + tagService := svctag.NewTagService(client) + region := client.Region + resourceName := tccommon.BuildTagResourceName("postgres", "DBInstanceId", region, d.Id()) + err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags) + if err != nil { + return err + } + } d.Partial(false) @@ -579,11 +612,12 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData func resourceTencentCLoudPostgresqlReadOnlyInstanceDelete(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_postgresql_readonly_instance.delete")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - - instanceId := d.Id() - postgresqlService := PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + postgresqlService = PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + instanceId = d.Id() + ) // isolate err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { @@ -591,8 +625,10 @@ func resourceTencentCLoudPostgresqlReadOnlyInstanceDelete(d *schema.ResourceData if e != nil { return tccommon.RetryError(e) } + return nil }) + if err != nil { return err } @@ -617,8 +653,10 @@ func resourceTencentCLoudPostgresqlReadOnlyInstanceDelete(d *schema.ResourceData if e != nil { return tccommon.RetryError(e) } + return nil }) + if err != nil { return err } diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.md b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.md index 304b7fdbab..3e39bb9566 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.md +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.md @@ -29,14 +29,14 @@ resource "tencentcloud_postgresql_instance" "example" { charge_type = "POSTPAID_BY_HOUR" vpc_id = tencentcloud_vpc.vpc.id subnet_id = tencentcloud_subnet.subnet.id - engine_version = "10.4" + engine_version = "10.23" root_user = "root123" root_password = "Root123$" charset = "UTF8" project_id = 0 - memory = 2 - cpu = 1 - storage = 10 + memory = 4 + cpu = 2 + storage = 200 tags = { test = "tf" @@ -73,11 +73,11 @@ resource "tencentcloud_postgresql_readonly_instance" "example" { zone = var.availability_zone name = "example" auto_renew_flag = 0 - db_version = "10.4" + db_version = "10.23" instance_charge_type = "POSTPAID_BY_HOUR" memory = 4 cpu = 2 - storage = 250 + storage = 200 vpc_id = tencentcloud_vpc.vpc.id subnet_id = tencentcloud_subnet.subnet.id need_support_ipv6 = 0 @@ -85,6 +85,10 @@ resource "tencentcloud_postgresql_readonly_instance" "example" { security_groups_ids = [ tencentcloud_security_group.example.id, ] + + tags = { + createdBy : "terraform" + } } ``` @@ -93,5 +97,5 @@ Import postgresql readonly instance can be imported using the id, e.g. ``` -$ terraform import tencentcloud_postgresql_readonly_instance.example instance_id +$ terraform import tencentcloud_postgresql_readonly_instance.example pgro-jtb4v6oo ``` \ No newline at end of file diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go index a23deddded..29fe5b6401 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go @@ -8,8 +8,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -var testPostgresqlReadonlyInstanceResourceKey = "tencentcloud_postgresql_readonly_instance.instance" +var testPostgresqlReadonlyInstanceResourceKey = "tencentcloud_postgresql_readonly_instance.example" +// go test -i; go test -test.run TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic -v func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { // t.Parallel() resource.Test(t, resource.TestCase{ @@ -22,15 +23,15 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { { PreConfig: func() { tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_COMMON) + tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) }, Config: testAccPostgresqlReadonlyInstanceInstance_basic_without_rogroup, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "master_db_instance_id"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "name", "tf_ro_instance_test"), + resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "name", "example"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "instance_charge_type", "POSTPAID_BY_HOUR"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "db_version", "15.1"), + resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "db_version", "10.23"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "project_id", "0"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "vpc_id"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "subnet_id"), @@ -38,7 +39,7 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "security_groups_ids.#", "1"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "memory", "4"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "cpu", "2"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "storage", "20"), + resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "storage", "200"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_ip"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_port"), ), @@ -46,15 +47,15 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { { PreConfig: func() { tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_COMMON) + tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) }, Config: testAccPostgresqlReadonlyInstanceInstance_basic_update_rogroup, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "master_db_instance_id"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "name", "tf_ro_instance_test_updated"), + resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "name", "example"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "instance_charge_type", "POSTPAID_BY_HOUR"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "db_version", "15.1"), + resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "db_version", "10.23"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "project_id", "0"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "vpc_id"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "subnet_id"), @@ -62,7 +63,7 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "security_groups_ids.#", "1"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "memory", "4"), resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "cpu", "2"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "storage", "20"), + resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "storage", "200"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_ip"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_port"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "read_only_group_id"), @@ -72,175 +73,146 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { }) } -func TestAccTencentCloudPostgresqlReadonlyInstanceResource_update_ro_group(t *testing.T) { - // t.Parallel() - resource.Test(t, resource.TestCase{ - PreCheck: func() { - tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheck(t) - }, - Providers: tcacctest.AccProviders, - Steps: []resource.TestStep{ - { - PreConfig: func() { - tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_COMMON) - }, - Config: testAccPostgresqlReadonlyInstanceInstance_with_rogroup, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "master_db_instance_id"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "name", "tf_ro_instance_test_rog"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "instance_charge_type", "POSTPAID_BY_HOUR"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "db_version", "15.1"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "project_id", "0"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "vpc_id"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "subnet_id"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "zone"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "security_groups_ids.#", "1"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "memory", "4"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "storage", "20"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_ip"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_port"), - ), - }, - { - PreConfig: func() { - tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_COMMON) - }, - Config: testAccPostgresqlReadonlyInstanceInstance_update_rogroup, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "master_db_instance_id"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "name", "tf_ro_instance_test_rog_updated"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "instance_charge_type", "POSTPAID_BY_HOUR"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "db_version", "15.1"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "project_id", "0"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "vpc_id"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "subnet_id"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "zone"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "security_groups_ids.#", "1"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "memory", "4"), - resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "storage", "20"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_ip"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_port"), - resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "read_only_group_id"), - ), - }, - }, - }) +const testAccPostgresqlReadonlyInstanceInstance_basic_without_rogroup string = ` +variable "availability_zone" { + default = "ap-guangzhou-3" } -const testAccPostgresqlReadonlyInstanceInstance_basic_without_rogroup string = tcacctest.OperationPresetPGSQL + tcacctest.DefaultVpcSubnets + tcacctest.DefaultSecurityGroupData + ` - resource "tencentcloud_postgresql_readonly_instance" "instance" { - auto_renew_flag = 0 - db_version = "15.1" - instance_charge_type = "POSTPAID_BY_HOUR" - master_db_instance_id = local.pgsql_id - memory = 4 - cpu = 2 - name = "tf_ro_instance_test" - need_support_ipv6 = 0 - project_id = 0 - security_groups_ids = [ - local.sg_id, - ] - storage = 20 - vpc_id = local.vpc_id - subnet_id = local.subnet_id - zone = var.default_az - } -` +# create vpc +resource "tencentcloud_vpc" "vpc" { + name = "vpc" + cidr_block = "10.0.0.0/16" +} -const testAccPostgresqlReadonlyInstanceInstance_basic_update_rogroup string = tcacctest.OperationPresetPGSQL + tcacctest.DefaultVpcSubnets + tcacctest.DefaultSecurityGroupData + ` - resource "tencentcloud_postgresql_readonly_instance" "instance" { - auto_renew_flag = 0 - db_version = "15.1" - instance_charge_type = "POSTPAID_BY_HOUR" - master_db_instance_id = local.pgsql_id - memory = 4 - cpu = 2 - name = "tf_ro_instance_test_updated" - need_support_ipv6 = 0 - project_id = 0 - security_groups_ids = [ - local.sg_id, - ] - storage = 20 - vpc_id = local.vpc_id - subnet_id = local.subnet_id - zone = var.default_az - read_only_group_id = tencentcloud_postgresql_readonly_group.new_ro_group.id - } +# create vpc subnet +resource "tencentcloud_subnet" "subnet" { + availability_zone = var.availability_zone + name = "subnet" + vpc_id = tencentcloud_vpc.vpc.id + cidr_block = "10.0.20.0/28" + is_multicast = false +} - resource "tencentcloud_postgresql_readonly_group" "new_ro_group" { - master_db_instance_id = local.pgsql_id - name = "tf_ro_group_test_new" - project_id = 0 - vpc_id = local.vpc_id - subnet_id = local.subnet_id - replay_lag_eliminate = 1 - replay_latency_eliminate = 1 - max_replay_lag = 100 - max_replay_latency = 512 - min_delay_eliminate_reserve = 1 +# create postgresql +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.23" + root_user = "root123" + root_password = "Root123$" + charset = "UTF8" + project_id = 0 + memory = 2 + cpu = 1 + storage = 10 + + tags = { + test = "tf" } -` +} -const testAccPostgresqlReadonlyInstanceInstance_with_rogroup string = tcacctest.OperationPresetPGSQL + tcacctest.DefaultVpcSubnets + tcacctest.DefaultSecurityGroupData + ` - resource "tencentcloud_postgresql_readonly_instance" "instance" { - auto_renew_flag = 0 - db_version = "15.1" - instance_charge_type = "POSTPAID_BY_HOUR" - master_db_instance_id = local.pgsql_id - memory = 4 - cpu = 2 - name = "tf_ro_instance_test_rog" - need_support_ipv6 = 0 - project_id = 0 - security_groups_ids = [ - local.sg_id, - ] - storage = 20 - vpc_id = local.vpc_id - subnet_id = local.subnet_id - zone = var.default_az - read_only_group_id = local.pgrogroup_id +resource "tencentcloud_postgresql_readonly_instance" "example" { + master_db_instance_id = tencentcloud_postgresql_instance.example.id + zone = var.availability_zone + name = "example" + auto_renew_flag = 0 + db_version = "10.23" + instance_charge_type = "POSTPAID_BY_HOUR" + memory = 4 + cpu = 2 + storage = 200 + vpc_id = tencentcloud_vpc.vpc.id + subnet_id = tencentcloud_subnet.subnet.id + need_support_ipv6 = 0 + project_id = 0 + security_groups_ids = [ + "sg-5275dorp", + ] + + tags = { + createdBy : "terraform" } +} ` -const testAccPostgresqlReadonlyInstanceInstance_update_rogroup string = tcacctest.OperationPresetPGSQL + tcacctest.DefaultVpcSubnets + tcacctest.DefaultSecurityGroupData + ` - resource "tencentcloud_postgresql_readonly_instance" "instance" { - auto_renew_flag = 0 - db_version = "15.1" - instance_charge_type = "POSTPAID_BY_HOUR" - master_db_instance_id = local.pgsql_id - memory = 4 - cpu = 2 - name = "tf_ro_instance_test_rog_updated" - need_support_ipv6 = 0 - project_id = 0 - security_groups_ids = [ - local.sg_id, - ] - storage = 20 - vpc_id = local.vpc_id - subnet_id = local.subnet_id - zone = var.default_az - read_only_group_id = tencentcloud_postgresql_readonly_group.new_ro_group.id +const testAccPostgresqlReadonlyInstanceInstance_basic_update_rogroup string = ` +variable "availability_zone" { + default = "ap-guangzhou-3" +} + +# create vpc +resource "tencentcloud_vpc" "vpc" { + name = "vpc" + cidr_block = "10.0.0.0/16" +} + +# create vpc subnet +resource "tencentcloud_subnet" "subnet" { + availability_zone = var.availability_zone + name = "subnet" + vpc_id = tencentcloud_vpc.vpc.id + cidr_block = "10.0.20.0/28" + is_multicast = false +} + +# create postgresql +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.23" + root_user = "root123" + root_password = "Root123$" + charset = "UTF8" + project_id = 0 + memory = 2 + cpu = 1 + storage = 10 + + tags = { + test = "tf" } +} - resource "tencentcloud_postgresql_readonly_group" "new_ro_group" { - master_db_instance_id = local.pgsql_id - name = "tf_ro_group_test_new" - project_id = 0 - vpc_id = local.vpc_id - subnet_id = local.subnet_id - replay_lag_eliminate = 1 - replay_latency_eliminate = 1 - max_replay_lag = 100 - max_replay_latency = 512 - min_delay_eliminate_reserve = 1 +resource "tencentcloud_postgresql_readonly_group" "example" { + master_db_instance_id = tencentcloud_postgresql_instance.example.id + name = "tf_ro_group" + project_id = 0 + vpc_id = tencentcloud_vpc.vpc.id + subnet_id = tencentcloud_subnet.subnet.id + replay_lag_eliminate = 1 + replay_latency_eliminate = 1 + max_replay_lag = 100 + max_replay_latency = 512 + min_delay_eliminate_reserve = 1 +} + +resource "tencentcloud_postgresql_readonly_instance" "example" { + read_only_group_id = tencentcloud_postgresql_readonly_group.example.id + master_db_instance_id = tencentcloud_postgresql_instance.example.id + zone = var.availability_zone + name = "example" + auto_renew_flag = 0 + db_version = "10.23" + instance_charge_type = "POSTPAID_BY_HOUR" + memory = 4 + cpu = 2 + storage = 200 + vpc_id = tencentcloud_vpc.vpc.id + subnet_id = tencentcloud_subnet.subnet.id + need_support_ipv6 = 0 + project_id = 0 + security_groups_ids = [ + "sg-5275dorp", + ] + + tags = { + createdBy : "terraform" } +} ` diff --git a/website/docs/r/postgresql_readonly_instance.html.markdown b/website/docs/r/postgresql_readonly_instance.html.markdown index 37ad0b2b5a..8a177862ca 100644 --- a/website/docs/r/postgresql_readonly_instance.html.markdown +++ b/website/docs/r/postgresql_readonly_instance.html.markdown @@ -96,6 +96,10 @@ resource "tencentcloud_postgresql_readonly_instance" "example" { security_groups_ids = [ tencentcloud_security_group.example.id, ] + + tags = { + createdBy : "terraform" + } } ``` @@ -120,6 +124,7 @@ The following arguments are supported: * `need_support_ipv6` - (Optional, Int, ForceNew) Whether to support IPv6 address access. Valid values: 1 (yes), 0 (no). * `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`. * `read_only_group_id` - (Optional, String) RO group ID. +* `tags` - (Optional, Map) The information of tags to be associated with instances. This parameter is left empty by default. * `voucher_ids` - (Optional, List: [`String`]) Specify Voucher Ids if `auto_voucher` was `1`, only support using 1 vouchers for now. ## Attributes Reference @@ -138,6 +143,6 @@ In addition to all arguments above, the following attributes are exported: postgresql readonly instance can be imported using the id, e.g. ``` -$ terraform import tencentcloud_postgresql_readonly_instance.example instance_id +$ terraform import tencentcloud_postgresql_readonly_instance.example pgro-jtb4v6oo ``` From fff94a12b5a90e7ac5b4c4c7e827b00ee1cf3cda Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 25 Jun 2024 17:40:33 +0800 Subject: [PATCH 2/5] add --- .changelog/2691.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2691.txt diff --git a/.changelog/2691.txt b/.changelog/2691.txt new file mode 100644 index 0000000000..d3c9785703 --- /dev/null +++ b/.changelog/2691.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_postgresql_readonly_instance: support `tags` params +``` From 79828d12931e0bdd12403b08947b646e8c1643d9 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 25 Jun 2024 17:46:13 +0800 Subject: [PATCH 3/5] add --- .../r/postgresql_readonly_instance.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/website/docs/r/postgresql_readonly_instance.html.markdown b/website/docs/r/postgresql_readonly_instance.html.markdown index 8a177862ca..095daaea4c 100644 --- a/website/docs/r/postgresql_readonly_instance.html.markdown +++ b/website/docs/r/postgresql_readonly_instance.html.markdown @@ -40,14 +40,14 @@ resource "tencentcloud_postgresql_instance" "example" { charge_type = "POSTPAID_BY_HOUR" vpc_id = tencentcloud_vpc.vpc.id subnet_id = tencentcloud_subnet.subnet.id - engine_version = "10.4" + engine_version = "10.23" root_user = "root123" root_password = "Root123$" charset = "UTF8" project_id = 0 - memory = 2 - cpu = 1 - storage = 10 + memory = 4 + cpu = 2 + storage = 200 tags = { test = "tf" @@ -84,11 +84,11 @@ resource "tencentcloud_postgresql_readonly_instance" "example" { zone = var.availability_zone name = "example" auto_renew_flag = 0 - db_version = "10.4" + db_version = "10.23" instance_charge_type = "POSTPAID_BY_HOUR" memory = 4 cpu = 2 - storage = 250 + storage = 200 vpc_id = tencentcloud_vpc.vpc.id subnet_id = tencentcloud_subnet.subnet.id need_support_ipv6 = 0 From 60756efd4e1839f294360c7549ee56a00e7e3086 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 25 Jun 2024 19:39:25 +0800 Subject: [PATCH 4/5] add --- ...ce_tc_postgresql_readonly_instance_test.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go index 29fe5b6401..4945a65877 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go @@ -42,6 +42,7 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "storage", "200"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_ip"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "private_access_port"), + resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "read_only_group_id"), ), }, { @@ -114,7 +115,21 @@ resource "tencentcloud_postgresql_instance" "example" { } } +resource "tencentcloud_postgresql_readonly_group" "example" { + master_db_instance_id = tencentcloud_postgresql_instance.example.id + name = "tf_ro_group" + project_id = 0 + vpc_id = tencentcloud_vpc.vpc.id + subnet_id = tencentcloud_subnet.subnet.id + replay_lag_eliminate = 1 + replay_latency_eliminate = 1 + max_replay_lag = 100 + max_replay_latency = 512 + min_delay_eliminate_reserve = 1 +} + resource "tencentcloud_postgresql_readonly_instance" "example" { + read_only_group_id = tencentcloud_postgresql_readonly_group.example.id master_db_instance_id = tencentcloud_postgresql_instance.example.id zone = var.availability_zone name = "example" @@ -179,7 +194,7 @@ resource "tencentcloud_postgresql_instance" "example" { } } -resource "tencentcloud_postgresql_readonly_group" "example" { +resource "tencentcloud_postgresql_readonly_group" "example_new" { master_db_instance_id = tencentcloud_postgresql_instance.example.id name = "tf_ro_group" project_id = 0 @@ -193,7 +208,7 @@ resource "tencentcloud_postgresql_readonly_group" "example" { } resource "tencentcloud_postgresql_readonly_instance" "example" { - read_only_group_id = tencentcloud_postgresql_readonly_group.example.id + read_only_group_id = tencentcloud_postgresql_readonly_group.example_new.id master_db_instance_id = tencentcloud_postgresql_instance.example.id zone = var.availability_zone name = "example" From 0591f18382b2051c78c1389db1f38dbec6b162eb Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Wed, 26 Jun 2024 14:47:37 +0800 Subject: [PATCH 5/5] add --- ...esource_tc_postgresql_readonly_instance.go | 13 +++++- ...ce_tc_postgresql_readonly_instance_test.go | 35 ++++++++------ .../service_tencentcloud_postgresql.go | 46 +++++++++++++++++++ 3 files changed, 77 insertions(+), 17 deletions(-) diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go index 4b831aa122..bb643d7c3a 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go @@ -397,8 +397,17 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceRead(d *schema.ResourceData, _ = d.Set("subnet_id", instance.SubnetId) _ = d.Set("name", instance.DBInstanceName) _ = d.Set("need_support_ipv6", instance.SupportIpv6) - // set readonly group when DescribeReadOnlyGroups ready for filter by the readonly group id - // _ = d.Set("read_only_group_id", readonlyGroup.Id) + + // read only group + masterDBInstanceId := instance.MasterDBInstanceId + readOnlyGroupId, err := postgresqlService.DescribeReadOnlyGroupsById(ctx, *masterDBInstanceId, d.Id()) + if err != nil { + return err + } + + if readOnlyGroupId != nil { + _ = d.Set("read_only_group_id", readOnlyGroupId) + } // security groups sg, err := postgresqlService.DescribeDBInstanceSecurityGroupsById(ctx, d.Id()) diff --git a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go index 4945a65877..8e4ca3c6a7 100644 --- a/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go +++ b/tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go @@ -3,6 +3,7 @@ package postgresql_test import ( "testing" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -15,17 +16,12 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { // t.Parallel() resource.Test(t, resource.TestCase{ PreCheck: func() { - tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheck(t) + acctest.AccPreCheck(t) }, Providers: tcacctest.AccProviders, Steps: []resource.TestStep{ { - PreConfig: func() { - tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) - }, - Config: testAccPostgresqlReadonlyInstanceInstance_basic_without_rogroup, + Config: testAccPostgresqlReadonlyInstanceInstance, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "master_db_instance_id"), @@ -46,11 +42,7 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { ), }, { - PreConfig: func() { - tcacctest.AccStepSetRegion(t, "ap-guangzhou") - tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) - }, - Config: testAccPostgresqlReadonlyInstanceInstance_basic_update_rogroup, + Config: testAccPostgresqlReadonlyInstanceInstanceUpdate, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"), resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "master_db_instance_id"), @@ -74,7 +66,7 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_basic(t *testing.T) { }) } -const testAccPostgresqlReadonlyInstanceInstance_basic_without_rogroup string = ` +const testAccPostgresqlReadonlyInstanceInstance string = ` variable "availability_zone" { default = "ap-guangzhou-3" } @@ -153,7 +145,7 @@ resource "tencentcloud_postgresql_readonly_instance" "example" { } ` -const testAccPostgresqlReadonlyInstanceInstance_basic_update_rogroup string = ` +const testAccPostgresqlReadonlyInstanceInstanceUpdate string = ` variable "availability_zone" { default = "ap-guangzhou-3" } @@ -194,7 +186,7 @@ resource "tencentcloud_postgresql_instance" "example" { } } -resource "tencentcloud_postgresql_readonly_group" "example_new" { +resource "tencentcloud_postgresql_readonly_group" "example" { master_db_instance_id = tencentcloud_postgresql_instance.example.id name = "tf_ro_group" project_id = 0 @@ -207,6 +199,19 @@ resource "tencentcloud_postgresql_readonly_group" "example_new" { min_delay_eliminate_reserve = 1 } +resource "tencentcloud_postgresql_readonly_group" "example_new" { + master_db_instance_id = tencentcloud_postgresql_instance.example.id + name = "tf_ro_group_new" + project_id = 0 + vpc_id = tencentcloud_vpc.vpc.id + subnet_id = tencentcloud_subnet.subnet.id + replay_lag_eliminate = 1 + replay_latency_eliminate = 1 + max_replay_lag = 100 + max_replay_latency = 512 + min_delay_eliminate_reserve = 1 +} + resource "tencentcloud_postgresql_readonly_instance" "example" { read_only_group_id = tencentcloud_postgresql_readonly_group.example_new.id master_db_instance_id = tencentcloud_postgresql_instance.example.id diff --git a/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go b/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go index ba557b3e59..39b2ae4be3 100644 --- a/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go +++ b/tencentcloud/services/postgresql/service_tencentcloud_postgresql.go @@ -492,6 +492,52 @@ func (me *PostgresqlService) DescribeDBInstanceSecurityGroupsById(ctx context.Co return } +func (me *PostgresqlService) DescribeReadOnlyGroupsById(ctx context.Context, masterDBInstanceId string, instanceId string) (readOnlyGroupId *string, errRet error) { + logId := tccommon.GetLogId(ctx) + request := postgresql.NewDescribeReadOnlyGroupsRequest() + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail,reason[%s]", logId, request.GetAction(), errRet.Error()) + } + }() + + request.Filters = []*postgresql.Filter{ + { + Name: helper.String("db-master-instance-id"), + Values: []*string{ + helper.String(masterDBInstanceId), + }, + }, + } + + response, err := me.client.UsePostgresqlClient().DescribeReadOnlyGroups(request) + if err != nil { + errRet = err + return + } + + if response == nil || response.Response == nil { + errRet = fmt.Errorf("TencentCloud SDK return nil response, %s", request.GetAction()) + return + } + + roGroupList := response.Response.ReadOnlyGroupList + if len(roGroupList) > 0 { + for _, roGroup := range roGroupList { + roDBInstanceList := roGroup.ReadOnlyDBInstanceList + for _, roDBInstance := range roDBInstanceList { + roDBInstanceId := *roDBInstance.DBInstanceId + if roDBInstanceId == instanceId { + readOnlyGroupId = roGroup.ReadOnlyGroupId + return + } + } + } + } + + return +} + func (me *PostgresqlService) ModifyDBInstanceSecurityGroupsById(ctx context.Context, instanceId string, securityGroupIds []*string) (errRet error) { logId := tccommon.GetLogId(ctx) request := postgresql.NewModifyDBInstanceSecurityGroupsRequest()