Skip to content

Commit 8edd492

Browse files
committed
Added qualified_name parameter to AtlanClient.append_terms to allow use of either guid or qualified_name.
1 parent 871d4f6 commit 8edd492

3 files changed

Lines changed: 69 additions & 24 deletions

File tree

pyatlan/client/atlan.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ def get_asset_by_qualified_name(
313313
raw_json["entity"]["attributes"].update(
314314
raw_json["entity"]["relationshipAttributes"]
315315
)
316-
asset = AssetResponse[A](**raw_json).entity
317-
asset.is_incomplete = False
316+
asset = self.handle_relationships(raw_json)
318317
if not isinstance(asset, asset_type):
319318
raise NotFoundError(
320319
message=f"Asset with qualifiedName {qualified_name} "
@@ -345,16 +344,7 @@ def get_asset_by_guid(
345344
GET_ENTITY_BY_GUID.format_path_with_params(guid),
346345
query_params,
347346
)
348-
if (
349-
"relationshipAttributes" in raw_json["entity"]
350-
and raw_json["entity"]["relationshipAttributes"]
351-
):
352-
raw_json["entity"]["attributes"].update(
353-
raw_json["entity"]["relationshipAttributes"]
354-
)
355-
raw_json["entity"]["relationshipAttributes"] = {}
356-
asset = AssetResponse[A](**raw_json).entity
357-
asset.is_incomplete = False
347+
asset = self.handle_relationships(raw_json)
358348
if not isinstance(asset, asset_type):
359349
raise NotFoundError(
360350
message=f"Asset with GUID {guid} is not of the type requested: {asset_type.__name__}.",
@@ -366,6 +356,19 @@ def get_asset_by_guid(
366356
raise NotFoundError(message=ae.user_message, code=ae.code) from ae
367357
raise ae
368358

359+
def handle_relationships(self, raw_json):
360+
if (
361+
"relationshipAttributes" in raw_json["entity"]
362+
and raw_json["entity"]["relationshipAttributes"]
363+
):
364+
raw_json["entity"]["attributes"].update(
365+
raw_json["entity"]["relationshipAttributes"]
366+
)
367+
raw_json["entity"]["relationshipAttributes"] = {}
368+
asset = AssetResponse[A](**raw_json).entity
369+
asset.is_incomplete = False
370+
return asset
371+
369372
@validate_arguments()
370373
def retrieve_minimal(self, guid: str, asset_type: Type[A]) -> A:
371374
return self.get_asset_by_guid(
@@ -605,9 +608,24 @@ def replace_custom_metadata(self, guid: str, custom_metadata: CustomMetadata):
605608

606609
@validate_arguments()
607610
def append_terms(
608-
self, guid: str, asset_type: Type[A], terms: list[AtlasGlossaryTerm]
611+
self,
612+
asset_type: Type[A],
613+
terms: list[AtlasGlossaryTerm],
614+
guid: Optional[str] = None,
615+
qualified_name: Optional[str] = None,
609616
) -> A:
610-
asset = self.get_asset_by_guid(guid=guid, asset_type=asset_type)
617+
if guid:
618+
if qualified_name:
619+
raise ValueError(
620+
"Either guid or qualified_name can be be specified not both"
621+
)
622+
asset = self.get_asset_by_guid(guid=guid, asset_type=asset_type)
623+
elif qualified_name:
624+
asset = self.get_asset_by_qualified_name(
625+
qualified_name=qualified_name, asset_type=asset_type
626+
)
627+
else:
628+
raise ValueError("Either guid or qualified name must be specified")
611629
if not terms:
612630
return asset
613631
replacement_terms: list[AtlasGlossaryTerm] = []

tests/integration/test_client.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def database(
3333
) -> Generator[Database, None, None]:
3434

3535
database = Database.create(
36-
name="Integration_Test_Entity_DB",
36+
name=f"Integration_Test_Entity_DB{next(iter_count)}",
3737
connection_qualified_name=connection.attributes.qualified_name,
3838
)
3939
database = client.upsert(database).assets_created(Database)[0]
@@ -75,7 +75,7 @@ def test_register_client():
7575
assert AtlanClient.get_default_client() == client
7676

7777

78-
def test_append_terms(
78+
def test_append_terms_with_guid(
7979
client: AtlanClient,
8080
make_term: Callable[[str], AtlasGlossaryTerm],
8181
database: Database,
@@ -90,3 +90,20 @@ def test_append_terms(
9090
database = client.get_asset_by_guid(guid=database.guid, asset_type=Database)
9191
assert len(database.terms) == 1
9292
assert database.terms[0].guid == term.guid
93+
94+
95+
def test_append_terms_with_qualified_name(
96+
client: AtlanClient,
97+
make_term: Callable[[str], AtlasGlossaryTerm],
98+
database: Database,
99+
):
100+
term = make_term("Term1")
101+
102+
assert (
103+
database := client.append_terms(
104+
qualified_name=database.qualified_name, asset_type=Database, terms=[term]
105+
)
106+
)
107+
database = client.get_asset_by_guid(guid=database.guid, asset_type=Database)
108+
assert len(database.terms) == 1
109+
assert database.terms[0].guid == term.guid

tests/unit/test_client.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,41 @@
99

1010

1111
@pytest.mark.parametrize(
12-
"guid, asset_type, terms, message",
12+
"guid, qualified_name, asset_type, terms, message",
1313
[
1414
(
1515
"123",
16+
None,
1617
Table,
1718
None,
18-
"1 validation error for AppendTerms\\nterms\\n none is not an allowed value "
19-
"\(type=type_error.none.not_allowed\)", # noqa: W605
19+
"1 validation error for AppendTerms\\nterms\\n none is not an allowed value ",
2020
),
2121
(
22+
None,
2223
None,
2324
Table,
2425
[AtlasGlossaryTerm()],
25-
"1 validation error for AppendTerms\\nguid\\n none is not an allowed value "
26-
"\(type=type_error.none.not_allowed\)", # noqa: W605
26+
"Either guid or qualified name must be specified",
2727
),
2828
(
2929
"123",
3030
None,
31+
None,
3132
[AtlasGlossaryTerm()],
32-
"1 validation error for AppendTerms\\nasset_type\\n none is not an allowed value "
33-
"\(type=type_error.none.not_allowed\)", # noqa: W605
33+
"1 validation error for AppendTerms\\nasset_type\\n none is not an allowed value ",
34+
),
35+
(
36+
"123",
37+
"default/abc",
38+
Table,
39+
[AtlasGlossaryTerm()],
40+
"Either guid or qualified_name can be be specified not both",
3441
),
3542
],
3643
)
3744
def test_append_terms_with_invalid_parameter_raises_valueerror(
3845
guid,
46+
qualified_name,
3947
asset_type,
4048
terms,
4149
message,
@@ -46,7 +54,9 @@ def test_append_terms_with_invalid_parameter_raises_valueerror(
4654
client = AtlanClient()
4755

4856
with pytest.raises(ValueError, match=message):
49-
client.append_terms(guid=guid, asset_type=asset_type, terms=terms)
57+
client.append_terms(
58+
guid=guid, qualified_name=qualified_name, asset_type=asset_type, terms=terms
59+
)
5060

5161

5262
def test_append_with_valid_guid_and_no_terms_returns_asset(monkeypatch):

0 commit comments

Comments
 (0)