Skip to content

Commit 0940169

Browse files
committed
[change] AtlanTag.of(): update to use client instance used for translating source-specific details
1 parent ac2c815 commit 0940169

3 files changed

Lines changed: 15 additions & 16 deletions

File tree

pyatlan/model/core.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from pydantic.v1.generics import GenericModel
2424

25+
from pyatlan.errors import ErrorCode
2526
from pyatlan.model.constants import DELETED_, DELETED_SENTINEL
2627
from pyatlan.model.enums import AnnouncementType, EntityStatus, SaveSemantic
2728
from pyatlan.model.retranslators import AtlanTagRetranslator
@@ -51,11 +52,11 @@ def __init__(self, display_text: str):
5152
# ):
5253
# raise ValueError(f"{display_text} is not a valid Classification")
5354
self._display_text = display_text
54-
self._id = id
55+
# self._id = id
5556

56-
@property
57-
def id(self):
58-
return self._id
57+
# @property
58+
# def id(self):
59+
# return self._id
5960

6061
@classmethod
6162
def get_deleted_sentinel(cls) -> "AtlanTagName":
@@ -319,17 +320,12 @@ class Config:
319320
)
320321

321322
attributes: Optional[Dict[str, Any]] = None
323+
tag_id: Optional[str] = Field(default=True, exclude=True)
322324

323325
# @property
324326
# def source_tag_attachements(self) -> List[SourceTagAttachment]:
325327
# return self._source_tag_attachements
326328

327-
# @validator("type_name", pre=True)
328-
# def type_name_is_tag_name(cls, value):
329-
# if isinstance(value, AtlanTagName):
330-
# return value
331-
# return AtlanTagName._convert_to_display_text(value)
332-
333329
# def __init__(self, *args, **kwargs):
334330
# from pyatlan.client.atlan import AtlanClient
335331

@@ -351,27 +347,28 @@ def of(
351347
atlan_tag_name: AtlanTagName,
352348
entity_guid: Optional[str] = None,
353349
source_tag_attachment: Optional[SourceTagAttachment] = None,
350+
client: Optional[AtlanClient] = None,
354351
) -> AtlanTag:
355-
from pyatlan.client.atlan import AtlanClient
356-
357352
"""
358353
Construct an Atlan tag assignment for a specific entity.
359354
360355
:param atlan_tag_name: human-readable name of the Atlan tag
361356
:param entity_guid: unique identifier (GUID) of the entity to which the Atlan tag is to be assigned
362357
:param source_tag_attachment: (optional) source-specific details for the tag
358+
:param client: (optional) client instance used for translating source-specific details
363359
:return: an Atlan tag assignment with default settings for propagation and a specific entity assignment
360+
:raises InvalidRequestError: if client is not provided and source_tag_attachment is specified
364361
"""
365362
tag = AtlanTag(type_name=atlan_tag_name)
366363
if entity_guid:
367364
tag.entity_guid = entity_guid
368365
tag.entity_status = EntityStatus.ACTIVE
369366
if source_tag_attachment:
367+
if not client:
368+
raise ErrorCode.NO_ATLAN_CLIENT.exception_with_parameters()
369+
tag_id = client.atlan_tag_cache.get_id_for_name(str(atlan_tag_name))
370370
source_tag_attr_id = (
371-
AtlanClient.get_current_client().atlan_tag_cache.get_source_tags_attr_id(
372-
atlan_tag_name.id
373-
)
374-
or ""
371+
client.atlan_tag_cache.get_source_tags_attr_id(tag_id) or ""
375372
)
376373
tag.attributes = {source_tag_attr_id: [source_tag_attachment]}
377374
tag.source_tag_attachements.append(source_tag_attachment)

pyatlan/model/translators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def translate(self, data: Dict[str, Any]) -> Dict[str, Any]:
4747
if not tag_name:
4848
return
4949
classification[self._TYPE_NAME] = tag_name
50+
classification["tag_id"] = tag_id
5051
# Check if the tag is a source tag (in that case tag has "attributes")
5152
attr_id = self.client.atlan_tag_cache.get_source_tags_attr_id(
5253
tag_id

tests/integration/test_index_search.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def test_source_tag_assign_with_value(client: AtlanClient, table: Table):
229229
SourceTagAttachmentValue(tag_attachment_value="Not Restricted")
230230
],
231231
),
232+
client=client,
232233
),
233234
]
234235
response = client.asset.save(to_update, replace_atlan_tags=True)

0 commit comments

Comments
 (0)