-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Add ChimeNotifier
#32222
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
Merged
Merged
Add ChimeNotifier
#32222
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d00f33b
Create Chime Notifier
cjames23 eeeadfc
Fix missing init for Chime Notification test folder
cjames23 2f920c6
Rename chime notifier test file
cjames23 6d5824e
Move Chime Notifier template fields to class attr
cjames23 21166e4
Fix Chime Notifier template fields
cjames23 f7e77eb
Fix doc refs for Chime Notifier
cjames23 a3ff7c4
Merge branch 'main' into chime-notifier-feature
cjames23 85c4e1d
Fix Chime notifications index.rst
cjames23 95a861a
Merge branch 'main' into chime-notifier-feature
cjames23 b12fc42
Merge branch 'main' into chime-notifier-feature
cjames23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| # 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. |
This file contains hidden or 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,61 @@ | ||
| # 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 functools import cached_property | ||
|
|
||
| from airflow.exceptions import AirflowOptionalProviderFeatureException | ||
| from airflow.providers.amazon.aws.hooks.chime import ChimeWebhookHook | ||
| from airflow.utils.context import Context | ||
|
|
||
| try: | ||
| from airflow.notifications.basenotifier import BaseNotifier | ||
| except ImportError: | ||
| raise AirflowOptionalProviderFeatureException( | ||
| "Failed to import BaseNotifier. This feature is only available in Airflow versions >= 2.6.0" | ||
| ) | ||
|
|
||
|
|
||
| class ChimeNotifier(BaseNotifier): | ||
| """ | ||
| Chime notifier to send messages to a chime room via callbacks. | ||
|
|
||
| :param: chime_conn_id: The chime connection to use with Endpoint as "https://hooks.chime.aws" and | ||
| the webhook token in the form of ```{webhook.id}?token{webhook.token}``` | ||
| :param: message: The message to send to the chime room associated with the webhook. | ||
|
|
||
| """ | ||
|
|
||
| template_fields = ("message",) | ||
|
|
||
| def __init__(self, *, chime_conn_id: str, message: str = "This is the default chime notifier message"): | ||
| super().__init__() | ||
| self.chime_conn_id = chime_conn_id | ||
| self.message = message | ||
|
|
||
| @cached_property | ||
| def hook(self): | ||
| """To reduce overhead cache the hook for the notifier.""" | ||
| return ChimeWebhookHook(chime_conn_id=self.chime_conn_id) | ||
|
|
||
| def notify(self, context: Context) -> None: | ||
| """Send a message to a Chime Chat Room.""" | ||
| self.hook.send_message(message=self.message) | ||
|
|
||
|
|
||
| send_chime_notification = ChimeNotifier | ||
This file contains hidden or 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 hidden or 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
53 changes: 53 additions & 0 deletions
53
docs/apache-airflow-providers-amazon/notifications/chime_notifier_howto_guide.rst
This file contains hidden or 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,53 @@ | ||
| .. 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. | ||
|
|
||
| How-to Guide for Chime notifications | ||
| ==================================== | ||
|
|
||
| Introduction | ||
| ------------ | ||
| Chime notifier (:class:`airflow.providers.amazon.aws.notifications.chime.ChimeNotifier`) allows users to send | ||
| messages to a Chime chat room setup via a webhook using the various ``on_*_callbacks`` at both the DAG level and Task level | ||
|
|
||
| You can also use a notifier with ``sla_miss_callback``. | ||
|
|
||
| .. note:: | ||
| When notifiers are used with `sla_miss_callback` the context will contain only values passed to the callback, refer :ref:`sla_miss_callback<concepts:sla_miss_callback>`. | ||
|
|
||
| Example Code: | ||
| ------------- | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from datetime import datetime | ||
| from airflow import DAG | ||
| from airflow.operators.bash import BashOperator | ||
| from airflow.providers.amazon.aws.notifications.chime import send_chime_notification | ||
|
|
||
| with DAG( | ||
| start_date=datetime(2023, 6, 27), | ||
| on_success_callback=[ | ||
| send_chime_notification(chime_conn_id="my_chime_conn", message="The DAG {{ dag.dag_id }} succeeded") | ||
| ], | ||
| ): | ||
| BashOperator( | ||
| task_id="mytask", | ||
| on_failure_callback=[ | ||
| send_chime_notification(chime_conn_id="my_chime_conn", message="The task {{ ti.task_id }} failed") | ||
| ], | ||
| bash_command="fail", | ||
| ) |
28 changes: 28 additions & 0 deletions
28
docs/apache-airflow-providers-amazon/notifications/index.rst
This file contains hidden or 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,28 @@ | ||
| .. 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. | ||
|
|
||
|
|
||
|
|
||
| Amazon AWS Notifications | ||
| ======================== | ||
|
|
||
|
|
||
| .. toctree:: | ||
| :maxdepth: 1 | ||
| :glob: | ||
|
|
||
| * |
This file contains hidden or 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,16 @@ | ||
| # 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. |
This file contains hidden or 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,73 @@ | ||
| # 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 unittest import mock | ||
|
|
||
| from airflow.models import Connection | ||
| from airflow.operators.empty import EmptyOperator | ||
| from airflow.providers.amazon.aws.hooks.chime import ChimeWebhookHook | ||
| from airflow.providers.amazon.aws.notifications.chime import ChimeNotifier, send_chime_notification | ||
| from airflow.utils import db | ||
|
|
||
|
|
||
| class TestChimeNotifier: | ||
| # Chime webhooks can't really have a default connection, so we need to create one for tests. | ||
| def setup_method(self): | ||
| db.merge_conn( | ||
| Connection( | ||
| conn_id="default-chime-webhook", | ||
| conn_type="chime", | ||
| host="hooks.chime.aws/incomingwebhooks/", | ||
| password="abcd-1134-ZeDA?token=somechimetoken111", | ||
| schema="https", | ||
| ) | ||
| ) | ||
|
|
||
| @mock.patch.object(ChimeWebhookHook, "send_message") | ||
| def test_chime_notifier(self, mock_chime_hook, dag_maker): | ||
| with dag_maker("test_chime_notifier") as dag: | ||
| EmptyOperator(task_id="task1") | ||
|
|
||
| notifier = send_chime_notification( | ||
| chime_conn_id="default-chime-webhook", message="Chime Test Message" | ||
| ) | ||
| notifier({"dag": dag}) | ||
| mock_chime_hook.assert_called_once_with(message="Chime Test Message") | ||
|
|
||
| @mock.patch.object(ChimeWebhookHook, "send_message") | ||
| def test_chime_notifier_with_notifier_class(self, mock_chime_hook, dag_maker): | ||
| with dag_maker("test_chime_notifier") as dag: | ||
| EmptyOperator(task_id="task1") | ||
|
|
||
| notifier = ChimeNotifier( | ||
| chime_conn_id="default-chime-webhook", message="Test Chime Message for Class" | ||
| ) | ||
| notifier({"dag": dag}) | ||
| mock_chime_hook.assert_called_once_with(message="Test Chime Message for Class") | ||
|
|
||
| @mock.patch.object(ChimeWebhookHook, "send_message") | ||
| def test_chime_notifier_templated(self, mock_chime_hook, dag_maker): | ||
| with dag_maker("test_chime_notifier") as dag: | ||
| EmptyOperator(task_id="task1") | ||
|
|
||
| notifier = send_chime_notification( | ||
| chime_conn_id="default-chime-webhook", message="Test Chime Message. Dag is {{ dag.dag_id }}." | ||
| ) | ||
| notifier({"dag": dag}) | ||
| mock_chime_hook.assert_called_once_with(message="Test Chime Message. Dag is test_chime_notifier.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we name the directory
notifiersinstead? To be consistent withoperators,hooks, ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notifications was consistent with how other providers have named such as Slack and Discord. But I am open to renaming if there is consensus.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets stay with the current convention for now.
Notifiers is a new experience and I prefer to gather feedback before we start deprecating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good