-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathexternal_assets.py
More file actions
415 lines (359 loc) · 13.9 KB
/
Copy pathexternal_assets.py
File metadata and controls
415 lines (359 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from typing import Any
import dagster._check as check
from dagster import AssetObservation
from dagster._core.definitions.asset_checks.asset_check_evaluation import AssetCheckEvaluation
from dagster._core.definitions.asset_checks.asset_check_spec import AssetCheckSeverity
from dagster._core.definitions.data_version import (
DATA_VERSION_IS_USER_PROVIDED_TAG,
DATA_VERSION_TAG,
)
from dagster._core.definitions.events import AssetKey, AssetMaterialization
from dagster._core.workspace.context import BaseWorkspaceRequestContext
from dagster._core.workspace.permissions import Permissions
from dagster_shared.seven import json
from starlette.requests import Request
from starlette.responses import JSONResponse
def _unauthorized_response_or_none(
context: BaseWorkspaceRequestContext, asset_key: AssetKey
) -> JSONResponse | None:
if context.has_permission_for_selector(Permissions.REPORT_RUNLESS_ASSET_EVENTS, asset_key):
return None
return JSONResponse(
{"error": "Not authorized to report runless asset events."},
status_code=401,
)
def _asset_key_from_request(key: str, request: Request, json_body):
check.invariant(key == "asset_key") #
if request.path_params.get(key):
# use from_user_string to treat / as multipart key separator
return AssetKey.from_user_string(request.path_params["asset_key"])
elif ReportAssetMatParam.asset_key in json_body:
return AssetKey(json_body[ReportAssetMatParam.asset_key])
elif ReportAssetMatParam.asset_key in request.query_params:
return AssetKey.from_db_string(request.query_params["asset_key"])
return None
def _value_from_body_or_params(key: str, request: Request, json_body) -> Any:
if key in json_body:
return json_body[key]
elif key in request.query_params:
return request.query_params[key]
return None
async def handle_report_asset_materialization_request(
context: BaseWorkspaceRequestContext,
request: Request,
) -> JSONResponse:
# Record a runless asset materialization event.
# The asset key is passed as url path with / delimiting parts or as a query param.
# Properties can be passed as json post body or query params, with that order of precedence.
body_content_type = request.headers.get("content-type")
if body_content_type is None:
json_body = {}
elif body_content_type == "application/json":
json_body = await request.json()
else:
return JSONResponse(
{
"error": (
f"Unhandled content type {body_content_type}, expect no body or"
" application/json"
),
},
status_code=400,
)
asset_key = _asset_key_from_request(ReportAssetMatParam.asset_key, request, json_body)
if asset_key is None:
return JSONResponse(
{
"error": (
"Empty asset key, must provide asset key as url path after"
" /report_asset_materialization/ or query param asset_key."
),
},
status_code=400,
)
unauthorized = _unauthorized_response_or_none(context, asset_key)
if unauthorized is not None:
return unauthorized
tags = context.get_reporting_user_tags()
user_tags = None
if ReportAssetMatParam.tags in json_body:
user_tags = json_body[ReportAssetMatParam.tags]
elif ReportAssetMatParam.tags in request.query_params:
try:
user_tags = json.loads(request.query_params[ReportAssetMatParam.tags])
except Exception as exc:
return JSONResponse(
{
"error": f"Error parsing tags json: {exc}",
},
status_code=400,
)
if user_tags is not None:
if not isinstance(user_tags, dict):
return JSONResponse(
{
"error": "Expected tags to be a json object.",
},
status_code=400,
)
# merged before the dedicated data_version param so that the explicit
# param takes precedence over a conflicting tag
tags.update(user_tags)
data_version = _value_from_body_or_params(ReportAssetMatParam.data_version, request, json_body)
if data_version is not None:
tags[DATA_VERSION_TAG] = data_version
tags[DATA_VERSION_IS_USER_PROVIDED_TAG] = "true"
partition = _value_from_body_or_params(ReportAssetMatParam.partition, request, json_body)
description = _value_from_body_or_params(ReportAssetMatParam.description, request, json_body)
metadata = None
if ReportAssetMatParam.metadata in json_body:
metadata = json_body[ReportAssetMatParam.metadata]
elif ReportAssetMatParam.metadata in request.query_params:
try:
metadata = json.loads(request.query_params[ReportAssetMatParam.metadata])
except Exception as exc:
return JSONResponse(
{
"error": f"Error parsing metadata json: {exc}",
},
status_code=400,
)
try:
mat = AssetMaterialization(
asset_key=asset_key,
partition=partition,
metadata=metadata,
description=description,
tags=tags,
)
except Exception as exc:
return JSONResponse(
{
"error": f"Error constructing AssetMaterialization: {exc}",
},
status_code=400,
)
context.instance.report_runless_asset_event(mat)
return JSONResponse({})
async def handle_report_asset_check_request(
context: BaseWorkspaceRequestContext,
request: Request,
) -> JSONResponse:
# Record a runless asset check evaluation event.
# The asset key is passed as url path with / delimiting parts or as a query param.
# Properties can be passed as json post body or query params, with that order of precedence.
body_content_type = request.headers.get("content-type")
if body_content_type is None:
json_body = {}
elif body_content_type == "application/json":
json_body = await request.json()
else:
return JSONResponse(
{
"error": (
f"Unhandled content type {body_content_type}, expect no body or"
" application/json"
),
},
status_code=400,
)
asset_key = _asset_key_from_request(ReportAssetCheckEvalParam.asset_key, request, json_body)
if asset_key is None:
return JSONResponse(
{
"error": (
"Empty asset key, must provide asset key as url path after"
" /report_asset_check_evaluation/ or query param asset_key."
),
},
status_code=400,
)
unauthorized = _unauthorized_response_or_none(context, asset_key)
if unauthorized is not None:
return unauthorized
passed = _value_from_body_or_params(ReportAssetCheckEvalParam.passed, request, json_body)
check_name = _value_from_body_or_params(
ReportAssetCheckEvalParam.check_name, request, json_body
)
severity = _value_from_body_or_params(ReportAssetCheckEvalParam.severity, request, json_body)
if severity is None:
severity = "ERROR" # default
if ReportAssetCheckEvalParam.passed in json_body:
passed = json_body[ReportAssetCheckEvalParam.passed]
elif ReportAssetCheckEvalParam.passed in request.query_params:
try:
passed = json.loads(request.query_params[ReportAssetCheckEvalParam.passed])
except Exception as exc:
return JSONResponse(
{
"error": f"Error parsing 'passed': {exc}",
},
status_code=400,
)
else:
return JSONResponse(
{
"error": "Missing required parameter 'passed'.",
},
status_code=400,
)
metadata = {}
if ReportAssetCheckEvalParam.metadata in json_body:
metadata = json_body[ReportAssetCheckEvalParam.metadata]
elif ReportAssetCheckEvalParam.metadata in request.query_params:
try:
metadata = json.loads(request.query_params[ReportAssetCheckEvalParam.metadata])
except Exception as exc:
return JSONResponse(
{
"error": f"Error parsing metadata json: {exc}",
},
status_code=400,
)
partition = _value_from_body_or_params(ReportAssetCheckEvalParam.partition, request, json_body)
try:
evaluation = AssetCheckEvaluation(
check_name=check_name,
passed=passed,
asset_key=asset_key,
metadata=metadata,
severity=AssetCheckSeverity(severity),
partition=partition,
)
except Exception as exc:
return JSONResponse(
{
"error": f"Error constructing AssetCheckEvaluation: {exc}",
},
status_code=400,
)
context.instance.report_runless_asset_event(evaluation)
return JSONResponse({})
async def handle_report_asset_observation_request(
context: BaseWorkspaceRequestContext,
request: Request,
) -> JSONResponse:
# Record a runless asset observation event.
# The asset key is passed as url path with / delimiting parts or as a query param.
# Properties can be passed as json post body or query params, with that order of precedence.
body_content_type = request.headers.get("content-type")
if body_content_type is None:
json_body = {}
elif body_content_type == "application/json":
json_body = await request.json()
else:
return JSONResponse(
{
"error": (
f"Unhandled content type {body_content_type}, expect no body or"
" application/json"
),
},
status_code=400,
)
asset_key = _asset_key_from_request(ReportAssetObsParam.asset_key, request, json_body)
if asset_key is None:
return JSONResponse(
{
"error": (
"Empty asset key, must provide asset key as url path after"
" /report_asset_materialization/ or query param asset_key."
),
},
status_code=400,
)
unauthorized = _unauthorized_response_or_none(context, asset_key)
if unauthorized is not None:
return unauthorized
metadata = {}
if ReportAssetObsParam.metadata in json_body:
metadata = json_body[ReportAssetObsParam.metadata]
elif ReportAssetObsParam.metadata in request.query_params:
try:
metadata = json.loads(request.query_params[ReportAssetObsParam.metadata])
except Exception as exc:
return JSONResponse(
{
"error": f"Error parsing metadata json: {exc}",
},
status_code=400,
)
partition = _value_from_body_or_params(ReportAssetObsParam.partition, request, json_body)
description = _value_from_body_or_params(ReportAssetObsParam.description, request, json_body)
tags = context.get_reporting_user_tags()
user_tags = None
if ReportAssetObsParam.tags in json_body:
user_tags = json_body[ReportAssetObsParam.tags]
elif ReportAssetObsParam.tags in request.query_params:
try:
user_tags = json.loads(request.query_params[ReportAssetObsParam.tags])
except Exception as exc:
return JSONResponse(
{
"error": f"Error parsing tags json: {exc}",
},
status_code=400,
)
if user_tags is not None:
if not isinstance(user_tags, dict):
return JSONResponse(
{
"error": "Expected tags to be a json object.",
},
status_code=400,
)
# merged before the dedicated data_version param so that the explicit
# param takes precedence over a conflicting tag
tags.update(user_tags)
data_version = _value_from_body_or_params(ReportAssetObsParam.data_version, request, json_body)
if data_version is not None:
tags[DATA_VERSION_TAG] = data_version
tags[DATA_VERSION_IS_USER_PROVIDED_TAG] = "true"
try:
observation = AssetObservation(
asset_key=asset_key,
partition=partition,
metadata=metadata,
description=description,
tags=tags,
)
except Exception as exc:
return JSONResponse(
{
"error": f"Error constructing AssetObservation: {exc}",
},
status_code=400,
)
context.instance.report_runless_asset_event(observation)
return JSONResponse({})
# note: Enum not used to avoid value type problems X(str, Enum) doesn't work as partition conflicts with keyword
class ReportAssetMatParam:
"""Class to collect all supported args by report_asset_materialization endpoint
to ensure consistency with related APIs.
"""
asset_key = "asset_key"
data_version = "data_version"
metadata = "metadata"
description = "description"
partition = "partition"
tags = "tags"
class ReportAssetCheckEvalParam:
"""Class to collect all supported args by report_asset_check endpoint
to ensure consistency with related APIs.
"""
asset_key = "asset_key"
check_name = "check_name"
metadata = "metadata"
severity = "severity"
passed = "passed"
partition = "partition"
class ReportAssetObsParam:
"""Class to collect all supported args by report_asset_observation endpoint
to ensure consistency with related APIs.
"""
asset_key = "asset_key"
data_version = "data_version"
metadata = "metadata"
description = "description"
partition = "partition"
tags = "tags"