@@ -6,7 +6,7 @@ Example Usage
66```hcl
77resource "tencentcloud_cam_service_linked_role" "service_linked_role" {
88 qcs_service_name = ["cvm.qcloud.com","ekslog.tke.cloud.tencent.com"]
9- custom_suffix = "x-1 "
9+ custom_suffix = "tf "
1010 description = "desc cam"
1111 tags = {
1212 "createdBy" = "terraform"
@@ -19,8 +19,10 @@ package tencentcloud
1919
2020import (
2121 "context"
22+ "encoding/json"
2223 "fmt"
2324 "log"
25+ "strings"
2426
2527 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
2628 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -39,13 +41,15 @@ func resourceTencentCloudCamServiceLinkedRole() *schema.Resource {
3941 Type : schema .TypeSet ,
4042 Elem : & schema.Schema {Type : schema .TypeString },
4143 Required : true ,
44+ ForceNew : true ,
4245 Description : "Authorization service, the Tencent Cloud service principal with this role attached." ,
4346 },
4447
4548 "custom_suffix" : {
4649 Type : schema .TypeString ,
4750 Optional : true ,
48- Description : "The custom suffix, based on the string you provide, is combined with the prefix provided by the service to form the full role name." ,
51+ ForceNew : true ,
52+ Description : "The custom suffix, based on the string you provide, is combined with the prefix provided by the service to form the full role name. This field is not allowed to contain the character `_`." ,
4953 },
5054
5155 "description" : {
@@ -60,6 +64,9 @@ func resourceTencentCloudCamServiceLinkedRole() *schema.Resource {
6064 Description : "Tag description list." ,
6165 },
6266 },
67+ Importer : & schema.ResourceImporter {
68+ State : schema .ImportStatePassthrough ,
69+ },
6370 }
6471}
6572
@@ -123,15 +130,14 @@ func resourceTencentCloudCamServiceLinkedRoleCreate(d *schema.ResourceData, meta
123130 roleId = * response .Response .RoleId
124131
125132 d .SetId (roleId )
126- ctx := context .WithValue (context .TODO (), logIdKey , logId )
127- if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
128- tagService := TagService {client : meta .(* TencentCloudClient ).apiV3Conn }
129- region := meta .(* TencentCloudClient ).apiV3Conn .Region
130- resourceName := fmt .Sprintf ("qcs::cam:%s:uin/:RoleId/%s" , region , roleId )
131- if err := tagService .ModifyTags (ctx , resourceName , tags , nil ); err != nil {
132- return err
133- }
134- }
133+ //ctx := context.WithValue(context.TODO(), logIdKey, logId)
134+ //if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
135+ // tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn}
136+ // resourceName := fmt.Sprintf("qcs::cam:%s:uin/:role/tencentcloudServiceRole/%s", "", roleId)
137+ // if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
138+ // return err
139+ // }
140+ //}
135141 return resourceTencentCloudCamServiceLinkedRoleRead (d , meta )
136142}
137143
@@ -155,25 +161,40 @@ func resourceTencentCloudCamServiceLinkedRoleRead(d *schema.ResourceData, meta i
155161 return fmt .Errorf ("resource `serviceLinkedRole` %s does not exist" , roleId )
156162 }
157163
158- // if qcsServiceName != "" {
159- // _ = d.Set("qcs_service_name", qcsServiceName)
160- // }
164+ if serviceLinkedRole .PolicyDocument != nil {
165+ var documentJson Document
166+ err = json .Unmarshal ([]byte (* serviceLinkedRole .PolicyDocument ), & documentJson )
167+ if err != nil {
168+ return err
169+ }
170+ if documentJson .Statement != nil && len (documentJson .Statement ) > 0 {
171+ principal := documentJson .Statement [0 ].Principal
172+ if principal .Service != nil && len (principal .Service ) > 0 {
173+ _ = d .Set ("qcs_service_name" , principal .Service )
174+ }
175+ }
176+ }
161177
162- // if customSuffix != "" {
163- // _ = d.Set("custom_suffix", customSuffix)
164- // }
178+ if serviceLinkedRole .RoleName != nil {
179+ roleName := strings .Split (* serviceLinkedRole .RoleName , "_" )
180+ if len (roleName ) > 0 {
181+ _ = d .Set ("custom_suffix" , roleName [len (roleName )- 1 ])
182+ }
183+ }
165184
166185 if serviceLinkedRole .Description != nil {
167186 _ = d .Set ("description" , serviceLinkedRole .Description )
168187 }
169188
170- tcClient := meta .(* TencentCloudClient ).apiV3Conn
171- tagService := & TagService {client : tcClient }
172- tags , err := tagService .DescribeResourceTags (ctx , "cam" , "RoleId" , tcClient .Region , roleId )
173- if err != nil {
174- return err
189+ if serviceLinkedRole .Tags != nil {
190+ tagsMap := map [string ]interface {}{}
191+ for _ , tag := range serviceLinkedRole .Tags {
192+ if tag .Key != nil && tag .Value != nil {
193+ tagsMap [* tag .Key ] = tag .Value
194+ }
195+ }
196+ _ = d .Set ("tags" , tagsMap )
175197 }
176- _ = d .Set ("tags" , tags )
177198
178199 return nil
179200}
@@ -184,47 +205,39 @@ func resourceTencentCloudCamServiceLinkedRoleUpdate(d *schema.ResourceData, meta
184205
185206 logId := getLogId (contextNil )
186207 ctx := context .WithValue (context .TODO (), logIdKey , logId )
187- request := cam .NewUpdateRoleDescriptionRequest ()
188208
189209 roleId := d .Id ()
190210
191- request .RoleId = & roleId
192-
193- if d .HasChange ("qcs_service_name" ) {
194- return fmt .Errorf ("`qcs_service_name` do not support change now." )
195- }
196-
197- if d .HasChange ("custom_suffix" ) {
198- return fmt .Errorf ("`custom_suffix` do not support change now." )
199- }
200-
201211 if d .HasChange ("description" ) {
212+ request := cam .NewUpdateRoleDescriptionRequest ()
213+ request .RoleId = & roleId
214+
202215 if v , ok := d .GetOk ("description" ); ok {
203216 request .Description = helper .String (v .(string ))
204217 }
205- }
206218
207- err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
208- result , e := meta .(* TencentCloudClient ).apiV3Conn .UseCamClient ().UpdateRoleDescription (request )
209- if e != nil {
210- return retryError (e )
211- } else {
212- log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " ,
213- logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
219+ err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
220+ result , e := meta .(* TencentCloudClient ).apiV3Conn .UseCamClient ().UpdateRoleDescription (request )
221+ if e != nil {
222+ return retryError (e )
223+ } else {
224+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " ,
225+ logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
226+ }
227+ return nil
228+ })
229+ if err != nil {
230+ log .Printf ("[CRITAL]%s create cam serviceLinkedRole failed, reason:%+v" , logId , err )
231+ return err
214232 }
215- return nil
216- })
217- if err != nil {
218- log .Printf ("[CRITAL]%s create cam serviceLinkedRole failed, reason:%+v" , logId , err )
219- return err
220233 }
221234
222235 if d .HasChange ("tags" ) {
223236 tcClient := meta .(* TencentCloudClient ).apiV3Conn
224237 tagService := & TagService {client : tcClient }
225238 oldTags , newTags := d .GetChange ("tags" )
226239 replaceTags , deleteTags := diffTags (oldTags .(map [string ]interface {}), newTags .(map [string ]interface {}))
227- resourceName := BuildTagResourceName ("cam" , "RoleId " , tcClient . Region , d .Id ())
240+ resourceName := BuildTagResourceName ("cam" , "role/tencentcloudServiceRole " , "" , d .Id ())
228241 if err := tagService .ModifyTags (ctx , resourceName , replaceTags , deleteTags ); err != nil {
229242 return err
230243 }
0 commit comments