-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIP-72: Add support for
outlet_events
in Task Context
part of #45717 This PR adds support for `outlet_events` in Context dict within the Task SDK by adding an endpoint on the API Server which is fetched when outlet_events is accessed.
- Loading branch information
Showing
17 changed files
with
679 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# 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 datetime import datetime | ||
|
||
from airflow.api_fastapi.core_api.base import BaseModel | ||
|
||
|
||
class DagScheduleAssetReference(BaseModel): | ||
"""DAG schedule reference schema for assets.""" | ||
|
||
dag_id: str | ||
created_at: datetime | ||
updated_at: datetime | ||
|
||
|
||
class TaskOutletAssetReference(BaseModel): | ||
"""Task outlet reference schema for assets.""" | ||
|
||
dag_id: str | ||
task_id: str | ||
created_at: datetime | ||
updated_at: datetime | ||
|
||
|
||
class AssetResponse(BaseModel): | ||
"""Asset schema for responses.""" | ||
|
||
id: int | ||
name: str | ||
uri: str | ||
group: str | ||
extra: dict | None = None | ||
created_at: datetime | ||
updated_at: datetime | ||
consuming_dags: list[DagScheduleAssetReference] | ||
producing_tasks: list[TaskOutletAssetReference] | ||
aliases: list[AssetAliasResponse] | ||
|
||
|
||
class AssetAliasResponse(BaseModel): | ||
"""Asset alias schema for responses.""" | ||
|
||
id: int | ||
name: str | ||
group: str |
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,79 @@ | ||
# 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 typing import Annotated | ||
|
||
from fastapi import HTTPException, Query, status | ||
from sqlalchemy import select | ||
|
||
from airflow.api_fastapi.common.db.common import SessionDep | ||
from airflow.api_fastapi.common.router import AirflowRouter | ||
from airflow.api_fastapi.execution_api.datamodels.asset import AssetResponse | ||
from airflow.models.asset import AssetModel | ||
|
||
# TODO: Add dependency on JWT token | ||
router = AirflowRouter( | ||
responses={ | ||
status.HTTP_404_NOT_FOUND: {"description": "Asset not found"}, | ||
status.HTTP_401_UNAUTHORIZED: {"description": "Unauthorized"}, | ||
}, | ||
) | ||
|
||
|
||
@router.get( | ||
"", | ||
responses={ | ||
status.HTTP_400_BAD_REQUEST: { | ||
"description": "Either 'name' or 'uri' query parameter must be provided" | ||
}, | ||
status.HTTP_403_FORBIDDEN: {"description": "Task does not have access to the variable"}, | ||
}, | ||
) | ||
def get_asset( | ||
session: SessionDep, | ||
name: Annotated[str | None, Query(description="The name of the Asset")] = None, | ||
uri: Annotated[str | None, Query(description="The URI of the Asset")] = None, | ||
) -> AssetResponse: | ||
"""Get an Airflow Asset by `name` or `uri`.""" | ||
if name: | ||
asset = session.scalar(select(AssetModel).where(AssetModel.name == name, AssetModel.active.has())) | ||
_raise_if_not_found(asset, f"Asset with name {name} not found") | ||
elif uri: | ||
asset = session.scalar(select(AssetModel).where(AssetModel.uri == uri, AssetModel.active.has())) | ||
_raise_if_not_found(asset, f"Asset with URI {uri} not found") | ||
else: | ||
raise HTTPException( | ||
status.HTTP_400_BAD_REQUEST, | ||
detail={ | ||
"reason": "bad_request", | ||
"message": "Either 'name' or 'uri' query parameter must be provided", | ||
}, | ||
) | ||
return AssetResponse.model_validate(asset) | ||
|
||
|
||
def _raise_if_not_found(asset, msg): | ||
if asset is None: | ||
raise HTTPException( | ||
status.HTTP_404_NOT_FOUND, | ||
detail={ | ||
"reason": "not_found", | ||
"message": msg, | ||
}, | ||
) |
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
Oops, something went wrong.