Skip to content

Commit bb1b45a

Browse files
authored
Merge pull request #53 from atlanhq/ACTIV-559
Add support for creating Badges
2 parents 670d1ce + 8ab2f4f commit bb1b45a

22 files changed

+3862
-819
lines changed

.github/workflows/pytest.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [push, workflow_dispatch]
44

55
jobs:
66
build:
7-
7+
concurrency: integration_tests
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:

HISTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.0.32 (May 18, 2023)
2+
3+
* Add create method to Badge
4+
* Add integrations tests for CustomMetaData
5+
16
## 0.0.31 (May 15, 2023)
27

38
* Added the following classes to support lineage retrieval

pyatlan/cache/custom_metadata_cache.py

+48-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Optional
55

66
from pyatlan.client.atlan import AtlanClient
7-
from pyatlan.error import LogicError, NotFoundError
7+
from pyatlan.error import InvalidRequestError, LogicError, NotFoundError
88
from pyatlan.model.core import CustomMetadata
99
from pyatlan.model.enums import AtlanTypeCategory
1010
from 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):

pyatlan/cache/group_cache.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright 2022 Atlan Pte. Ltd.
3+
from typing import Optional
4+
5+
from pyatlan.client.atlan import AtlanClient
6+
7+
8+
class GroupCache:
9+
map_id_to_name: dict[str, str] = dict()
10+
map_name_to_id: dict[str, str] = dict()
11+
map_alias_to_id: dict[str, str] = dict()
12+
13+
@classmethod
14+
def _refresh_cache(cls) -> None:
15+
client = AtlanClient.get_default_client()
16+
if client is None:
17+
client = AtlanClient()
18+
groups = client.get_all_groups()
19+
if groups is not None:
20+
cls.map_id_to_name = {}
21+
cls.map_name_to_id = {}
22+
cls.map_alias_to_id = {}
23+
for group in groups:
24+
group_id = str(group.id)
25+
group_name = str(group.name)
26+
group_alias = str(group.alias)
27+
cls.map_id_to_name[group_id] = group_name
28+
cls.map_name_to_id[group_name] = group_id
29+
cls.map_alias_to_id[group_alias] = group_id
30+
31+
@classmethod
32+
def get_id_for_name(cls, name: str) -> Optional[str]:
33+
"""
34+
Translate the provided human-readable group name to its GUID.
35+
"""
36+
if group_id := cls.map_name_to_id.get(name):
37+
return group_id
38+
cls._refresh_cache()
39+
return cls.map_name_to_id.get(name)
40+
41+
@classmethod
42+
def get_id_for_alias(cls, alias: str) -> Optional[str]:
43+
"""
44+
Translate the provided alias to its GUID.
45+
"""
46+
if group_id := cls.map_alias_to_id.get(alias):
47+
return group_id
48+
cls._refresh_cache()
49+
return cls.map_alias_to_id.get(alias)
50+
51+
@classmethod
52+
def get_name_for_id(cls, idstr: str) -> Optional[str]:
53+
"""
54+
Translate the provided group GUID to the human-readable group name.
55+
"""
56+
if group_name := cls.map_id_to_name.get(idstr):
57+
return group_name
58+
cls._refresh_cache()
59+
return cls.map_id_to_name.get(idstr)

0 commit comments

Comments
 (0)