Skip to content

Commit 67dff24

Browse files
gitmknanonymous
andauthored
fix: support for multiple qcs_service_name (#1452)
* fix: support for multiple qcs_service_name * feat: add changelog Co-authored-by: anonymous <anonymous@mail.org>
1 parent e795588 commit 67dff24

File tree

4 files changed

+31
-58
lines changed

4 files changed

+31
-58
lines changed

.changelog/1452.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_cam_service_linked_role: support for multiple qcs_service_name
3+
```

tencentcloud/resource_tc_cam_service_linked_role.go

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Example Usage
55
66
```hcl
77
resource "tencentcloud_cam_service_linked_role" "service_linked_role" {
8-
qcs_service_name = "postgreskms.postgres.cloud.tencent.com"
8+
qcs_service_name = ["cvm.qcloud.com","ekslog.tke.cloud.tencent.com"]
99
custom_suffix = "x-1"
1010
description = "desc cam"
1111
tags = {
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"fmt"
2323
"log"
24-
"strings"
2524

2625
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2726
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -37,7 +36,8 @@ func resourceTencentCloudCamServiceLinkedRole() *schema.Resource {
3736
Delete: resourceTencentCloudCamServiceLinkedRoleDelete,
3837
Schema: map[string]*schema.Schema{
3938
"qcs_service_name": {
40-
Type: schema.TypeString,
39+
Type: schema.TypeSet,
40+
Elem: &schema.Schema{Type: schema.TypeString},
4141
Required: true,
4242
Description: "Authorization service, the Tencent Cloud service principal with this role attached.",
4343
},
@@ -70,20 +70,21 @@ func resourceTencentCloudCamServiceLinkedRoleCreate(d *schema.ResourceData, meta
7070
logId := getLogId(contextNil)
7171

7272
var (
73-
request = cam.NewCreateServiceLinkedRoleRequest()
74-
response *cam.CreateServiceLinkedRoleResponse
75-
roleId string
76-
qcsServiceName = ""
77-
customSuffix = ""
73+
request = cam.NewCreateServiceLinkedRoleRequest()
74+
response *cam.CreateServiceLinkedRoleResponse
75+
roleId string
7876
)
7977

8078
if v, ok := d.GetOk("qcs_service_name"); ok {
81-
qcsServiceName = v.(string)
82-
request.QCSServiceName = helper.Strings([]string{v.(string)})
79+
serviceName := v.(*schema.Set).List()
80+
serviceNameArr := make([]*string, 0, len(serviceName))
81+
for _, name := range serviceName {
82+
serviceNameArr = append(serviceNameArr, helper.String(name.(string)))
83+
}
84+
request.QCSServiceName = serviceNameArr
8385
}
8486

8587
if v, ok := d.GetOk("custom_suffix"); ok {
86-
customSuffix = v.(string)
8788
request.CustomSuffix = helper.String(v.(string))
8889
}
8990

@@ -121,7 +122,7 @@ func resourceTencentCloudCamServiceLinkedRoleCreate(d *schema.ResourceData, meta
121122

122123
roleId = *response.Response.RoleId
123124

124-
d.SetId(roleId + FILED_SP + qcsServiceName + FILED_SP + customSuffix)
125+
d.SetId(roleId)
125126
ctx := context.WithValue(context.TODO(), logIdKey, logId)
126127
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
127128
tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
@@ -142,14 +143,7 @@ func resourceTencentCloudCamServiceLinkedRoleRead(d *schema.ResourceData, meta i
142143
ctx := context.WithValue(context.TODO(), logIdKey, logId)
143144
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn}
144145

145-
resourceId := d.Id()
146-
items := strings.Split(resourceId, FILED_SP)
147-
if len(items) != 3 {
148-
return fmt.Errorf("invalid ID %s", resourceId)
149-
}
150-
roleId := items[0]
151-
qcsServiceName := items[1]
152-
customSuffix := items[2]
146+
roleId := d.Id()
153147

154148
serviceLinkedRole, err := service.DescribeCamServiceLinkedRole(ctx, roleId)
155149
if err != nil {
@@ -161,13 +155,13 @@ func resourceTencentCloudCamServiceLinkedRoleRead(d *schema.ResourceData, meta i
161155
return fmt.Errorf("resource `serviceLinkedRole` %s does not exist", roleId)
162156
}
163157

164-
if qcsServiceName != "" {
165-
_ = d.Set("qcs_service_name", qcsServiceName)
166-
}
158+
// if qcsServiceName != "" {
159+
// _ = d.Set("qcs_service_name", qcsServiceName)
160+
// }
167161

168-
if customSuffix != "" {
169-
_ = d.Set("custom_suffix", customSuffix)
170-
}
162+
// if customSuffix != "" {
163+
// _ = d.Set("custom_suffix", customSuffix)
164+
// }
171165

172166
if serviceLinkedRole.Description != nil {
173167
_ = d.Set("description", serviceLinkedRole.Description)
@@ -192,14 +186,7 @@ func resourceTencentCloudCamServiceLinkedRoleUpdate(d *schema.ResourceData, meta
192186
ctx := context.WithValue(context.TODO(), logIdKey, logId)
193187
request := cam.NewUpdateRoleDescriptionRequest()
194188

195-
resourceId := d.Id()
196-
items := strings.Split(resourceId, FILED_SP)
197-
if len(items) != 3 {
198-
return fmt.Errorf("invalid ID %s", resourceId)
199-
}
200-
roleId := items[0]
201-
// qcsServiceName := items[1]
202-
// customSuffix := items[2]
189+
roleId := d.Id()
203190

204191
request.RoleId = &roleId
205192

@@ -255,14 +242,7 @@ func resourceTencentCloudCamServiceLinkedRoleDelete(d *schema.ResourceData, meta
255242

256243
service := CamService{client: meta.(*TencentCloudClient).apiV3Conn}
257244

258-
resourceId := d.Id()
259-
items := strings.Split(resourceId, FILED_SP)
260-
if len(items) != 3 {
261-
return fmt.Errorf("invalid ID %s", resourceId)
262-
}
263-
roleId := items[0]
264-
// qcsServiceName := items[1]
265-
// customSuffix := items[2]
245+
roleId := d.Id()
266246

267247
serviceLinkedRole, err := service.DescribeCamServiceLinkedRole(ctx, roleId)
268248
if err != nil {

tencentcloud/resource_tc_cam_service_linked_role_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tencentcloud
33
import (
44
"context"
55
"fmt"
6-
"strings"
76
"testing"
87

98
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -23,7 +22,7 @@ func TestAccTencentCloudCamServiceLinkedRoleResource_basic(t *testing.T) {
2322
Check: resource.ComposeTestCheckFunc(
2423
testAccCheckCamServiceLinkedRoleExists("tencentcloud_cam_service_linked_role.service_linked_role"),
2524
resource.TestCheckResourceAttrSet("tencentcloud_cam_service_linked_role.service_linked_role", "id"),
26-
resource.TestCheckResourceAttr("tencentcloud_cam_service_linked_role.service_linked_role", "qcs_service_name", "postgreskms.postgres.cloud.tencent.com"),
25+
resource.TestCheckResourceAttr("tencentcloud_cam_service_linked_role.service_linked_role", "qcs_service_name.#", "2"),
2726
resource.TestCheckResourceAttr("tencentcloud_cam_service_linked_role.service_linked_role", "custom_suffix", "x-1"),
2827
resource.TestCheckResourceAttr("tencentcloud_cam_service_linked_role.service_linked_role", "description", "desc cam"),
2928
resource.TestCheckResourceAttr("tencentcloud_cam_service_linked_role.service_linked_role", "tags.createdBy", "terraform"),
@@ -48,13 +47,8 @@ func testAccCheckCamServiceLinkedRoleDestroy(s *terraform.State) error {
4847
if rs.Primary.ID == "" {
4948
return fmt.Errorf("[CHECK]CAM ServiceLinkedRole id is not set")
5049
}
51-
items := strings.Split(rs.Primary.ID, FILED_SP)
52-
if len(items) != 3 {
53-
return fmt.Errorf("invalid ID %s", rs.Primary.ID)
54-
}
55-
roleId := items[0]
5650

57-
instance, err := camService.DescribeCamServiceLinkedRole(ctx, roleId)
51+
instance, err := camService.DescribeCamServiceLinkedRole(ctx, rs.Primary.ID)
5852
if err == nil && instance != nil {
5953
return fmt.Errorf("[CHECK]CAM ServiceLinkedRole still exists: %s", rs.Primary.ID)
6054
}
@@ -75,11 +69,7 @@ func testAccCheckCamServiceLinkedRoleExists(n string) resource.TestCheckFunc {
7569
return fmt.Errorf("[CHECK]CAM ServiceLinkedRole id is not set")
7670
}
7771

78-
items := strings.Split(rs.Primary.ID, FILED_SP)
79-
if len(items) != 3 {
80-
return fmt.Errorf("invalid ID %s", rs.Primary.ID)
81-
}
82-
roleId := items[0]
72+
roleId := rs.Primary.ID
8373

8474
camService := CamService{
8575
client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn,
@@ -98,7 +88,7 @@ func testAccCheckCamServiceLinkedRoleExists(n string) resource.TestCheckFunc {
9888
const testAccCamServiceLinkedRole = `
9989
10090
resource "tencentcloud_cam_service_linked_role" "service_linked_role" {
101-
qcs_service_name = "postgreskms.postgres.cloud.tencent.com"
91+
qcs_service_name = ["cvm.qcloud.com","ekslog.tke.cloud.tencent.com"]
10292
custom_suffix = "x-1"
10393
description = "desc cam"
10494
tags = {

website/docs/r/cam_service_linked_role.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Provides a resource to create a cam service_linked_role
1515

1616
```hcl
1717
resource "tencentcloud_cam_service_linked_role" "service_linked_role" {
18-
qcs_service_name = "postgreskms.postgres.cloud.tencent.com"
18+
qcs_service_name = ["cvm.qcloud.com", "ekslog.tke.cloud.tencent.com"]
1919
custom_suffix = "x-1"
2020
description = "desc cam"
2121
tags = {
@@ -28,7 +28,7 @@ resource "tencentcloud_cam_service_linked_role" "service_linked_role" {
2828

2929
The following arguments are supported:
3030

31-
* `qcs_service_name` - (Required, String) Authorization service, the Tencent Cloud service principal with this role attached.
31+
* `qcs_service_name` - (Required, Set: [`String`]) Authorization service, the Tencent Cloud service principal with this role attached.
3232
* `custom_suffix` - (Optional, String) The custom suffix, based on the string you provide, is combined with the prefix provided by the service to form the full role name.
3333
* `description` - (Optional, String) role description.
3434
* `tags` - (Optional, Map) Tag description list.

0 commit comments

Comments
 (0)