Repository navigation
AIP-84 Migrate delete a connection to FastAPI API #42571
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
pierrejeambrun
merged 10 commits into
apache:main
from
bugraoz93:42559/aip-84/delete-connection-to-fast-api
Oct 2, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
edfcf95
Include connections router and migrate delete a connection endpoint t…
bugraoz93 277f5e8
Mark tests as db_test
bugraoz93 e2caaae
Use only pyfixture session
bugraoz93 935e09d
make method async
bugraoz93 a6ff050
setup method to setup_attrs
bugraoz93 0c1c965
Merge branch 'main' into 42559/aip-84/delete-connection-to-fast-api
bugraoz93 a02bfba
Convert APIRouter tags, make setup method unified
bugraoz93 c5e598b
Merge branch 'main' into 42559/aip-84/delete-connection-to-fast-api
bugraoz93 7f1fb27
Merge branch 'main' into 42559/aip-84/delete-connection-to-fast-api
bugraoz93 d30d43e
Use AirflowRouter over fastapi.APIRouter
bugraoz93 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
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,47 @@ | ||
| # 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 fastapi import Depends, HTTPException | ||
| from sqlalchemy import select | ||
| from sqlalchemy.orm import Session | ||
| from typing_extensions import Annotated | ||
|
|
||
| from airflow.api_fastapi.db.common import get_session | ||
| from airflow.api_fastapi.openapi.exceptions import create_openapi_http_exception_doc | ||
| from airflow.api_fastapi.views.router import AirflowRouter | ||
| from airflow.models import Connection | ||
|
|
||
| connections_router = AirflowRouter(tags=["Connection"]) | ||
|
|
||
|
|
||
| @connections_router.delete( | ||
| "/connections/{connection_id}", | ||
| status_code=204, | ||
| responses=create_openapi_http_exception_doc([401, 403, 404]), | ||
| ) | ||
| async def delete_connection( | ||
| connection_id: str, | ||
| session: Annotated[Session, Depends(get_session)], | ||
| ): | ||
| """Delete a connection entry.""" | ||
| connection = session.scalar(select(Connection).filter_by(conn_id=connection_id)) | ||
|
|
||
| if connection is None: | ||
| raise HTTPException(404, f"The Connection with connection_id: `{connection_id}` was not found") | ||
|
|
||
| session.delete(connection) | ||
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
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,63 @@ | ||
| # 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 pytest | ||
|
|
||
| from airflow.models import Connection | ||
| from airflow.utils.session import provide_session | ||
| from tests.test_utils.db import clear_db_connections | ||
|
|
||
| pytestmark = pytest.mark.db_test | ||
|
|
||
| TEST_CONN_ID = "test_connection_id" | ||
| TEST_CONN_TYPE = "test_type" | ||
|
|
||
|
|
||
| @provide_session | ||
| def _create_connection(session) -> None: | ||
| connection_model = Connection(conn_id=TEST_CONN_ID, conn_type=TEST_CONN_TYPE) | ||
| session.add(connection_model) | ||
|
|
||
|
|
||
| class TestConnectionEndpoint: | ||
|
pierrejeambrun marked this conversation as resolved.
Resolved
|
||
| @pytest.fixture(autouse=True) | ||
| def setup(self) -> None: | ||
| clear_db_connections(False) | ||
|
|
||
| def teardown_method(self) -> None: | ||
| clear_db_connections() | ||
|
|
||
| def create_connection(self): | ||
| _create_connection() | ||
|
|
||
|
|
||
| class TestDeleteConnection(TestConnectionEndpoint): | ||
| def test_delete_should_respond_204(self, test_client, session): | ||
| self.create_connection() | ||
| conns = session.query(Connection).all() | ||
| assert len(conns) == 1 | ||
| response = test_client.delete(f"/public/connections/{TEST_CONN_ID}") | ||
| assert response.status_code == 204 | ||
| connection = session.query(Connection).all() | ||
| assert len(connection) == 0 | ||
|
|
||
| def test_delete_should_respond_404(self, test_client): | ||
| response = test_client.delete(f"/public/connections/{TEST_CONN_ID}") | ||
| assert response.status_code == 404 | ||
| body = response.json() | ||
| assert f"The Connection with connection_id: `{TEST_CONN_ID}` was not found" == body["detail"] | ||
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.