Skip to content

fix(pg): [124136139]support tags #3364

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 2 commits into from
May 21, 2025
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/3364.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_postgresql_readonly_instance: support tags
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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"

Expand Down Expand Up @@ -132,11 +133,12 @@ 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,
Computed: true,
Description: "Tags.",
},
"read_only_group_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -252,14 +254,6 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData
if v, ok := d.GetOk("dedicated_cluster_id"); ok {
request.DedicatedClusterId = helper.String(v.(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
Expand Down Expand Up @@ -366,6 +360,15 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData
return err
}

if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
tagService := svctag.NewTagService(tcClient)
resourceName := tccommon.BuildTagResourceName("postgres", "DBInstanceId", tcClient.Region, d.Id())
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
return err
}
}

return resourceTencentCloudPostgresqlReadOnlyInstanceRead(d, meta)
}

Expand Down Expand Up @@ -437,11 +440,14 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceRead(d *schema.ResourceData,
_ = 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)
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
tagService := svctag.NewTagService(tcClient)
tags, err := tagService.DescribeResourceTags(ctx, "postgres", "DBInstanceId", tcClient.Region, d.Id())
if err != nil {
return err
}

_ = d.Set("tags", tags)

// computed
_ = d.Set("create_time", instance.CreateTime)
Expand Down Expand Up @@ -616,20 +622,18 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceUpdate(d *schema.ResourceData

}

//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{}))

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
}
}

d.Partial(false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_prepaid(t *testing.T)
})
}

func TestAccTencentCloudPostgresqlReadonlyInstanceResource_tags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{
{
Config: testAccPostgresqlReadonlyInstanceInstance_tag,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"),
resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "tags.tf", "test"),
),
},
{
Config: testAccPostgresqlReadonlyInstanceInstance_tagUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"),
resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "tags.tf", "test1"),
),
},
},
})
}

const testAccPostgresqlReadonlyInstanceInstance_basic_without_rogroup string = tcacctest.OperationPresetPGSQL + tcacctest.DefaultVpcSubnets + tcacctest.DefaultSecurityGroupData + `
resource "tencentcloud_postgresql_readonly_instance" "instance" {
auto_renew_flag = 0
Expand Down Expand Up @@ -281,3 +306,43 @@ resource "tencentcloud_postgresql_readonly_instance" "instance" {
zone = "ap-guangzhou-3"
}
`

const testAccPostgresqlReadonlyInstanceInstance_tag = `
resource "tencentcloud_postgresql_readonly_instance" "instance" {
db_version = "17.0"
master_db_instance_id = "postgres-opvw0y6n"
memory = 4
cpu = 2
name = "tf_ro_instance_tag"
need_support_ipv6 = 0
project_id = 0
security_groups_ids = ["sg-kensue7b"]
storage = 20
vpc_id = "vpc-kvy6qnqj"
subnet_id = "subnet-rgj6t4u0"
zone = "ap-guangzhou-6"
tags = {
tf = "test"
}
}
`

const testAccPostgresqlReadonlyInstanceInstance_tagUpdate = `
resource "tencentcloud_postgresql_readonly_instance" "instance" {
db_version = "17.0"
master_db_instance_id = "postgres-opvw0y6n"
memory = 4
cpu = 2
name = "tf_ro_instance_tag"
need_support_ipv6 = 0
project_id = 0
security_groups_ids = ["sg-kensue7b"]
storage = 20
vpc_id = "vpc-kvy6qnqj"
subnet_id = "subnet-rgj6t4u0"
zone = "ap-guangzhou-6"
tags = {
tf = "test1"
}
}
`
1 change: 1 addition & 0 deletions website/docs/r/postgresql_readonly_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,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) Tags.
* `voucher_ids` - (Optional, List: [`String`]) Specify Voucher Ids if `auto_voucher` was `1`, only support using 1 vouchers for now.
* `wait_switch` - (Optional, Int) Switch time after instance configurations are modified. `0`: Switch immediately; `2`: Switch during maintenance time window. Default: `0`. Note: This only takes effect when updating the `memory`, `storage`, `cpu` fields.

Expand Down
Loading