jedcunningham commented on code in PR #42455:
URL: https://github.com/apache/airflow/pull/42455#discussion_r1774500888


##########
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,
+):

Review Comment:
   ```suggestion
   ) -> Backfill:
   ```



##########
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,

Review Comment:
   ```suggestion
       dag_id: str,
       from_date: str,
       to_date: str,
       max_active_runs: int,
       reverse: bool,
       dag_run_conf: dict | None,
   ```
   Don't need the defaults - they are already done and passed through to here 
anyways.



##########
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:
   Hmm, I wonder if we can just check the PUT on the decorator? e.g. for 
[patch_dags we only check the 
PUT](https://github.com/apache/airflow/blob/b9b7bfc6a8dff0324b00b8fb2a39624ad213a4c2/airflow/api_connexion/endpoints/dag_endpoint.py#L167)



##########
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"):
+        raise PermissionDenied()
+    if br.completed_at:
+        raise Conflict("Backfill is already completed.")
+    if br.is_paused:
+        br.is_paused = False
+    session.commit()
+    return backfill_schema.dump(br)
+
+
[email protected]_access_dag("GET")
+@action_logging
+@provide_session
+def cancel_backfill(backfill_id, session):
+    br: Backfill = session.get(Backfill, backfill_id)
+    if not _has_access(dag_ids=[br.dag_id], methods="PUT"):
+        raise PermissionDenied()
+
+    if br.completed_at is not None:
+        raise Conflict("Backfill is already completed.")
+
+    br.completed_at = timezone.utcnow()
+
+    # first, pause
+    if not br.is_paused:
+        br.is_paused = True
+    session.commit()
+    return backfill_schema.dump(br)
+
+
[email protected]_access_dag("GET")
+@action_logging
+@provide_session
+def get_backfill(backfill_id: int, session: Session = NEW_SESSION):
+    backfill = session.get(Backfill, backfill_id)
+    if backfill:
+        if not _has_access([backfill.dag_id], methods=["GET"]):
+            raise PermissionDenied()
+        return backfill_schema.dump(backfill)
+    raise NotFound("Backfill not found")
+
+
[email protected]_access_dag("PUT")
+@action_logging
+def create_backfill(
+    dag_id: str,
+    from_date: str,
+    to_date: str,
+    max_active_runs: int = 10,
+    reverse: bool = False,
+    dag_run_conf: dict | None = None,
+) -> APIResponse:
+    backfill_obj = _create_backfill(
+        dag_id=dag_id,
+        from_date=from_date,
+        to_date=to_date,
+        max_active_runs=max_active_runs,
+        reverse=reverse,
+        dag_run_conf=dag_run_conf,
+    )
+    if not backfill_obj:

Review Comment:
   Can this even be falsy?



##########
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

Review Comment:
   ```suggestion
       return backfill_collection_schema.dump(obj)
   ```
   
   nit



-- 
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]

Reply via email to