Skip to content

Commit 4fabb8d

Browse files
tongyimingmikatong
and
mikatong
authored
fix(pg): [124136139]support tags (#3364)
* support tags * add changelog --------- Co-authored-by: mikatong <[email protected]>
1 parent 8116398 commit 4fabb8d

File tree

4 files changed

+105
-32
lines changed

4 files changed

+105
-32
lines changed

.changelog/3364.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_readonly_instance: support tags
3+
```

tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance.go

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
1111
svccrs "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/crs"
12+
svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
1213

1314
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
1415

@@ -132,11 +133,12 @@ func ResourceTencentCloudPostgresqlReadonlyInstance() *schema.Resource {
132133
ForceNew: true,
133134
Description: "Whether to support IPv6 address access. Valid values: 1 (yes), 0 (no).",
134135
},
135-
//"tag_list": {
136-
// Type: schema.TypeMap,
137-
// Optional: true,
138-
// Description: "The information of tags to be associated with instances. This parameter is left empty by default..",
139-
//},
136+
"tags": {
137+
Type: schema.TypeMap,
138+
Optional: true,
139+
Computed: true,
140+
Description: "Tags.",
141+
},
140142
"read_only_group_id": {
141143
Type: schema.TypeString,
142144
Optional: true,
@@ -252,14 +254,6 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData
252254
if v, ok := d.GetOk("dedicated_cluster_id"); ok {
253255
request.DedicatedClusterId = helper.String(v.(string))
254256
}
255-
//if tags := helper.GetTags(d, "tag_list"); len(tags) > 0 {
256-
// for k, v := range tags {
257-
// request.TagList = &postgresql.Tag{
258-
// TagKey: &k,
259-
// TagValue: &v,
260-
// }
261-
// }
262-
//}
263257

264258
// get specCode with db_version and memory
265259
var allowVersion, allowSpec []string
@@ -366,6 +360,15 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceCreate(d *schema.ResourceData
366360
return err
367361
}
368362

363+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
364+
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
365+
tagService := svctag.NewTagService(tcClient)
366+
resourceName := tccommon.BuildTagResourceName("postgres", "DBInstanceId", tcClient.Region, d.Id())
367+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
368+
return err
369+
}
370+
}
371+
369372
return resourceTencentCloudPostgresqlReadOnlyInstanceRead(d, meta)
370373
}
371374

@@ -437,11 +440,14 @@ func resourceTencentCloudPostgresqlReadOnlyInstanceRead(d *schema.ResourceData,
437440
_ = d.Set("security_groups_ids", sg)
438441
}
439442

440-
//tags := make(map[string]string, len(instance.TagList))
441-
//for _, tag := range instance.TagList {
442-
// tags[*tag.TagKey] = *tag.TagValue
443-
//}
444-
//_ = d.Set("tag_list", tags)
443+
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
444+
tagService := svctag.NewTagService(tcClient)
445+
tags, err := tagService.DescribeResourceTags(ctx, "postgres", "DBInstanceId", tcClient.Region, d.Id())
446+
if err != nil {
447+
return err
448+
}
449+
450+
_ = d.Set("tags", tags)
445451

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

617623
}
618624

619-
//if d.HasChange("tags") {
620-
//
621-
// oldValue, newValue := d.GetChange("tags")
622-
// replaceTags, deleteTags := diffTags(oldValue.(map[string]interface{}), newValue.(map[string]interface{}))
623-
//
624-
// tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
625-
// tagService := svctag.NewTagService(tcClient)
626-
// resourceName := tccommon.BuildTagResourceName("postgres", "DBInstanceId", tcClient.Region, d.Id())
627-
// err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
628-
// if err != nil {
629-
// return err
630-
// }
631-
//
632-
//}
625+
if d.HasChange("tags") {
626+
oldValue, newValue := d.GetChange("tags")
627+
replaceTags, deleteTags := svctag.DiffTags(oldValue.(map[string]interface{}), newValue.(map[string]interface{}))
628+
629+
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
630+
tagService := svctag.NewTagService(tcClient)
631+
resourceName := tccommon.BuildTagResourceName("postgres", "DBInstanceId", tcClient.Region, d.Id())
632+
err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
633+
if err != nil {
634+
return err
635+
}
636+
}
633637

634638
d.Partial(false)
635639

tencentcloud/services/postgresql/resource_tc_postgresql_readonly_instance_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,31 @@ func TestAccTencentCloudPostgresqlReadonlyInstanceResource_prepaid(t *testing.T)
150150
})
151151
}
152152

153+
func TestAccTencentCloudPostgresqlReadonlyInstanceResource_tags(t *testing.T) {
154+
resource.Test(t, resource.TestCase{
155+
PreCheck: func() {
156+
tcacctest.AccPreCheck(t)
157+
},
158+
Providers: tcacctest.AccProviders,
159+
Steps: []resource.TestStep{
160+
{
161+
Config: testAccPostgresqlReadonlyInstanceInstance_tag,
162+
Check: resource.ComposeTestCheckFunc(
163+
resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"),
164+
resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "tags.tf", "test"),
165+
),
166+
},
167+
{
168+
Config: testAccPostgresqlReadonlyInstanceInstance_tagUpdate,
169+
Check: resource.ComposeTestCheckFunc(
170+
resource.TestCheckResourceAttrSet(testPostgresqlReadonlyInstanceResourceKey, "id"),
171+
resource.TestCheckResourceAttr(testPostgresqlReadonlyInstanceResourceKey, "tags.tf", "test1"),
172+
),
173+
},
174+
},
175+
})
176+
}
177+
153178
const testAccPostgresqlReadonlyInstanceInstance_basic_without_rogroup string = tcacctest.OperationPresetPGSQL + tcacctest.DefaultVpcSubnets + tcacctest.DefaultSecurityGroupData + `
154179
resource "tencentcloud_postgresql_readonly_instance" "instance" {
155180
auto_renew_flag = 0
@@ -281,3 +306,43 @@ resource "tencentcloud_postgresql_readonly_instance" "instance" {
281306
zone = "ap-guangzhou-3"
282307
}
283308
`
309+
310+
const testAccPostgresqlReadonlyInstanceInstance_tag = `
311+
resource "tencentcloud_postgresql_readonly_instance" "instance" {
312+
db_version = "17.0"
313+
master_db_instance_id = "postgres-opvw0y6n"
314+
memory = 4
315+
cpu = 2
316+
name = "tf_ro_instance_tag"
317+
need_support_ipv6 = 0
318+
project_id = 0
319+
security_groups_ids = ["sg-kensue7b"]
320+
storage = 20
321+
vpc_id = "vpc-kvy6qnqj"
322+
subnet_id = "subnet-rgj6t4u0"
323+
zone = "ap-guangzhou-6"
324+
tags = {
325+
tf = "test"
326+
}
327+
}
328+
`
329+
330+
const testAccPostgresqlReadonlyInstanceInstance_tagUpdate = `
331+
resource "tencentcloud_postgresql_readonly_instance" "instance" {
332+
db_version = "17.0"
333+
master_db_instance_id = "postgres-opvw0y6n"
334+
memory = 4
335+
cpu = 2
336+
name = "tf_ro_instance_tag"
337+
need_support_ipv6 = 0
338+
project_id = 0
339+
security_groups_ids = ["sg-kensue7b"]
340+
storage = 20
341+
vpc_id = "vpc-kvy6qnqj"
342+
subnet_id = "subnet-rgj6t4u0"
343+
zone = "ap-guangzhou-6"
344+
tags = {
345+
tf = "test1"
346+
}
347+
}
348+
`

website/docs/r/postgresql_readonly_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ The following arguments are supported:
227227
* `need_support_ipv6` - (Optional, Int, ForceNew) Whether to support IPv6 address access. Valid values: 1 (yes), 0 (no).
228228
* `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`.
229229
* `read_only_group_id` - (Optional, String) RO group ID.
230+
* `tags` - (Optional, Map) Tags.
230231
* `voucher_ids` - (Optional, List: [`String`]) Specify Voucher Ids if `auto_voucher` was `1`, only support using 1 vouchers for now.
231232
* `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.
232233

0 commit comments

Comments
 (0)