[docs]classUserCache:""" Lazily-loaded cache for translating Atlan-internal users into their various IDs. """def__init__(self,client:AtlanClient):self.client:AtlanClient=clientself.map_id_to_name:Dict[str,str]={}self.map_name_to_id:Dict[str,str]={}self.map_email_to_id:Dict[str,str]={}self.lock:Lock=Lock()
[docs]defget_id_for_name(self,name:str)->Optional[str]:""" Translate the provided human-readable username to its GUID. :param name: human-readable name of the user :returns: unique identifier (GUID) of the user """returnself._get_id_for_name(name=name)
[docs]defget_id_for_email(self,email:str)->Optional[str]:""" Translate the provided email to its GUID. :param email: email address of the user :returns: unique identifier (GUID) of the user """returnself._get_id_for_email(email=email)
[docs]defget_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided user GUID to the human-readable username. :param idstr: unique identifier (GUID) of the user :returns: username of the user """returnself._get_name_for_id(idstr=idstr)
[docs]defvalidate_names(self,names:Iterable[str]):""" Validate that the given human-readable usernames are valid. A ValueError will be raised in any are not. :param names: a collection of usernames to be checked """returnself._validate_names(names)
def_refresh_cache(self)->None:withself.lock:users=self.client.user.get_all()ifnotusers:returnself.map_id_to_name={}self.map_name_to_id={}self.map_email_to_id={}foruserinusers:user_id=str(user.id)username=str(user.username)user_email=str(user.email)self.map_id_to_name[user_id]=usernameself.map_name_to_id[username]=user_idself.map_email_to_id[user_email]=user_iddef_get_id_for_name(self,name:str)->Optional[str]:""" Translate the provided human-readable username to its GUID. :param name: human-readable name of the user :returns: unique identifier (GUID) of the user """ifuser_id:=self.map_name_to_id.get(name):returnuser_id# If we are translating an API token,# short-circuit any further cache refreshifname.startswith(SERVICE_ACCOUNT_):token=self.client.token.get_by_id(client_id=name)iftokenandtoken.guid:self.map_name_to_id[name]=token.guidreturntoken.guidelse:raiseErrorCode.API_TOKEN_NOT_FOUND_BY_NAME.exception_with_parameters(name)self._refresh_cache()returnself.map_name_to_id.get(name)def_get_id_for_email(self,email:str)->Optional[str]:""" Translate the provided email to its GUID. :param email: email address of the user :returns: unique identifier (GUID) of the user """ifuser_id:=self.map_email_to_id.get(email):returnuser_idself._refresh_cache()returnself.map_email_to_id.get(email)def_get_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided user GUID to the human-readable username. :param idstr: unique identifier (GUID) of the user :returns: username of the user """ifusername:=self.map_id_to_name.get(idstr):returnusername# If the username isn't found, check if it is an API tokentoken=self.client.token.get_by_guid(guid=idstr)iftokenandtoken.client_id:returntoken.usernameelse:self._refresh_cache()returnself.map_id_to_name.get(idstr)def_validate_names(self,names:Iterable[str]):""" Validate that the given human-readable usernames are valid. A ValueError will be raised in any are not. :param names: a collection of usernames to be checked """forusernameinnames:ifnotself.get_id_for_name(username)andnotself.client.token.get_by_id(username):raiseValueError(f"Provided username {username} was not found in Atlan.")