-
Notifications
You must be signed in to change notification settings - Fork 16.4k
AIP-84: Migrate Extra Links endpoint to fastapi #44277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ef44a38
migrate extra link endpoint to fastapi
e7572f8
add airflow license
a45e136
add tests
c3a1767
Apply suggestions from code review
prabhusneha 11340ed
add teardown in test
4f66b89
change async to sync
0cddfe6
Merge branch 'main' into extra_link_endpoint
prabhusneha 650f05f
Address PR comments and fix static checks
ccccba9
Address PR comment
a598faa
Merge branch 'main' into extra_link_endpoint
prabhusneha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,25 @@ | ||
| # 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 pydantic import RootModel | ||
|
|
||
|
|
||
| class ExtraLinksResponse(RootModel): | ||
| """Extra Links Response.""" | ||
|
|
||
| root: dict[str, str | None] | ||
This file contains hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
prabhusneha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # 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 typing import TYPE_CHECKING, Annotated | ||
|
|
||
| from fastapi import Depends, HTTPException, Request, status | ||
| from sqlalchemy.orm import Session | ||
| from sqlalchemy.sql import select | ||
|
|
||
| from airflow.api_fastapi.common.db.common import get_session | ||
| from airflow.api_fastapi.common.router import AirflowRouter | ||
| from airflow.api_fastapi.core_api.datamodels.extra_links import ExtraLinksResponse | ||
| from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc | ||
| from airflow.exceptions import TaskNotFound | ||
prabhusneha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from sqlalchemy.orm.session import Session | ||
|
|
||
| from airflow.models import DAG | ||
|
|
||
|
|
||
| extra_links_router = AirflowRouter( | ||
| tags=["Extra Links"], prefix="/dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances/{task_id}/links" | ||
prabhusneha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
|
|
||
| @extra_links_router.get( | ||
| "", | ||
| responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), | ||
| tags=["Task Instance"], | ||
| ) | ||
| def get_extra_links( | ||
| dag_id: str, | ||
| dag_run_id: str, | ||
| task_id: str, | ||
| session: Annotated[Session, Depends(get_session)], | ||
| request: Request, | ||
| ) -> ExtraLinksResponse: | ||
| """Get extra links for task instance.""" | ||
| from airflow.models.taskinstance import TaskInstance | ||
|
|
||
| dag: DAG = request.app.state.dag_bag.get_dag(dag_id) | ||
| if not dag: | ||
| raise HTTPException(status.HTTP_404_NOT_FOUND, f"DAG with ID = {dag_id} not found") | ||
|
|
||
| try: | ||
| task = dag.get_task(task_id) | ||
| except TaskNotFound: | ||
| raise HTTPException(status.HTTP_404_NOT_FOUND, f"Task with ID = {task_id} not found") | ||
|
|
||
| ti = session.scalar( | ||
| select(TaskInstance).where( | ||
| TaskInstance.dag_id == dag_id, | ||
| TaskInstance.run_id == dag_run_id, | ||
| TaskInstance.task_id == task_id, | ||
| ) | ||
| ) | ||
|
|
||
| if not ti: | ||
| raise HTTPException( | ||
| status.HTTP_404_NOT_FOUND, | ||
| f"DAG Run with ID = {dag_run_id} not found", | ||
| ) | ||
|
|
||
| all_extra_link_pairs = ( | ||
| (link_name, task.get_extra_links(ti, link_name)) for link_name in task.extra_links | ||
| ) | ||
| all_extra_links = {link_name: link_url or None for link_name, link_url in sorted(all_extra_link_pairs)} | ||
| return ExtraLinksResponse.model_validate(all_extra_links) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.