Skip to content
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

AIP-84 Migrate delete a connection to FastAPI API #42571

2 changes: 2 additions & 0 deletions airflow/api_connexion/endpoints/connection_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from airflow.secrets.environment_variables import CONN_ENV_PREFIX
from airflow.security import permissions
from airflow.utils import helpers
from airflow.utils.api_migration import mark_fastapi_migration_done
from airflow.utils.log.action_logger import action_event_from_permission
from airflow.utils.session import NEW_SESSION, provide_session
from airflow.utils.strings import get_random_string
Expand All @@ -53,6 +54,7 @@
RESOURCE_EVENT_PREFIX = "connection"


@mark_fastapi_migration_done
@security.requires_access_connection("DELETE")
@provide_session
@action_logging(
Expand Down
41 changes: 41 additions & 0 deletions airflow/api_fastapi/openapi/v1-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,47 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/connections/{connection_id}:
delete:
tags:
- CONNECTION
summary: Delete Connection
description: Delete a connection entry.
operationId: delete_connection_public_connections__connection_id__delete
parameters:
- name: connection_id
in: path
required: true
schema:
type: string
title: Connection Id
responses:
'204':
description: Successful Response
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Unauthorized
'403':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Forbidden
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Not Found
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
components:
schemas:
DAGCollectionResponse:
Expand Down
2 changes: 2 additions & 0 deletions airflow/api_fastapi/views/public/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

from fastapi import APIRouter

from airflow.api_fastapi.views.public.connections import connections_router
from airflow.api_fastapi.views.public.dags import dags_router

public_router = APIRouter(prefix="/public")


public_router.include_router(dags_router)
public_router.include_router(connections_router)
47 changes: 47 additions & 0 deletions airflow/api_fastapi/views/public/connections.py
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 APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import Session
from typing_extensions import Annotated

from airflow.api_fastapi.db import get_session
from airflow.api_fastapi.openapi.exceptions import create_openapi_http_exception_doc
from airflow.models import Connection

connections_router = APIRouter(tags=["CONNECTION"])
bugraoz93 marked this conversation as resolved.
Show resolved Hide resolved


@connections_router.delete(
"/connections/{connection_id}",
status_code=204,
responses=create_openapi_http_exception_doc([401, 403, 404]),
)
async def delete_connection(
*,
bugraoz93 marked this conversation as resolved.
Show resolved Hide resolved
connection_id: str,
session: Annotated[Session, Depends(get_session)],
):
pierrejeambrun marked this conversation as resolved.
Show resolved Hide resolved
"""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)
12 changes: 11 additions & 1 deletion airflow/ui/openapi-gen/queries/common.ts
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 { DagService, DatasetService } from "../requests/services.gen";
import {
ConnectionService,
DagService,
DatasetService,
} from "../requests/services.gen";
import { DagRunState } from "../requests/types.gen";

export type DatasetServiceNextRunDatasetsUiNextRunDatasetsDagIdGetDefaultResponse =
Expand Down Expand Up @@ -79,3 +83,9 @@ export const UseDagServiceGetDagsPublicDagsGetKeyFn = (
export type DagServicePatchDagPublicDagsDagIdPatchMutationResult = Awaited<
ReturnType<typeof DagService.patchDagPublicDagsDagIdPatch>
>;
export type ConnectionServiceDeleteConnectionPublicConnectionsConnectionIdDeleteMutationResult =
Awaited<
ReturnType<
typeof ConnectionService.deleteConnectionPublicConnectionsConnectionIdDelete
>
>;
46 changes: 45 additions & 1 deletion airflow/ui/openapi-gen/queries/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
UseQueryOptions,
} from "@tanstack/react-query";

import { DagService, DatasetService } from "../requests/services.gen";
import {
ConnectionService,
DagService,
DatasetService,
} from "../requests/services.gen";
import { DAGPatchBody, DagRunState } from "../requests/types.gen";
import * as Common from "./common";

Expand Down Expand Up @@ -168,3 +172,43 @@ export const useDagServicePatchDagPublicDagsDagIdPatch = <
}) as unknown as Promise<TData>,
...options,
});
/**
* Delete Connection
* Delete a connection entry.
* @param data The data for the request.
* @param data.connectionId
* @returns void Successful Response
* @throws ApiError
*/
export const useConnectionServiceDeleteConnectionPublicConnectionsConnectionIdDelete =
<
TData = Common.ConnectionServiceDeleteConnectionPublicConnectionsConnectionIdDeleteMutationResult,
TError = unknown,
TContext = unknown,
>(
options?: Omit<
UseMutationOptions<
TData,
TError,
{
connectionId: string;
},
TContext
>,
"mutationFn"
>,
) =>
useMutation<
TData,
TError,
{
connectionId: string;
},
TContext
>({
mutationFn: ({ connectionId }) =>
ConnectionService.deleteConnectionPublicConnectionsConnectionIdDelete({
connectionId,
}) as unknown as Promise<TData>,
...options,
});
30 changes: 30 additions & 0 deletions airflow/ui/openapi-gen/requests/services.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
GetDagsPublicDagsGetResponse,
PatchDagPublicDagsDagIdPatchData,
PatchDagPublicDagsDagIdPatchResponse,
DeleteConnectionPublicConnectionsConnectionIdDeleteData,
DeleteConnectionPublicConnectionsConnectionIdDeleteResponse,
} from "./types.gen";

export class DatasetService {
Expand Down Expand Up @@ -111,3 +113,31 @@ export class DagService {
});
}
}

export class ConnectionService {
/**
* Delete Connection
* Delete a connection entry.
* @param data The data for the request.
* @param data.connectionId
* @returns void Successful Response
* @throws ApiError
*/
public static deleteConnectionPublicConnectionsConnectionIdDelete(
data: DeleteConnectionPublicConnectionsConnectionIdDeleteData,
): CancelablePromise<DeleteConnectionPublicConnectionsConnectionIdDeleteResponse> {
return __request(OpenAPI, {
method: "DELETE",
url: "/public/connections/{connection_id}",
path: {
connection_id: data.connectionId,
},
errors: {
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
422: "Validation Error",
},
});
}
}
33 changes: 33 additions & 0 deletions airflow/ui/openapi-gen/requests/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export type PatchDagPublicDagsDagIdPatchData = {

export type PatchDagPublicDagsDagIdPatchResponse = DAGResponse;

export type DeleteConnectionPublicConnectionsConnectionIdDeleteData = {
connectionId: string;
};

export type DeleteConnectionPublicConnectionsConnectionIdDeleteResponse = void;

export type $OpenApiTs = {
"/ui/next_run_datasets/{dag_id}": {
get: {
Expand Down Expand Up @@ -183,4 +189,31 @@ export type $OpenApiTs = {
};
};
};
"/public/connections/{connection_id}": {
delete: {
req: DeleteConnectionPublicConnectionsConnectionIdDeleteData;
res: {
/**
* Successful Response
*/
204: void;
/**
* Unauthorized
*/
401: HTTPExceptionResponse;
/**
* Forbidden
*/
403: HTTPExceptionResponse;
/**
* Not Found
*/
404: HTTPExceptionResponse;
/**
* Validation Error
*/
422: HTTPValidationError;
};
};
};
};
63 changes: 63 additions & 0 deletions tests/api_fastapi/views/public/test_connections.py
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

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.
Show resolved Hide resolved
@pytest.fixture(autouse=True)
def setup_attrs(self) -> None:
bugraoz93 marked this conversation as resolved.
Show resolved Hide resolved
clear_db_connections(False)

def teardown_method(self) -> None:
clear_db_connections()

def create_connection(self):
_create_connection()


class TestDeleteConnection(TestConnectionEndpoint):
@pytest.mark.db_test
bugraoz93 marked this conversation as resolved.
Show resolved Hide resolved
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

@pytest.mark.db_test
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"]