44from typing import Any , Optional
55
66from pyatlan .client .atlan import AtlanClient
7- from pyatlan .error import LogicError , NotFoundError
7+ from pyatlan .error import InvalidRequestError , LogicError , NotFoundError
88from pyatlan .model .core import CustomMetadata
99from pyatlan .model .enums import AtlanTypeCategory
1010from pyatlan .model .typedef import AttributeDef , CustomMetadataDef
@@ -72,7 +72,7 @@ def refresh_cache(cls) -> None:
7272 attr_id = str (attr .name )
7373 attr_name = str (attr .display_name )
7474 # Use a renamed attribute everywhere
75- attr_renamed = to_snake_case (attr_name . replace ( " " , "" ) )
75+ attr_renamed = to_snake_case (attr_name )
7676 cls .map_attr_id_to_name [type_id ][attr_id ] = attr_renamed
7777 if attr .options and attr .options .is_archived :
7878 cls .archived_attr_ids [attr_id ] = attr_renamed
@@ -91,26 +91,48 @@ def refresh_cache(cls) -> None:
9191 cls .types_by_asset [asset_type ].add (attrib_type )
9292
9393 @classmethod
94- def get_id_for_name (cls , name : str ) -> Optional [ str ] :
94+ def get_id_for_name (cls , name : str ) -> str :
9595 """
9696 Translate the provided human-readable custom metadata set name to its Atlan-internal ID string.
9797 """
98+ if name is None or not name .strip ():
99+ raise InvalidRequestError (
100+ message = "No name was provided when attempting to retrieve custom metadata." ,
101+ code = "ATLAN-PYTHON-404-008" ,
102+ param = "" ,
103+ )
98104 if cm_id := cls .map_name_to_id .get (name ):
99105 return cm_id
100106 # If not found, refresh the cache and look again (could be stale)
101107 cls .refresh_cache ()
102- return cls .map_name_to_id .get (name )
108+ if cm_id := cls .map_name_to_id .get (name ):
109+ return cm_id
110+ raise NotFoundError (
111+ message = f"Custom metadata with name { name } does not exist." ,
112+ code = "ATLAN-PYTHON-404-009" ,
113+ )
103114
104115 @classmethod
105- def get_name_for_id (cls , idstr : str ) -> Optional [ str ] :
116+ def get_name_for_id (cls , idstr : str ) -> str :
106117 """
107118 Translate the provided Atlan-internal custom metadata ID string to the human-readable custom metadata set name.
108119 """
120+ if idstr is None or not idstr .strip ():
121+ raise InvalidRequestError (
122+ message = "No ID was provided when attempting to retrieve custom metadata." ,
123+ code = "ATLAN-PYTHON-404-008" ,
124+ param = "" ,
125+ )
109126 if cm_name := cls .map_id_to_name .get (idstr ):
110127 return cm_name
111128 # If not found, refresh the cache and look again (could be stale)
112129 cls .refresh_cache ()
113- return cls .map_id_to_name .get (idstr )
130+ if cm_name := cls .map_id_to_name .get (idstr ):
131+ return cm_name
132+ raise NotFoundError (
133+ message = f"Custom metadata with ID { idstr } does not exist." ,
134+ code = "ATLAN-PYTHON-404-009" ,
135+ )
114136
115137 @classmethod
116138 def get_type_for_id (cls , idstr : str ) -> Optional [type ]:
@@ -152,23 +174,32 @@ def get_all_custom_attributes(
152174 return m
153175
154176 @classmethod
155- def get_attr_id_for_name (cls , set_name : str , attr_name : str ) -> Optional [ str ] :
177+ def get_attr_id_for_name (cls , set_name : str , attr_name : str ) -> str :
156178 """
157179 Translate the provided human-readable custom metadata set and attribute names to the Atlan-internal ID string
158180 for the attribute.
159181 """
160- attr_id = None
161- if set_id := cls .get_id_for_name (set_name ):
162- if sub_map := cls .map_attr_name_to_id .get (set_id ):
163- attr_id = sub_map .get (attr_name )
182+ set_id = cls .get_id_for_name (set_name )
183+ if sub_map := cls .map_attr_name_to_id .get (set_id ):
184+ attr_id = sub_map .get (attr_name )
164185 if attr_id :
165186 # If found, return straight away
166187 return attr_id
167- # Otherwise, refresh the cache and look again (could be stale)
168- cls .refresh_cache ()
169- if sub_map := cls .map_attr_name_to_id .get (set_id ):
170- return sub_map .get (attr_name )
171- return None
188+ # Otherwise, refresh the cache and look again (could be stale)
189+ cls .refresh_cache ()
190+ if sub_map := cls .map_attr_name_to_id .get (set_id ):
191+ attr_id = sub_map .get (attr_name )
192+ if attr_id :
193+ # If found, return straight away
194+ return attr_id
195+ raise NotFoundError (
196+ message = f"Custom metadata property with name { attr_name } does not exist in custom metadata { set_name } ." ,
197+ code = "ATLAN-PYTHON-404-009" ,
198+ )
199+ raise NotFoundError (
200+ message = f"Custom metadata with ID { set_id } does not exist." ,
201+ code = "ATLAN-PYTHON-404-009" ,
202+ )
172203
173204 @classmethod
174205 def get_attr_name_for_id (cls , set_id : str , attr_id : str ) -> Optional [str ]:
@@ -177,8 +208,7 @@ def get_attr_name_for_id(cls, set_id: str, attr_id: str) -> Optional[str]:
177208 for the attribute.
178209 """
179210 if sub_map := cls .map_attr_id_to_name .get (set_id ):
180- attr_name = sub_map .get (attr_id )
181- if attr_name :
211+ if attr_name := sub_map .get (attr_id ):
182212 return attr_name
183213 cls .refresh_cache ()
184214 if sub_map := cls .map_attr_id_to_name .get (set_id ):
0 commit comments