Skip to content

Commit

Permalink
Example Airflow DAG that sends an email message through SendGrid (#7894)
Browse files Browse the repository at this point in the history
* 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
pavel-salnikov and leahecole authored Aug 30, 2022
1 parent 4762700 commit 0666a6a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
47 changes: 47 additions & 0 deletions composer/workflows/email_operator_sendgrid.py
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]
26 changes: 26 additions & 0 deletions composer/workflows/email_operator_sendgrid_test.py
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)

0 comments on commit 0666a6a

Please sign in to comment.