| 
									
										
										
										
											2025-04-03 10:39:47 +05:30
										 |  |  | #  Copyright 2025 Collate | 
					
						
							|  |  |  | #  Licensed under the Collate Community License, Version 1.0 (the "License"); | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | #  you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | #  You may obtain a copy of the License at | 
					
						
							| 
									
										
										
										
											2025-04-03 10:39:47 +05:30
										 |  |  | #  https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | #  Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | #  distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | #  See the License for the specific language governing permissions and | 
					
						
							|  |  |  | #  limitations under the License. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | Trigger endpoint | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | import traceback | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  | from typing import Callable | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  | from flask import Blueprint, Response, request | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | from openmetadata_managed_apis.api.response import ApiResponse | 
					
						
							| 
									
										
										
										
											2025-05-06 12:41:24 +02:00
										 |  |  | from openmetadata_managed_apis.api.utils import ( | 
					
						
							|  |  |  |     get_request_arg, | 
					
						
							|  |  |  |     get_request_conf, | 
					
						
							|  |  |  |     get_request_dag_id, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | from openmetadata_managed_apis.operations.trigger import trigger | 
					
						
							| 
									
										
										
										
											2022-08-26 07:29:38 +02:00
										 |  |  | from openmetadata_managed_apis.utils.logger import routes_logger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | logger = routes_logger() | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  | def get_fn(blueprint: Blueprint) -> Callable: | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  |     Return the function loaded to a route | 
					
						
							|  |  |  |     :param blueprint: Flask Blueprint to assign route to | 
					
						
							|  |  |  |     :return: routed function | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  |     # Lazy import the requirements | 
					
						
							|  |  |  |     # pylint: disable=import-outside-toplevel | 
					
						
							|  |  |  |     from airflow.api_connexion import security | 
					
						
							|  |  |  |     from airflow.security import permissions | 
					
						
							|  |  |  |     from airflow.www.app import csrf | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  |     @blueprint.route("/trigger", methods=["POST"]) | 
					
						
							|  |  |  |     @csrf.exempt | 
					
						
							|  |  |  |     @security.requires_access([(permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG)]) | 
					
						
							|  |  |  |     def trigger_dag() -> Response: | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2025-05-06 12:41:24 +02:00
										 |  |  |         Trigger a dag run with optional configuration | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         dag_id = get_request_dag_id() | 
					
						
							| 
									
										
										
										
											2022-07-28 14:46:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             run_id = get_request_arg(request, "run_id", raise_missing=False) | 
					
						
							| 
									
										
										
										
											2025-05-06 12:41:24 +02:00
										 |  |  |             conf = get_request_conf() | 
					
						
							|  |  |  |             response = trigger(dag_id, run_id, conf=conf) | 
					
						
							| 
									
										
										
										
											2022-09-15 10:32:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         except Exception as exc: | 
					
						
							|  |  |  |             logger.debug(traceback.format_exc()) | 
					
						
							|  |  |  |             logger.error(f"Failed to trigger dag [{dag_id}]: {exc}") | 
					
						
							|  |  |  |             return ApiResponse.error( | 
					
						
							|  |  |  |                 status=ApiResponse.STATUS_SERVER_ERROR, | 
					
						
							|  |  |  |                 error=f"Workflow [{dag_id}] has filed to trigger due to [{exc}] ", | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return trigger_dag |