[docs]classRoleCache:""" Lazily-loaded cache for translating Atlan-internal roles into their various IDs. """def__init__(self,client:AtlanClient):self.client:AtlanClient=clientself.cache_by_id:Dict[str,AtlanRole]={}self.map_id_to_name:Dict[str,str]={}self.map_name_to_id:Dict[str,str]={}self.lock:Lock=Lock()
[docs]defget_id_for_name(self,name:str)->Optional[str]:""" Translate the provided human-readable role name to its GUID. :param name: human-readable name of the role :returns: unique identifier (GUID) of the role """returnself._get_id_for_name(name=name)
[docs]defget_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided role GUID to the human-readable role name. :param idstr: unique identifier (GUID) of the role :returns: human-readable name of the role """returnself._get_name_for_id(idstr=idstr)
[docs]defvalidate_idstrs(self,idstrs:Iterable[str]):""" Validate that the given role GUIDs are valid. A ValueError will be raised in any are not. :param idstrs: a collection of unique identifiers (GUID) of the roles to be checked """returnself._validate_idstrs(idstrs=idstrs)
def_refresh_cache(self)->None:withself.lock:response=self.client.role.get(limit=100,post_filter='{"name":{"$ilike":"$%"}}')ifnotresponse:returnself.cache_by_id={}self.map_id_to_name={}self.map_name_to_id={}forroleinresponse.records:role_id=role.idrole_name=role.nameself.cache_by_id[role_id]=roleself.map_id_to_name[role_id]=role_nameself.map_name_to_id[role_name]=role_iddef_get_id_for_name(self,name:str)->Optional[str]:""" Translate the provided human-readable role name to its GUID. :param name: human-readable name of the role :returns: unique identifier (GUID) of the role """ifrole_id:=self.map_name_to_id.get(name):returnrole_idself._refresh_cache()returnself.map_name_to_id.get(name)def_get_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided role GUID to the human-readable role name. :param idstr: unique identifier (GUID) of the role :returns: human-readable name of the role """ifrole_name:=self.map_id_to_name.get(idstr):returnrole_nameself._refresh_cache()returnself.map_id_to_name.get(idstr)def_validate_idstrs(self,idstrs:Iterable[str]):""" Validate that the given role GUIDs are valid. A ValueError will be raised in any are not. :param idstrs: a collection of unique identifiers (GUID) of the roles to be checked """forrole_idinidstrs:ifnotself.get_name_for_id(role_id):raiseValueError(f"Provided role ID {role_id} was not found in Atlan.")