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: Migrating DELETE a queued asset events for DAG to fastAPI #44130

Merged
1 change: 1 addition & 0 deletions airflow/api_connexion/endpoints/asset_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def get_dag_asset_queued_event(
return queued_event_schema.dump(queued_event)


@mark_fastapi_migration_done
@security.requires_access_asset("DELETE")
@security.requires_access_dag("GET")
@provide_session
Expand Down
61 changes: 61 additions & 0 deletions airflow/api_fastapi/core_api/openapi/v1-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,67 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/dags/{dag_id}/assets/queuedEvent/{uri}:
delete:
tags:
- Asset
summary: Delete Dag Asset Queued Event
description: Delete a queued asset event for a DAG.
operationId: delete_dag_asset_queued_event
parameters:
- name: dag_id
in: path
required: true
schema:
type: string
title: Dag Id
- name: uri
in: path
required: true
schema:
type: string
title: Uri
- name: before
in: query
required: false
schema:
anyOf:
- type: string
- type: 'null'
title: Before
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
'400':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Bad Request
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPExceptionResponse'
description: Not Found
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
/public/backfills/:
get:
tags:
Expand Down
31 changes: 30 additions & 1 deletion airflow/api_fastapi/core_api/routes/public/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from typing import Annotated

from fastapi import Depends, HTTPException, status
from sqlalchemy import select
from sqlalchemy import delete, select
from sqlalchemy.orm import Session, joinedload, subqueryload

from airflow.api_fastapi.common.db.common import get_session, paginated_select
Expand Down Expand Up @@ -252,3 +252,32 @@ def get_dag_asset_queued_events(
],
total_entries=total_entries,
)


@assets_router.delete(
"/dags/{dag_id}/assets/queuedEvent/{uri:path}",
status_code=status.HTTP_204_NO_CONTENT,
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_404_NOT_FOUND,
]
),
)
def delete_dag_asset_queued_event(
dag_id: str,
uri: str,
session: Annotated[Session, Depends(get_session)],
before: OptionalDateTimeQuery = None,
):
"""Delete a queued asset event for a DAG."""
where_clause = _generate_queued_event_where_clause(dag_id=dag_id, before=before, uri=uri)
delete_statement = (
delete(AssetDagRunQueue).where(*where_clause).execution_options(synchronize_session="fetch")
)
result = session.execute(delete_statement)
if result.rowcount == 0:
raise HTTPException(
status.HTTP_404_NOT_FOUND,
detail=f"Queue event with dag_id: `{dag_id}` and asset uri: `{uri}` was not found",
)
3 changes: 3 additions & 0 deletions airflow/ui/openapi-gen/queries/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,9 @@ export type PoolServicePatchPoolMutationResult = Awaited<
export type VariableServicePatchVariableMutationResult = Awaited<
ReturnType<typeof VariableService.patchVariable>
>;
export type AssetServiceDeleteDagAssetQueuedEventMutationResult = Awaited<
ReturnType<typeof AssetService.deleteDagAssetQueuedEvent>
>;
export type ConnectionServiceDeleteConnectionMutationResult = Awaited<
ReturnType<typeof ConnectionService.deleteConnection>
>;
Expand Down
47 changes: 47 additions & 0 deletions airflow/ui/openapi-gen/queries/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,53 @@ export const useVariableServicePatchVariable = <
}) as unknown as Promise<TData>,
...options,
});
/**
* Delete Dag Asset Queued Event
* Delete a queued asset event for a DAG.
* @param data The data for the request.
* @param data.dagId
* @param data.uri
* @param data.before
* @returns void Successful Response
* @throws ApiError
*/
export const useAssetServiceDeleteDagAssetQueuedEvent = <
TData = Common.AssetServiceDeleteDagAssetQueuedEventMutationResult,
TError = unknown,
TContext = unknown,
>(
options?: Omit<
UseMutationOptions<
TData,
TError,
{
before?: string;
dagId: string;
uri: string;
},
TContext
>,
"mutationFn"
>,
) =>
useMutation<
TData,
TError,
{
before?: string;
dagId: string;
uri: string;
},
TContext
>({
mutationFn: ({ before, dagId, uri }) =>
AssetService.deleteDagAssetQueuedEvent({
before,
dagId,
uri,
}) as unknown as Promise<TData>,
...options,
});
/**
* Delete Connection
* Delete a connection entry.
Expand Down
35 changes: 35 additions & 0 deletions airflow/ui/openapi-gen/requests/services.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {
GetAssetResponse,
GetDagAssetQueuedEventsData,
GetDagAssetQueuedEventsResponse,
DeleteDagAssetQueuedEventData,
DeleteDagAssetQueuedEventResponse,
HistoricalMetricsData,
HistoricalMetricsResponse,
RecentDagRunsData,
Expand Down Expand Up @@ -306,6 +308,39 @@ export class AssetService {
},
});
}

/**
* Delete Dag Asset Queued Event
* Delete a queued asset event for a DAG.
* @param data The data for the request.
* @param data.dagId
* @param data.uri
* @param data.before
* @returns void Successful Response
* @throws ApiError
*/
public static deleteDagAssetQueuedEvent(
data: DeleteDagAssetQueuedEventData,
): CancelablePromise<DeleteDagAssetQueuedEventResponse> {
return __request(OpenAPI, {
method: "DELETE",
url: "/public/dags/{dag_id}/assets/queuedEvent/{uri}",
path: {
dag_id: data.dagId,
uri: data.uri,
},
query: {
before: data.before,
},
errors: {
400: "Bad Request",
401: "Unauthorized",
403: "Forbidden",
404: "Not Found",
422: "Validation Error",
},
});
}
}

export class DashboardService {
Expand Down
39 changes: 39 additions & 0 deletions airflow/ui/openapi-gen/requests/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,14 @@ export type GetDagAssetQueuedEventsData = {

export type GetDagAssetQueuedEventsResponse = QueuedEventCollectionResponse;

export type DeleteDagAssetQueuedEventData = {
before?: string | null;
dagId: string;
uri: string;
};

export type DeleteDagAssetQueuedEventResponse = void;

export type HistoricalMetricsData = {
endDate: string;
startDate: string;
Expand Down Expand Up @@ -1692,6 +1700,37 @@ export type $OpenApiTs = {
};
};
};
"/public/dags/{dag_id}/assets/queuedEvent/{uri}": {
delete: {
req: DeleteDagAssetQueuedEventData;
res: {
/**
* Successful Response
*/
204: void;
/**
* Bad Request
*/
400: HTTPExceptionResponse;
/**
* Unauthorized
*/
401: HTTPExceptionResponse;
/**
* Forbidden
*/
403: HTTPExceptionResponse;
/**
* Not Found
*/
404: HTTPExceptionResponse;
/**
* Validation Error
*/
422: HTTPValidationError;
};
};
};
"/ui/dashboard/historical_metrics_data": {
get: {
req: HistoricalMetricsData;
Expand Down
35 changes: 35 additions & 0 deletions tests/api_fastapi/core_api/routes/public/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,41 @@ def test_should_respond_404(self, test_client):
assert response.json()["detail"] == "Queue event with dag_id: `not_exists` was not found"


class TestDeleteDagAssetQueuedEvent(TestQueuedEventEndpoint):
def test_delete_should_respond_204(self, test_client, session, create_dummy_dag):
dag, _ = create_dummy_dag()
dag_id = dag.dag_id
asset_uri = "s3://bucket/key/1"
self.create_assets(session=session, num=1)
asset_id = 1

self._create_asset_dag_run_queues(dag_id, asset_id, session)
adrq = session.query(AssetDagRunQueue).all()
assert len(adrq) == 1

response = test_client.delete(
f"/public/dags/{dag_id}/assets/queuedEvent/{asset_uri}",
)

assert response.status_code == 204
adrq = session.query(AssetDagRunQueue).all()
assert len(adrq) == 0

def test_should_respond_404(self, test_client):
dag_id = "not_exists"
asset_uri = "not_exists"

response = test_client.delete(
f"/public/dags/{dag_id}/assets/queuedEvent/{asset_uri}",
)

assert response.status_code == 404
assert (
response.json()["detail"]
== "Queue event with dag_id: `not_exists` and asset uri: `not_exists` was not found"
)


class TestPostAssetEvents(TestAssets):
@pytest.mark.usefixtures("time_freezer")
def test_should_respond_200(self, test_client, session):
Expand Down