Skip to content

Commit 1270520

Browse files
committed
Add get_custom_metadata to CustomMetadataCache
1 parent f363382 commit 1270520

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ wheels/
5050
.installed.cfg
5151
*.egg
5252
*.DS_Store
53+
examples/

pyatlan/cache/custom_metadata_cache.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# Copyright 2022 Atlan Pte. Ltd.
33
import json
4-
from typing import Optional
4+
from typing import Any, Optional
55

66
from pyatlan.client.atlan import AtlanClient
77
from pyatlan.error import LogicError, NotFoundError
8+
from pyatlan.model.core import BusinessAttributes
89
from pyatlan.model.enums import AtlanTypeCategory
910
from pyatlan.model.typedef import AttributeDef, CustomMetadataDef
1011

@@ -202,3 +203,30 @@ def get_attributes_for_search_results(cls, set_name: str) -> Optional[list[str]]
202203
cls._refresh_cache()
203204
return cls._get_attributes_for_search_results(set_id)
204205
return None
206+
207+
@classmethod
208+
def get_custom_metadata(
209+
cls,
210+
name: str,
211+
asset_type: type,
212+
business_attributes: Optional[dict[str, Any]] = None,
213+
) -> BusinessAttributes:
214+
type_name = asset_type.__name__
215+
ba_id = cls.get_id_for_name(name)
216+
if ba_id is None:
217+
raise ValueError(f"No custom metadata with the name: {name} exist")
218+
for a_type in CustomMetadataCache.types_by_asset[type_name]:
219+
if (
220+
hasattr(a_type, "_meta_data_type_name")
221+
and a_type._meta_data_type_name == name
222+
):
223+
break
224+
else:
225+
raise ValueError(f"Custom metadata {name} is not applicable to {type_name}")
226+
if ba_type := CustomMetadataCache.get_type_for_id(ba_id):
227+
return (
228+
ba_type(business_attributes[ba_id])
229+
if business_attributes and ba_id in business_attributes
230+
else ba_type()
231+
)
232+
raise ValueError(f"Custom metadata {name} is not applicable to {type_name}")

tests/integration/custom_metadata_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,17 @@ def test_custom_metadata_has_human_readable_properties(client: AtlanClient):
116116
assert anomalo.validation_rules_details is None
117117
monte_carlo = table.get_business_attributes("Monte Carlo")
118118
assert monte_carlo is not None
119+
120+
121+
def test_get_custom_metadata():
122+
custom_metadata = CustomMetadataCache.get_custom_metadata(
123+
name="RACI", asset_type=Table
124+
)
125+
assert custom_metadata is not None
126+
127+
128+
def test_get_custom_metadata_when_name_is_invalid_then_raises_value_error():
129+
with pytest.raises(
130+
ValueError, match="No custom metadata with the name: Bogs exist"
131+
):
132+
CustomMetadataCache.get_custom_metadata(name="Bogs", asset_type=Table)

0 commit comments

Comments
 (0)