dstandish commented on code in PR #42455: URL: https://github.com/apache/airflow/pull/42455#discussion_r1775489462
########## airflow/api_connexion/endpoints/backfill_endpoint.py: ########## @@ -0,0 +1,181 @@ +# 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, + from_date, + to_date, + max_active_runs=10, + reverse=False, + dag_run_conf: dict | None = None, + session: Session = NEW_SESSION, +): + serdag = session.get(SerializedDagModel, dag_id) + if not serdag: + raise NotFound(f"Could not find dag {dag_id}") + + from_date = pendulum.parse(from_date) + to_date = pendulum.parse(to_date) + br = Backfill( + dag_id=dag_id, + from_date=from_date, + to_date=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), + ) + serialized = backfill_collection_schema.dump(obj) + return serialized + + [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.") + if br.is_paused is False: + br.is_paused = True + session.commit() + return backfill_schema.dump(br) + + [email protected]_access_dag("GET") +@action_logging +@provide_session +def unpause_backfill(backfill_id, session): + br = session.get(Backfill, backfill_id) + if not _has_access(dag_ids=[br.dag_id], methods="PUT"): Review Comment: the difference is, we are not given a dag_id in the request. we only get a backfill id. so 2 things 1. i wanted to piggy back off dag permissions -- dag can_edit --> can backfill. let me know if you think we need to do something different like add permissions and role stuff specifically for backfill. 2. from technical perspective, we could probably push this up into the decorator. lmk if you think that's a good idea. -- 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]
