Skip to content
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

Remove deprecated code from Pagerduty provider #44653

Merged
merged 4 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions providers/src/airflow/providers/pagerduty/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
Changelog
---------

main
.....

.. warning::
All deprecated classes, parameters and features have been removed from the pagerduty provider package.
The following breaking changes were introduced:

* Removed deprecated method ``create_event`` from ``providers.pagerduty.hooks.pagerduty`` and ``providers.pagerduty.hooks.pagerduty_events``. Please use the ``airflow.providers.pagerduty.hooks.pagerduty_events.PagerdutyEventsHook.send_event`` to interact with the Events API

3.8.1
.....

Expand Down
71 changes: 1 addition & 70 deletions providers/src/airflow/providers/pagerduty/hooks/pagerduty.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
from typing import Any

import pdpyras
from deprecated import deprecated

from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
from airflow.exceptions import AirflowException
from airflow.hooks.base import BaseHook
from airflow.providers.pagerduty.hooks.pagerduty_events import PagerdutyEventsHook

Expand Down Expand Up @@ -110,74 +109,6 @@ def get_session(self) -> pdpyras.APISession:
self._session = pdpyras.APISession(self.token)
return self._session

@deprecated(
reason=(
"This method will be deprecated. Please use the "
"`airflow.providers.pagerduty.hooks.PagerdutyEventsHook` to interact with the Events API"
),
category=AirflowProviderDeprecationWarning,
)
def create_event(
self,
summary: str,
severity: str,
source: str = "airflow",
action: str = "trigger",
routing_key: str | None = None,
dedup_key: str | None = None,
custom_details: Any | None = None,
group: str | None = None,
component: str | None = None,
class_type: str | None = None,
images: list[Any] | None = None,
links: list[Any] | None = None,
) -> dict:
"""
Create event for service integration.

:param summary: Summary for the event
:param severity: Severity for the event, needs to be one of: info, warning, error, critical
:param source: Specific human-readable unique identifier, such as a
hostname, for the system having the problem.
:param action: Event action, needs to be one of: trigger, acknowledge,
resolve. Default to trigger if not specified.
:param routing_key: Integration key. If not specified, will try to read
from connection's extra json blob.
:param dedup_key: A string which identifies the alert triggered for the given event.
Required for the actions acknowledge and resolve.
:param custom_details: Free-form details from the event. Can be a dictionary or a string.
If a dictionary is passed it will show up in PagerDuty as a table.
:param group: A cluster or grouping of sources. For example, sources
"prod-datapipe-02" and "prod-datapipe-03" might both be part of "prod-datapipe"
:param component: The part or component of the affected system that is broken.
:param class_type: The class/type of the event.
:param images: List of images to include. Each dictionary in the list accepts the following keys:
`src`: The source (URL) of the image being attached to the incident. This image must be served via
HTTPS.
`href`: [Optional] URL to make the image a clickable link.
`alt`: [Optional] Alternative text for the image.
:param links: List of links to include. Each dictionary in the list accepts the following keys:
`href`: URL of the link to be attached.
`text`: [Optional] Plain text that describes the purpose of the link, and can be used as the
link's text.
:return: PagerDuty Events API v2 response.
"""
routing_key = routing_key or self.routing_key

return PagerdutyEventsHook(integration_key=routing_key).create_event(
summary=summary,
severity=severity,
source=source,
action=action,
dedup_key=dedup_key,
custom_details=custom_details,
group=group,
component=component,
class_type=class_type,
images=images,
links=links,
)

def test_connection(self):
try:
session = pdpyras.APISession(self.token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
from typing import TYPE_CHECKING, Any

import pdpyras
from deprecated import deprecated

from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
from airflow.exceptions import AirflowException
from airflow.hooks.base import BaseHook

if TYPE_CHECKING:
Expand Down Expand Up @@ -77,75 +76,6 @@ def __init__(
"Cannot get token: No valid integration key nor pagerduty_events_conn_id supplied."
)

@deprecated(
reason=(
"This method will be deprecated. Please use the "
"`PagerdutyEventsHook.send_event` to interact with the Events API"
),
category=AirflowProviderDeprecationWarning,
)
def create_event(
self,
summary: str,
severity: str,
source: str = "airflow",
action: str = "trigger",
dedup_key: str | None = None,
custom_details: Any | None = None,
group: str | None = None,
component: str | None = None,
class_type: str | None = None,
images: list[Any] | None = None,
links: list[Any] | None = None,
) -> dict:
"""
Create event for service integration.

:param summary: Summary for the event
:param severity: Severity for the event, needs to be one of: info, warning, error, critical
:param source: Specific human-readable unique identifier, such as a
hostname, for the system having the problem.
:param action: Event action, needs to be one of: trigger, acknowledge,
resolve. Default to trigger if not specified.
:param dedup_key: A string which identifies the alert triggered for the given event.
Required for the actions acknowledge and resolve.
:param custom_details: Free-form details from the event. Can be a dictionary or a string.
If a dictionary is passed it will show up in PagerDuty as a table.
:param group: A cluster or grouping of sources. For example, sources
"prod-datapipe-02" and "prod-datapipe-03" might both be part of "prod-datapipe"
:param component: The part or component of the affected system that is broken.
:param class_type: The class/type of the event.
:param images: List of images to include. Each dictionary in the list accepts the following keys:
`src`: The source (URL) of the image being attached to the incident. This image must be served via
HTTPS.
`href`: [Optional] URL to make the image a clickable link.
`alt`: [Optional] Alternative text for the image.
:param links: List of links to include. Each dictionary in the list accepts the following keys:
`href`: URL of the link to be attached.
`text`: [Optional] Plain text that describes the purpose of the link, and can be used as the
link's text.
:return: PagerDuty Events API v2 response.
"""
data = PagerdutyEventsHook.prepare_event_data(
summary=summary,
severity=severity,
source=source,
custom_details=custom_details,
component=component,
group=group,
class_type=class_type,
action=action,
dedup_key=dedup_key,
images=images,
links=links,
action_key_name="event_action",
)

session = pdpyras.EventsAPISession(self.integration_key)
resp = session.post("/v2/enqueue", json=data)
resp.raise_for_status()
return resp.json()

def send_event(
self,
summary: str,
Expand Down
49 changes: 0 additions & 49 deletions providers/tests/pagerduty/hooks/test_pagerduty.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
# under the License.
from __future__ import annotations

from unittest import mock

import pytest

from airflow.exceptions import AirflowProviderDeprecationWarning
from airflow.models import Connection
from airflow.providers.pagerduty.hooks.pagerduty import PagerdutyHook
from airflow.providers.pagerduty.hooks.pagerduty_events import PagerdutyEventsHook
Expand Down Expand Up @@ -79,49 +76,3 @@ def test_get_service(self, requests_mock):
session = hook.get_session()
resp = session.rget("/services/PZYX321")
assert resp == mock_response_body

@mock.patch.object(PagerdutyEventsHook, "__init__")
@mock.patch.object(PagerdutyEventsHook, "create_event")
def test_create_event(self, events_hook_create_event, events_hook_init):
events_hook_init.return_value = None
hook = PagerdutyHook(pagerduty_conn_id=DEFAULT_CONN_ID)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="This method will be deprecated. Please use the `airflow.providers.pagerduty.hooks.PagerdutyEventsHook` to interact with the Events API",
):
hook.create_event(
summary="test",
source="airflow_test",
severity="error",
)
events_hook_init.assert_called_with(integration_key="integration_key")
events_hook_create_event.assert_called_with(
summary="test",
source="airflow_test",
severity="error",
action="trigger",
dedup_key=None,
custom_details=None,
group=None,
component=None,
class_type=None,
images=None,
links=None,
)

@mock.patch.object(PagerdutyEventsHook, "create_event", mock.MagicMock(return_value=None))
@mock.patch.object(PagerdutyEventsHook, "__init__")
def test_create_event_override(self, events_hook_init):
events_hook_init.return_value = None
hook = PagerdutyHook(pagerduty_conn_id=DEFAULT_CONN_ID)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="This method will be deprecated. Please use the `airflow.providers.pagerduty.hooks.PagerdutyEventsHook` to interact with the Events API",
):
hook.create_event(
routing_key="different_key",
summary="test",
source="airflow_test",
severity="error",
)
events_hook_init.assert_called_with(integration_key="different_key")
39 changes: 0 additions & 39 deletions providers/tests/pagerduty/hooks/test_pagerduty_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import pytest

from airflow.exceptions import AirflowProviderDeprecationWarning
from airflow.models import Connection
from airflow.providers.pagerduty.hooks.pagerduty import PagerdutyEventsHook
from airflow.utils import db
Expand All @@ -43,25 +42,6 @@ def test_token_parameter_override(self, events_connections):
hook = PagerdutyEventsHook(integration_key="override_key", pagerduty_events_conn_id=DEFAULT_CONN_ID)
assert hook.integration_key == "override_key", "token initialised."

def test_create_event(self, requests_mock, events_connections):
hook = PagerdutyEventsHook(pagerduty_events_conn_id=DEFAULT_CONN_ID)
mock_response_body = {
"status": "success",
"message": "Event processed",
"dedup_key": "samplekeyhere",
}
requests_mock.post("https://events.pagerduty.com/v2/enqueue", json=mock_response_body)
with pytest.warns(
AirflowProviderDeprecationWarning,
match="This method will be deprecated. Please use the `PagerdutyEventsHook.send_event` to interact with the Events API",
):
resp = hook.create_event(
summary="test",
source="airflow_test",
severity="error",
)
assert resp == mock_response_body

def test_create_change_event(self, requests_mock, events_connections):
hook = PagerdutyEventsHook(pagerduty_events_conn_id=DEFAULT_CONN_ID)
change_event_id = "change_event_id"
Expand All @@ -81,22 +61,3 @@ def test_send_event(self, requests_mock, events_connections):
requests_mock.post("https://events.pagerduty.com/v2/enqueue", json=mock_response_body)
resp = hook.send_event(summary="test", source="airflow_test", severity="error", dedup_key=dedup_key)
assert resp == dedup_key

def test_create_event_deprecation_warning(self, requests_mock, events_connections):
hook = PagerdutyEventsHook(pagerduty_events_conn_id=DEFAULT_CONN_ID)
mock_response_body = {
"status": "success",
"message": "Event processed",
"dedup_key": "samplekeyhere",
}
requests_mock.post("https://events.pagerduty.com/v2/enqueue", json=mock_response_body)
warning = (
"This method will be deprecated. Please use the `PagerdutyEventsHook.send_event`"
" to interact with the Events API"
)
with pytest.warns(AirflowProviderDeprecationWarning, match=warning):
hook.create_event(
summary="test",
source="airflow_test",
severity="error",
)
Loading