|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +""" |
| 18 | +Example DAG demonstrating cross-team asset triggering with ``allow_teams``. |
| 19 | +
|
| 20 | +When Multi-Team mode is enabled (``[core] multi_team = True``), asset events are filtered by team |
| 21 | +membership. By default, a consuming DAG only receives events from DAGs within the same team. |
| 22 | +
|
| 23 | +Usage: |
| 24 | + - ``team_analytics_producer`` (belonging to ``team_analytics``) produces events on ``shared_data``. |
| 25 | + - ``team_ml_consumer`` (belonging to ``team_ml``) consumes ``shared_data``. |
| 26 | + - Because ``shared_data`` has ``allow_teams=["team_analytics"]``, events from ``team_analytics`` |
| 27 | + are accepted by ``team_ml_consumer``. |
| 28 | + - Without ``allow_teams``, the cross-team event would be blocked. |
| 29 | +""" |
| 30 | + |
| 31 | +from __future__ import annotations |
| 32 | + |
| 33 | +import pendulum |
| 34 | + |
| 35 | +from airflow.providers.standard.operators.bash import BashOperator |
| 36 | +from airflow.sdk import DAG, Asset |
| 37 | + |
| 38 | +# [START asset_allow_teams] |
| 39 | +# Define an asset that accepts events from team_analytics in addition to the consumer's own team. |
| 40 | +shared_data = Asset( |
| 41 | + name="shared_data", |
| 42 | + uri="s3://data-lake/shared/output.csv", |
| 43 | + allow_teams=["team_analytics"], |
| 44 | +) |
| 45 | + |
| 46 | +# Producer DAG — belongs to team_analytics (via its DAG bundle configuration). |
| 47 | +# When this DAG's task completes, it emits an asset event for shared_data. |
| 48 | +with DAG( |
| 49 | + dag_id="team_analytics_producer", |
| 50 | + start_date=pendulum.datetime(2024, 1, 1, tz="UTC"), |
| 51 | + schedule="@daily", |
| 52 | + catchup=False, |
| 53 | + tags=["team_analytics", "produces", "asset-scheduled", "allow-teams"], |
| 54 | +) as producer_dag: |
| 55 | + BashOperator( |
| 56 | + task_id="produce_shared_data", |
| 57 | + outlets=[shared_data], |
| 58 | + bash_command="echo 'Producing shared data for cross-team consumption'", |
| 59 | + ) |
| 60 | + |
| 61 | +# Consumer DAG — belongs to team_ml (via its DAG bundle configuration). |
| 62 | +# This DAG is triggered when shared_data is updated. Because shared_data has |
| 63 | +# allow_teams=["team_analytics"], events from team_analytics are accepted. |
| 64 | +with DAG( |
| 65 | + dag_id="team_ml_consumer", |
| 66 | + start_date=pendulum.datetime(2024, 1, 1, tz="UTC"), |
| 67 | + schedule=[shared_data], |
| 68 | + catchup=False, |
| 69 | + tags=["team_ml", "consumes", "asset-scheduled", "allow-teams"], |
| 70 | +) as consumer_dag: |
| 71 | + BashOperator( |
| 72 | + task_id="consume_shared_data", |
| 73 | + bash_command="echo 'Consuming shared data from team_analytics'", |
| 74 | + ) |
| 75 | +# [END asset_allow_teams] |
0 commit comments