Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.

Commit a45923b

Browse files
committed
Updates for SDK v0.6.2
Signed-off-by: Christopher Grote <cmgrote@users.noreply.github.com>
1 parent a153896 commit a45923b

File tree

4 files changed

+66
-50
lines changed

4 files changed

+66
-50
lines changed

custom_metadata/update_cm_on_assets.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pyatlan.client.atlan import AtlanClient
77
from pyatlan.model.assets import Asset
88
from pyatlan.model.enums import AtlanConnectorType
9-
from pyatlan.model.search import DSL, IndexSearchRequest, TermAttributes, Terms
9+
from pyatlan.model.fluent_search import FluentSearch
1010
from pyatlan.utils import get_logger
1111

1212
CUSTOM_METADATA_NAME = "Quality Data"
@@ -19,7 +19,7 @@ def find_asset(
1919
connection_name: str,
2020
asset_name: str,
2121
attributes: Optional[list[str]] = None,
22-
) -> Asset:
22+
) -> Optional[Asset]:
2323
"""
2424
Given a connector type and otherwise-qualified name (not including the
2525
connection portion of the qualified_name), finds and returns the asset in
@@ -39,14 +39,14 @@ def find_asset(
3939
qualified_names = []
4040
for connection in connections:
4141
qualified_names.append(f"{connection.qualified_name}/{asset_name}")
42-
by_name = Terms(field=TermAttributes.QUALIFIED_NAME.value, values=qualified_names)
43-
dsl = DSL(query=by_name)
44-
search_request = IndexSearchRequest(
45-
dsl=dsl,
46-
attributes=attributes,
47-
)
48-
results = client.search(search_request)
49-
return results.current_page()[0] if results else None
42+
search_request = (
43+
FluentSearch(_includes_on_results=attributes).where(
44+
Asset.QUALIFIED_NAME.within(qualified_names)
45+
)
46+
).to_request()
47+
if results := client.search(search_request):
48+
return results.current_page()[0]
49+
return None
5050

5151

5252
def update_custom_metadata(
@@ -80,29 +80,33 @@ def update_custom_metadata(
8080

8181

8282
def main():
83-
asset = find_asset(
83+
if asset := find_asset(
8484
connector_type=AtlanConnectorType.SNOWFLAKE,
8585
connection_name="development",
8686
asset_name="RAW/WIDEWORLDIMPORTERS_PURCHASING/SUPPLIERS",
8787
attributes=CustomMetadataCache.get_attributes_for_search_results(
8888
CUSTOM_METADATA_NAME
8989
),
90-
)
91-
logger.info(f"Found asset: {asset}")
92-
updated = update_custom_metadata(
93-
asset=asset,
94-
rating="OK",
95-
passed=10,
96-
failed=5,
97-
reports=["https://www.example.com", "https://www.atlan.com"],
98-
)
99-
# Note that the updated asset will NOT show the custom metadata, if you want
100-
# to see the custom metadata you need to re-retrieve the asset itself
101-
assert updated # noqa: S101
102-
result = client.get_asset_by_guid(
103-
guid=updated.guid, asset_type=type(updated), ignore_relationships=True
104-
)
105-
logger.info(f"Asset's custom metadata was updated: {result}")
90+
):
91+
logger.info(f"Found asset: {asset}")
92+
updated = update_custom_metadata(
93+
asset=asset,
94+
rating="OK",
95+
passed=10,
96+
failed=5,
97+
reports=["https://www.example.com", "https://www.atlan.com"],
98+
)
99+
# Note that the updated asset will NOT show the custom metadata, if you want
100+
# to see the custom metadata you need to re-retrieve the asset itself
101+
assert updated # noqa: S101
102+
result = client.get_asset_by_guid(
103+
guid=updated.guid, asset_type=type(updated), ignore_relationships=True
104+
)
105+
logger.info(f"Asset's custom metadata was updated: {result}")
106+
else:
107+
logger.warn(
108+
"Unable to find asset: (development)/RAW/WIDEWORLDIMPORTERS_PURCHASING/SUPPLIERS"
109+
)
106110

107111

108112
if __name__ == "__main__":

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pyatlan>=0.4.0
1+
pyatlan>=0.6.2

search/and_star_assets.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# Copyright 2023 Atlan Pte. Ltd.
33
import time
44

5-
from pyatlan.client.atlan import AtlanClient, IndexSearchRequest
5+
from pyatlan.client.atlan import AtlanClient
66
from pyatlan.model.assets import Asset, AtlasGlossaryTerm
7-
from pyatlan.model.search import DSL, Term
7+
from pyatlan.model.fluent_search import FluentSearch
88
from pyatlan.model.structs import StarredDetails
99
from pyatlan.utils import get_logger
1010

@@ -20,14 +20,17 @@ def find_assets() -> AtlanClient.SearchResults:
2020
2121
:returns: results of the search
2222
"""
23-
are_active = Term.with_state("ACTIVE")
24-
are_term = Term.with_type_name("AtlasGlossaryTerm")
2523
glossary = client.find_glossary_by_name("Metrics")
26-
in_glossary = Term.with_glossary(glossary.qualified_name)
27-
dsl = DSL(query=are_active + are_term + in_glossary, from_=0, size=100)
28-
search_request = IndexSearchRequest(
29-
dsl=dsl, attributes=["starredDetailsList", "starredBy", "anchor"]
30-
)
24+
search_request = (
25+
FluentSearch()
26+
.where(FluentSearch.active_assets())
27+
.where(FluentSearch.asset_type(AtlasGlossaryTerm))
28+
.where(AtlasGlossaryTerm.ANCHOR.eq(glossary.qualified_name))
29+
.page_size(100)
30+
.include_on_results(Asset.STARRED_DETAILS_LIST)
31+
.include_on_results(Asset.STARRED_BY)
32+
.include_on_results(AtlasGlossaryTerm.ANCHOR)
33+
).to_request()
3134
return client.search(search_request)
3235

3336

search/and_traverse_lineage.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# SPDX-License-Identifier: Apache-2.0
22
# Copyright 2023 Atlan Pte. Ltd.
3-
from pyatlan.client.atlan import AtlanClient, IndexSearchRequest
3+
from pyatlan.client.atlan import AtlanClient
44
from pyatlan.model.assets import Asset, SigmaWorkbook
5-
from pyatlan.model.enums import AtlanComparisonOperator, LineageDirection
5+
from pyatlan.model.enums import (
6+
AtlanComparisonOperator,
7+
CertificateStatus,
8+
LineageDirection,
9+
)
10+
from pyatlan.model.fluent_search import FluentSearch
611
from pyatlan.model.lineage import EntityFilter, FilterList, LineageListRequest
7-
from pyatlan.model.search import DSL, Term
812
from pyatlan.utils import get_logger
913

1014
client = AtlanClient()
1115
logger = get_logger(level="INFO")
1216

1317

14-
def find_all(type_name: str) -> AtlanClient.SearchResults:
18+
def find_all(asset_type: type) -> AtlanClient.SearchResults:
1519
"""
1620
This query will find all assets of the specified type
1721
that are active (not archived or soft-deleted).
1822
19-
:param type_name: type of assets to find
23+
:param asset_type: type of assets to find
2024
:returns: results of the search
2125
"""
22-
are_active = Term.with_state("ACTIVE")
23-
are_term = Term.with_type_name(type_name)
24-
dsl = DSL(query=are_active + are_term, from_=0, size=100)
25-
search_request = IndexSearchRequest(dsl=dsl)
26+
search_request = (
27+
FluentSearch()
28+
.where(FluentSearch.asset_type(asset_type))
29+
.where(FluentSearch.active_assets())
30+
.page_size(100)
31+
).to_request()
2632
return client.search(search_request)
2733

2834

@@ -41,14 +47,17 @@ def upstream_certified_sources(guid: str) -> list[Asset]:
4147
request.direction = LineageDirection.UPSTREAM
4248
request.offset = 0
4349
request.size = 100
44-
request.attributes = ["name", "certificateStatus"]
50+
request.attributes = [
51+
Asset.NAME.atlan_field_name,
52+
Asset.CERTIFICATE_STATUS.atlan_field_name,
53+
]
4554
request.entity_filters = FilterList(
4655
condition="AND",
4756
criteria=[
4857
EntityFilter(
49-
attribute_name="certificateStatus",
58+
attribute_name=Asset.CERTIFICATE_STATUS.atlan_field_name,
5059
operator=AtlanComparisonOperator.CONTAINS,
51-
attribute_value="VERIFIED",
60+
attribute_value=CertificateStatus.VERIFIED.value,
5261
)
5362
],
5463
)
@@ -61,7 +70,7 @@ def upstream_certified_sources(guid: str) -> list[Asset]:
6170

6271

6372
def main():
64-
results = find_all(type_name="SigmaWorkbook")
73+
results = find_all(SigmaWorkbook)
6574
for workbook in results:
6675
if isinstance(workbook, SigmaWorkbook):
6776
verified_sources = upstream_certified_sources(workbook.guid)

0 commit comments

Comments
 (0)