Lee-W commented on code in PR #37570:
URL: https://github.com/apache/airflow/pull/37570#discussion_r1497229772


##########
airflow/api_connexion/endpoints/dataset_endpoint.py:
##########
@@ -301,3 +312,37 @@ def delete_dataset_queued_events(
         "Queue event not found",
         detail=f"Queue event with dataset uri: `{uri}` was not found",
     )
+
+
+@security.requires_access_dataset("POST")
+@provide_session
+@action_logging(
+    event=action_event_from_permission(
+        prefix=RESOURCE_EVENT_PREFIX,
+        permission=permissions.ACTION_CAN_CREATE,
+    ),
+)
+def post_dataset_event(session: Session = NEW_SESSION) -> APIResponse:
+    """Post dataset event."""
+    try:
+        json_body = dataset_event_schema.load(get_json_request_dict(), 
session=session)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err))
+    uri = json_body["dataset_uri"]
+    dm = session.scalar(select(DatasetModel).where(DatasetModel.uri == 
uri).limit(1))
+    if not dm:
+        raise NotFound(title="Dataset not found", detail=f"Dataset with uri: 
'{uri}' not found")

Review Comment:
   It seems to be named this way elsewhere in this module



##########
airflow/api_connexion/endpoints/dataset_endpoint.py:
##########
@@ -301,3 +312,37 @@ def delete_dataset_queued_events(
         "Queue event not found",
         detail=f"Queue event with dataset uri: `{uri}` was not found",
     )
+
+
+@security.requires_access_dataset("POST")
+@provide_session
+@action_logging(
+    event=action_event_from_permission(
+        prefix=RESOURCE_EVENT_PREFIX,
+        permission=permissions.ACTION_CAN_CREATE,
+    ),
+)
+def post_dataset_event(session: Session = NEW_SESSION) -> APIResponse:
+    """Post dataset event."""
+    try:
+        json_body = dataset_event_schema.load(get_json_request_dict(), 
session=session)
+    except ValidationError as err:
+        raise BadRequest(detail=str(err))
+    uri = json_body["dataset_uri"]
+    dm = session.scalar(select(DatasetModel).where(DatasetModel.uri == 
uri).limit(1))
+    if not dm:
+        raise NotFound(title="Dataset not found", detail=f"Dataset with uri: 
'{uri}' not found")

Review Comment:
   ```suggestion
       dataset = session.scalar(select(DatasetModel).where(DatasetModel.uri == 
uri).limit(1))
       if not dataset:
           raise NotFound(title="Dataset not found", detail=f"Dataset with uri: 
'{uri}' not found")
   ```



##########
airflow/datasets/manager.py:
##########
@@ -72,16 +72,20 @@ def register_dataset_change(
         if not dataset_model:
             self.log.warning("DatasetModel %s not found", dataset)
             return
-        session.add(
-            DatasetEvent(
-                dataset_id=dataset_model.id,
-                source_task_id=task_instance.task_id,
-                source_dag_id=task_instance.dag_id,
-                source_run_id=task_instance.run_id,
-                source_map_index=task_instance.map_index,
-                extra=extra,
+        event_kwargs = {
+            "dataset_id": dataset_model.id,
+            "extra": extra,
+        }
+        if task_instance:
+            event_kwargs.update(
+                {
+                    "source_task_id": task_instance.task_id,
+                    "source_dag_id": task_instance.dag_id,
+                    "source_run_id": task_instance.run_id,
+                    "source_map_index": task_instance.map_index,
+                }
             )
-        )
+        session.add(DatasetEvent(**event_kwargs))

Review Comment:
   Will `task_instance=None` affect anything?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to