[docs]classGroupCache:""" Lazily-loaded cache for translating Atlan-internal groups into their various IDs. """caches:Dict[int,"GroupCache"]={}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_alias_to_id:Dict[str,str]={}self.lock:Lock=Lock()
[docs]defget_id_for_name(self,name:str)->Optional[str]:""" Translate the provided internal group name to its GUID. :param name: human-readable name of the group :returns: unique identifier (GUID) of the group """returnself._get_id_for_name(name=name)
[docs]defget_id_for_alias(self,alias:str)->Optional[str]:""" Translate the provided human-readable group name to its GUID. :param alias: name of the group as it appears in the UI :returns: unique identifier (GUID) of the group """returnself._get_id_for_alias(alias=alias)
[docs]defget_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided group GUID to the internal group name. :param idstr: unique identifier (GUID) of the group :returns: human-readable name of the group """returnself._get_name_for_id(idstr=idstr)
[docs]defvalidate_aliases(self,aliases:Iterable[str]):""" Validate that the given (internal) group names are valid. A ValueError will be raised in any are not. :param aliases: a collection of (internal) group names to be checked """returnself._validate_aliases(aliases)
def_refresh_cache(self)->None:withself.lock:groups=self.client.group.get_all()ifnotgroups:returnself.map_id_to_name={}self.map_name_to_id={}self.map_alias_to_id={}forgroupingroups:group_id=str(group.id)group_name=str(group.name)group_alias=str(group.alias)self.map_id_to_name[group_id]=group_nameself.map_name_to_id[group_name]=group_idself.map_alias_to_id[group_alias]=group_iddef_get_id_for_name(self,name:str)->Optional[str]:""" Translate the provided internal group name to its GUID. :param name: internal name of the group :returns: unique identifier (GUID) of the group """ifgroup_id:=self.map_name_to_id.get(name):returngroup_idself._refresh_cache()returnself.map_name_to_id.get(name)def_get_id_for_alias(self,alias:str)->Optional[str]:""" Translate the provided human-readable group name to its GUID. :param alias: name of the group as it appears in the UI :returns: unique identifier (GUID) of the group """ifgroup_id:=self.map_alias_to_id.get(alias):returngroup_idself._refresh_cache()returnself.map_alias_to_id.get(alias)def_get_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided group GUID to the internal group name. :param idstr: unique identifier (GUID) of the group :returns: internal name of the group """ifgroup_name:=self.map_id_to_name.get(idstr):returngroup_nameself._refresh_cache()returnself.map_id_to_name.get(idstr)def_validate_aliases(self,aliases:Iterable[str]):""" Validate that the given (internal) group names are valid. A ValueError will be raised in any are not. :param aliases: a collection of (internal) group names to be checked """forgroup_aliasinaliases:ifnotself.get_id_for_name(group_alias):raiseValueError(f"Provided group name {group_alias} was not found in Atlan.")