-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example Airflow DAG that sends an email message through SendGrid (#7894)
* Example Airflow DAG that sends an email message through SendGrid * Fix region tag format * Fix Copyright header check * Remove daily scheduling and unneeded task name * Lint the sample * Add unit test * Fix imports in the unit test * Update composer/workflows/email_operator_sendgrid_test.py Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
- Loading branch information
1 parent
4762700
commit 0666a6a
Showing
2 changed files
with
73 additions
and
0 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,47 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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. | ||
|
||
|
||
"""Example Airflow DAG that sends an email message through SendGrid. | ||
You must configure email notifications so that Airflow sends email through | ||
SendGrid. For more information, see | ||
https://cloud.google.com/composer/docs/composer-2/configure-email#sendgrid | ||
""" | ||
|
||
# [START composer_email_operator_sendgrid] | ||
|
||
import datetime | ||
|
||
import airflow | ||
from airflow.operators.email import EmailOperator | ||
|
||
|
||
with airflow.DAG( | ||
"composer_sample_sendgrid", | ||
start_date=datetime.datetime(2022, 1, 1), | ||
) as dag: | ||
|
||
task_email = EmailOperator( | ||
task_id="send-email", | ||
conn_id="sendgrid_default", | ||
# You can specify more than one recipient with a list. | ||
to="user@example.com", | ||
subject="EmailOperator test for SendGrid", | ||
html_content="This is a test message sent through SendGrid.", | ||
dag=dag, | ||
) | ||
|
||
# [END composer_email_operator_sendgrid] |
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,26 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed 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 | ||
# | ||
# https://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. | ||
|
||
import internal_unit_testing | ||
|
||
|
||
def test_email_operator_sendgrid() -> None: | ||
"""Test that the DAG file can be successfully imported. | ||
This tests that the DAG can be parsed, but does not run it in an Airflow | ||
environment. This is a recommended confidence check by the official Airflow | ||
docs: https://airflow.incubator.apache.org/tutorial.html#testing | ||
""" | ||
from . import email_operator_sendgrid | ||
|
||
internal_unit_testing.assert_has_valid_dag(email_operator_sendgrid) |