def ensure_user_role_exists(keystone, user_name, tenant_name, role_name, check_mode):
 user = get_user(keystone, user_name)
 tenant = get_tenant(keystone, tenant_name)
 roles = [x for x in keystone.roles.roles_for_user(user, tenant) if (x.name == role_name)]
 count = len(roles)
 if (count == 1):
  role = roles[0]
  return (False, role.id)
 elif (count > 1):
  raise ValueError(('%d roles with name %s' % (count, role_name)))
 if check_mode:
  return (True, None)
 try:
  role = get_role(keystone, role_name)
 except KeyError:
  role = keystone.roles.create(role_name)
 keystone.roles.add_user_role(user, role, tenant)
 return (True, role.id)

def ensure_user_role_exists ( keystone , user_name , tenant_name , role_name , check_mode ) : user = get_user ( keystone , user_name ) tenant = get_tenant ( keystone , tenant_name ) roles = [ x for x in keystone . roles . roles_for_user ( user , tenant ) if ( x . name == role_name ) ] count = len ( roles ) if ( count == 1 ) : role = roles [ 0 ] return ( False , role . id ) elif ( count > 1 ) : raise ValueError ( ( '%d roles with name %s' % ( count , role_name ) ) ) if check_mode : return ( True , None ) try : role = get_role ( keystone , role_name ) except KeyError : role = keystone . roles . create ( role_name ) keystone . roles . add_user_role ( user , role , tenant ) return ( True , role . id )
check if role exists return if a new role was created or if the role was newly assigned to the user for the tenant .