GCP Get Custom IAM role permission Python

623 views
Skip to first unread message

poornachand sounderrajan

unread,
Apr 20, 2020, 1:08:17 PM4/20/20
to Google Cloud Developers

 I am using python code to get the permissions present in my IAM role. Even though, I pass the parameter as per the documentation https://cloud.google.com/iam/docs/creating-custom-roles#iam-custom-roles-get-metadata-python. It is throwing me the below error. role['name'] assumes the role id from the IAM role. I am not able to figure it out from the google docs. Any help would be appreciated.


TypeError: Parameter "name" value "roles/projects/my-new-project-273607/roles/CustomRole" does not match the pattern "^roles/[^/]+$"

role = service.roles().get(name='roles/' + role['name']).execute()
for permission in role['includedPermissions']:
    print(permission)
print('permissions:' + role['includedPermissions'])

waela

unread,
Apr 21, 2020, 7:05:26 AM4/21/20
to Google Cloud Developers

I think you're adding the 'roles/' when you do not have to as it's kinda formatted within the library.

generically speaking the name has the following value name='projects/' + project + '/roles/' + name)

In the error you can see there is an extra "roles/" at the bge

The example code to get role Metadata as in here[1], is the following (pay attention to the .get() part)

def get_role(name):
    """Gets a role."""

    # pylint: disable=no-member
    role = service.roles().get(name=name).execute()
    print(role['name'])
    for permission in role['includedPermissions']:
        print(permission)
Reply all
Reply to author
Forward
0 new messages