dstandish commented on code in PR #42455: URL: https://github.com/apache/airflow/pull/42455#discussion_r1775996042
########## airflow/api_connexion/endpoints/backfill_endpoint.py: ########## @@ -0,0 +1,176 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. + +from __future__ import annotations + +import logging +from typing import TYPE_CHECKING + +import pendulum +from flask import g +from sqlalchemy import select + +from airflow.api_connexion import security +from airflow.api_connexion.exceptions import Conflict, NotFound, PermissionDenied +from airflow.api_connexion.schemas.backfill_schema import ( + BackfillCollection, + backfill_collection_schema, + backfill_schema, +) +from airflow.models.backfill import Backfill +from airflow.models.serialized_dag import SerializedDagModel +from airflow.utils import timezone +from airflow.utils.session import NEW_SESSION, provide_session +from airflow.www.decorators import action_logging +from airflow.www.extensions.init_auth_manager import get_auth_manager + +if TYPE_CHECKING: + from sqlalchemy.orm import Session + + from airflow.api_connexion.types import APIResponse + +log = logging.getLogger(__name__) + +RESOURCE_EVENT_PREFIX = "dag" + + +@provide_session +def _create_backfill( + *, + dag_id: str, + from_date: str, + to_date: str, + max_active_runs: int, + reverse: bool, + dag_run_conf: dict | None, + session: Session = NEW_SESSION, +) -> Backfill: + serdag = session.get(SerializedDagModel, dag_id) + if not serdag: + raise NotFound(f"Could not find dag {dag_id}") + + br = Backfill( + dag_id=dag_id, + from_date=pendulum.parse(from_date), + to_date=pendulum.parse(to_date), + max_active_runs=max_active_runs, + dag_run_conf=dag_run_conf, + ) + session.add(br) + session.commit() + return br + + +def _has_access(dag_ids, methods): + if get_auth_manager().filter_permitted_dag_ids(dag_ids=dag_ids, methods=methods, user=g.user): + return True + return False + + [email protected]_access_dag("GET") +@action_logging +@provide_session +def list_backfills(dag_id, session): + backfills = session.scalars(select(Backfill).where(Backfill.dag_id == dag_id)).all() + obj = BackfillCollection( + backfills=backfills, + total_entries=len(backfills), + ) + return backfill_collection_schema.dump(obj) + + [email protected]_access_dag("GET") +@action_logging +@provide_session +def pause_backfill(backfill_id, session): + br = session.get(Backfill, backfill_id) + if not _has_access(dag_ids=[br.dag_id], methods="PUT"): + raise PermissionDenied() + if br.completed_at: + raise Conflict("Backfill is already completed.") Review Comment: thanks, yeah, this was updated in consoltation with vincent.... welcome to have another look. i added a decorator that injects dag id -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
