4
4
from typing import Any , Optional
5
5
6
6
from pyatlan .client .atlan import AtlanClient
7
- from pyatlan .error import LogicError , NotFoundError
7
+ from pyatlan .error import InvalidRequestError , LogicError , NotFoundError
8
8
from pyatlan .model .core import CustomMetadata
9
9
from pyatlan .model .enums import AtlanTypeCategory
10
10
from pyatlan .model .typedef import AttributeDef , CustomMetadataDef
@@ -72,7 +72,7 @@ def refresh_cache(cls) -> None:
72
72
attr_id = str (attr .name )
73
73
attr_name = str (attr .display_name )
74
74
# Use a renamed attribute everywhere
75
- attr_renamed = to_snake_case (attr_name . replace ( " " , "" ) )
75
+ attr_renamed = to_snake_case (attr_name )
76
76
cls .map_attr_id_to_name [type_id ][attr_id ] = attr_renamed
77
77
if attr .options and attr .options .is_archived :
78
78
cls .archived_attr_ids [attr_id ] = attr_renamed
@@ -91,26 +91,48 @@ def refresh_cache(cls) -> None:
91
91
cls .types_by_asset [asset_type ].add (attrib_type )
92
92
93
93
@classmethod
94
- def get_id_for_name (cls , name : str ) -> Optional [ str ] :
94
+ def get_id_for_name (cls , name : str ) -> str :
95
95
"""
96
96
Translate the provided human-readable custom metadata set name to its Atlan-internal ID string.
97
97
"""
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
+ )
98
104
if cm_id := cls .map_name_to_id .get (name ):
99
105
return cm_id
100
106
# If not found, refresh the cache and look again (could be stale)
101
107
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
+ )
103
114
104
115
@classmethod
105
- def get_name_for_id (cls , idstr : str ) -> Optional [ str ] :
116
+ def get_name_for_id (cls , idstr : str ) -> str :
106
117
"""
107
118
Translate the provided Atlan-internal custom metadata ID string to the human-readable custom metadata set name.
108
119
"""
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
+ )
109
126
if cm_name := cls .map_id_to_name .get (idstr ):
110
127
return cm_name
111
128
# If not found, refresh the cache and look again (could be stale)
112
129
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
+ )
114
136
115
137
@classmethod
116
138
def get_type_for_id (cls , idstr : str ) -> Optional [type ]:
@@ -152,23 +174,32 @@ def get_all_custom_attributes(
152
174
return m
153
175
154
176
@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 :
156
178
"""
157
179
Translate the provided human-readable custom metadata set and attribute names to the Atlan-internal ID string
158
180
for the attribute.
159
181
"""
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 )
164
185
if attr_id :
165
186
# If found, return straight away
166
187
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
+ )
172
203
173
204
@classmethod
174
205
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]:
177
208
for the attribute.
178
209
"""
179
210
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 ):
182
212
return attr_name
183
213
cls .refresh_cache ()
184
214
if sub_map := cls .map_attr_id_to_name .get (set_id ):
0 commit comments