Skip to content

Commit ced105c

Browse files
authored
Merge pull request #575 from atlanhq/APP-5956
APP-5956 : Add custom `AtlanError` for the `DataProduct.get_assets` method
2 parents d44d087 + 6bbf207 commit ced105c

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

pyatlan/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,13 @@ class ErrorCode(Enum):
629629
"Set `test` to `True` or remove `username` and `password`.",
630630
InvalidRequestError,
631631
)
632+
MISSING_DATA_PRODUCT_ASSET_DSL = (
633+
400,
634+
"ATLAN-PYTHON-400-072",
635+
"Missing value for `data_product_assets_d_s_l`, which is required to retrieve DataProduct assets.",
636+
"Ensure your product instance has a valid `data_product_assets_d_s_l` value before making the request.",
637+
InvalidRequestError,
638+
)
632639
AUTHENTICATION_PASSTHROUGH = (
633640
401,
634641
"ATLAN-PYTHON-401-000",

pyatlan/generator/templates/methods/asset/data_product.jinja2

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@
9090
Reterieves list of all assets linked to the provided data product.
9191

9292
:param client: connectivity to an Atlan tenant (optional). If not provided, the default client will be used.
93-
93+
:raises InvalidRequestError: if DataProduct asset DSL cannot be found (does not exist) in Atlan
9494
:raises AtlanError: if there is an issue interacting with the API
9595
:returns: instance of `IndexSearchResults` with list of all assets linked to the provided data product
96-
9796
"""
9897
from pyatlan.client.atlan import AtlanClient
9998

10099
client = AtlanClient.get_current_client() if not client else client
101100
dp_dsl = self.data_product_assets_d_s_l
102-
json_object = json.loads(dp_dsl) if dp_dsl else {}
101+
if not dp_dsl:
102+
raise ErrorCode.MISSING_DATA_PRODUCT_ASSET_DSL.exception_with_parameters()
103+
json_object = json.loads(dp_dsl)
103104
request = IndexSearchRequest(**json_object.get("query", {}))
104105
response = client.asset.search(request)
105106
return response

pyatlan/model/assets/core/data_product.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from pydantic.v1 import Field, StrictStr, validator
1313

14+
from pyatlan.errors import ErrorCode
1415
from pyatlan.model.data_mesh import DataProductsAssetsDSL
1516
from pyatlan.model.enums import (
1617
DataProductCriticality,
@@ -128,16 +129,17 @@ def get_assets(self, client: Optional[AtlanClient] = None):
128129
Reterieves list of all assets linked to the provided data product.
129130
130131
:param client: connectivity to an Atlan tenant (optional). If not provided, the default client will be used.
131-
132+
:raises InvalidRequestError: if DataProduct asset DSL cannot be found (does not exist) in Atlan
132133
:raises AtlanError: if there is an issue interacting with the API
133134
:returns: instance of `IndexSearchResults` with list of all assets linked to the provided data product
134-
135135
"""
136136
from pyatlan.client.atlan import AtlanClient
137137

138138
client = AtlanClient.get_current_client() if not client else client
139139
dp_dsl = self.data_product_assets_d_s_l
140-
json_object = json.loads(dp_dsl) if dp_dsl else {}
140+
if not dp_dsl:
141+
raise ErrorCode.MISSING_DATA_PRODUCT_ASSET_DSL.exception_with_parameters()
142+
json_object = json.loads(dp_dsl)
141143
request = IndexSearchRequest(**json_object.get("query", {}))
142144
response = client.asset.search(request)
143145
return response

tests/unit/model/data_product_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55

6+
from pyatlan.client.atlan import AtlanClient
7+
from pyatlan.errors import InvalidRequestError
68
from pyatlan.model.assets import AtlasGlossary, DataProduct
79
from pyatlan.model.enums import CertificateStatus, DataProductStatus
810
from pyatlan.model.fluent_search import CompoundQuery, FluentSearch
@@ -21,6 +23,26 @@
2123
ASSETS_PLAYBOOK_FILTER = '{"condition":"AND","isGroupLocked":false,"rules":[]}'
2224

2325

26+
@pytest.fixture(autouse=True)
27+
def set_env(monkeypatch):
28+
monkeypatch.setenv("ATLAN_BASE_URL", "https://test.atlan.com")
29+
monkeypatch.setenv("ATLAN_API_KEY", "test-api-key")
30+
31+
32+
@pytest.fixture()
33+
def client():
34+
return AtlanClient()
35+
36+
37+
@pytest.fixture()
38+
def current_client(client, monkeypatch):
39+
monkeypatch.setattr(
40+
AtlanClient,
41+
"get_current_client",
42+
lambda: client,
43+
)
44+
45+
2446
def load_json(respones_dir, filename):
2547
with (respones_dir / filename).open() as input_file:
2648
return load(input_file)
@@ -144,6 +166,21 @@ def test_create_for_modification():
144166
_assert_product(test_product)
145167

146168

169+
def test_get_assets_with_missing_dp_asset_dsl(client: AtlanClient):
170+
data_product = DataProduct()
171+
data_product.attributes.name = DATA_PRODUCT_NAME
172+
data_product.parent_domain_qualified_name = DATA_PRODUCT_QUALIFIED_NAME
173+
data_product.data_product_assets_d_s_l = None
174+
with pytest.raises(
175+
InvalidRequestError,
176+
match=(
177+
"Missing value for `data_product_assets_d_s_l`, "
178+
"which is required to retrieve DataProduct assets."
179+
),
180+
):
181+
data_product.get_assets(client=client)
182+
183+
147184
def test_trim_to_required():
148185
test_product = DataProduct.create_for_modification(
149186
qualified_name=DATA_PRODUCT_QUALIFIED_NAME,

0 commit comments

Comments
 (0)