Skip to content

Commit

Permalink
Fix IBM-Cloud#3803 set roles in resource_key
Browse files Browse the repository at this point in the history
  • Loading branch information
Kavya Handadi authored and SunithaGudisagarIBM1 committed Sep 14, 2022
1 parent 376d737 commit 7e16792
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions ibm/service/resourcecontroller/resource_ibm_resource_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,36 +300,32 @@ func resourceIBMResourceKeyRead(d *schema.ResourceData, meta interface{}) error
d.Set("status", *resourceKey.State)
if resourceKey.Credentials != nil && resourceKey.Credentials.IamRoleCRN != nil {
roleCrn := *resourceKey.Credentials.IamRoleCRN
roleName := roleCrn[strings.LastIndex(roleCrn, ":")+1:]

// TODO.S: update client
if strings.Contains(roleCrn, ":customRole:") {
iamPolicyManagementClient, err := meta.(conns.ClientSession).IAMPolicyManagementV1API()
if err == nil {
var resourceCRN string
if resourceKey.CRN != nil {
serviceName := strings.Split(*resourceKey.CRN, ":")
if len(serviceName) > 4 {
resourceCRN = serviceName[4]
}
}
listRoleOptions := &iampolicymanagementv1.ListRolesOptions{
AccountID: resourceKey.AccountID,
ServiceName: &resourceCRN,
iamPolicyManagementClient, err := meta.(conns.ClientSession).IAMPolicyManagementV1API()
if err == nil {
var resourceCRN string
if resourceKey.CRN != nil {
serviceName := strings.Split(*resourceKey.CRN, ":")
if len(serviceName) > 4 {
resourceCRN = serviceName[4]
}
roleList, _, err := iamPolicyManagementClient.ListRoles(listRoleOptions)
roles := roleList.CustomRoles
if err == nil && len(roles) > 0 {
for _, role := range roles {
if *role.Name == roleName {
customRoleName := role.DisplayName
d.Set("role", customRoleName)
}
}
listRoleOptions := &iampolicymanagementv1.ListRolesOptions{
AccountID: resourceKey.AccountID,
ServiceName: &resourceCRN,
}
roleList, resp, err := iamPolicyManagementClient.ListRoles(listRoleOptions)
roles := flex.MapRoleListToPolicyRoles(*roleList)
if err == nil && len(roles) > 0 {
for _, role := range roles {
if *role.RoleID == roleCrn {
RoleName := role.DisplayName
d.Set("role", RoleName)
}
}
}
} else {
d.Set("role", roleName)
if err != nil {
log.Printf("[ERROR] Error listing IAM Roles %s, %s", err, resp)
}
}
}

Expand Down

0 comments on commit 7e16792

Please sign in to comment.