forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
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 delete a connection to FastAPI API (apache#42571)
* Include connections router and migrate delete a connection endpoint to fastapi * Mark tests as db_test * Use only pyfixture session * make method async * setup method to setup_attrs * Convert APIRouter tags, make setup method unified * Use AirflowRouter over fastapi.APIRouter
- Loading branch information
Showing
9 changed files
with
270 additions
and
2 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,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 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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
// generated with @7nohe/[email protected] | ||
import { UseQueryResult } from "@tanstack/react-query"; | ||
|
||
import { AssetService, DagService } from "../requests/services.gen"; | ||
import { | ||
AssetService, | ||
ConnectionService, | ||
DagService, | ||
} from "../requests/services.gen"; | ||
import { DagRunState } from "../requests/types.gen"; | ||
|
||
export type AssetServiceNextRunAssetsDefaultResponse = Awaited< | ||
|
@@ -76,3 +80,6 @@ export type DagServicePatchDagsMutationResult = Awaited< | |
export type DagServicePatchDagMutationResult = Awaited< | ||
ReturnType<typeof DagService.patchDag> | ||
>; | ||
export type ConnectionServiceDeleteConnectionMutationResult = Awaited< | ||
ReturnType<typeof ConnectionService.deleteConnection> | ||
>; |
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,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: | ||
@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"] |