forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIP-84: Migrate Dag Parsing endpoint to FastApi (apache#44416)
* AIP-84: Migrate Dag Parsing endpoint to FastApi * Address PR comments * Change the test class name * Address PR comments and fix tests * Address PR comment * remove database isolation option to fix failing check * Address PR comment --------- Co-authored-by: Sneha Prabhu <[email protected]>
- Loading branch information
1 parent
69cd237
commit 3e427c9
Showing
9 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 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 | ||
|
||
from collections.abc import Sequence | ||
from typing import TYPE_CHECKING, Annotated | ||
|
||
from fastapi import Depends, HTTPException, Request, status | ||
from itsdangerous import BadSignature, URLSafeSerializer | ||
from sqlalchemy import select | ||
from sqlalchemy.orm import Session | ||
|
||
from airflow.api_fastapi.common.db.common import get_session | ||
from airflow.api_fastapi.common.router import AirflowRouter | ||
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc | ||
from airflow.auth.managers.models.resource_details import DagDetails | ||
from airflow.models.dag import DagModel | ||
from airflow.models.dagbag import DagPriorityParsingRequest | ||
|
||
if TYPE_CHECKING: | ||
from airflow.auth.managers.models.batch_apis import IsAuthorizedDagRequest | ||
|
||
dag_parsing_router = AirflowRouter(tags=["DAG Parsing"], prefix="/parseDagFile/{file_token}") | ||
|
||
|
||
@dag_parsing_router.put( | ||
"", | ||
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), | ||
status_code=status.HTTP_201_CREATED, | ||
) | ||
def reparse_dag_file( | ||
file_token: str, | ||
session: Annotated[Session, Depends(get_session)], | ||
request: Request, | ||
) -> None: | ||
"""Request re-parsing a DAG file.""" | ||
secret_key = request.app.state.secret_key | ||
auth_s = URLSafeSerializer(secret_key) | ||
try: | ||
path = auth_s.loads(file_token) | ||
except BadSignature: | ||
raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found") | ||
|
||
requests: Sequence[IsAuthorizedDagRequest] = [ | ||
{"method": "PUT", "details": DagDetails(id=dag_id)} | ||
for dag_id in session.scalars(select(DagModel.dag_id).where(DagModel.fileloc == path)) | ||
] | ||
if not requests: | ||
raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found") | ||
|
||
parsing_request = DagPriorityParsingRequest(fileloc=path) | ||
session.add(parsing_request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
tests/api_fastapi/core_api/routes/public/test_dag_parsing.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# 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 os | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
from sqlalchemy import select | ||
|
||
from airflow.models import DagBag | ||
from airflow.models.dagbag import DagPriorityParsingRequest | ||
from airflow.utils.session import provide_session | ||
|
||
from tests_common.test_utils.db import clear_db_dag_parsing_requests | ||
|
||
pytestmark = pytest.mark.db_test | ||
|
||
if TYPE_CHECKING: | ||
from airflow.models.dag import DAG | ||
|
||
|
||
class TestDagParsingEndpoint: | ||
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) | ||
EXAMPLE_DAG_FILE = os.path.join("airflow", "example_dags", "example_bash_operator.py") | ||
EXAMPLE_DAG_ID = "example_bash_operator" | ||
TEST_DAG_ID = "latest_only" | ||
NOT_READABLE_DAG_ID = "latest_only_with_trigger" | ||
TEST_MULTIPLE_DAGS_ID = "asset_produces_1" | ||
|
||
@staticmethod | ||
def clear_db(): | ||
clear_db_dag_parsing_requests() | ||
|
||
@provide_session | ||
@pytest.fixture(autouse=True) | ||
def setup(self, session=None) -> None: | ||
self.clear_db() | ||
|
||
def teardown_method(self) -> None: | ||
self.clear_db() | ||
|
||
def test_201_and_400_requests(self, url_safe_serializer, session, test_client): | ||
dagbag = DagBag(dag_folder=self.EXAMPLE_DAG_FILE) | ||
dagbag.sync_to_db() | ||
test_dag: DAG = dagbag.dags[self.TEST_DAG_ID] | ||
|
||
url = f"/public/parseDagFile/{url_safe_serializer.dumps(test_dag.fileloc)}" | ||
response = test_client.put(url, headers={"Accept": "application/json"}) | ||
assert response.status_code == 201 | ||
parsing_requests = session.scalars(select(DagPriorityParsingRequest)).all() | ||
assert parsing_requests[0].fileloc == test_dag.fileloc | ||
|
||
# Duplicate file parsing request | ||
response = test_client.put(url, headers={"Accept": "application/json"}) | ||
assert response.status_code == 409 | ||
parsing_requests = session.scalars(select(DagPriorityParsingRequest)).all() | ||
assert parsing_requests[0].fileloc == test_dag.fileloc | ||
|
||
def test_bad_file_request(self, url_safe_serializer, session, test_client): | ||
url = f"/public/parseDagFile/{url_safe_serializer.dumps('/some/random/file.py')}" | ||
response = test_client.put(url, headers={"Accept": "application/json"}) | ||
assert response.status_code == 404 | ||
|
||
parsing_requests = session.scalars(select(DagPriorityParsingRequest)).all() | ||
assert parsing_requests == [] |