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

Improve alert group deletion API #3124

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Make it possible to acknowledge/unacknowledge and resolve/unresolve alert groups via API by @vadimkerr ([#3108](https://github.com/grafana/oncall/pull/3108))

### Changed

- Improve alert group deletion API by @vadimkerr ([#3124](https://github.com/grafana/oncall/pull/3124))

## v1.3.42 (2023-10-04)

### Added
Expand Down
16 changes: 8 additions & 8 deletions docs/sources/oncall-api-reference/alertgroups.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ These available filter parameters should be provided as `GET` arguments:

`GET {{API_URL}}/api/v1/alert_groups/`

# Acknowledge alert groups
# Acknowledge an alert group

```shell
curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/acknowledge" \
Expand All @@ -66,7 +66,7 @@ curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/acknowledge" \

`POST {{API_URL}}/api/v1/alert_groups/<ALERT_GROUP_ID>/acknowledge`

# Unacknowledge alert groups
# Unacknowledge an alert group

```shell
curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/unacknowledge" \
Expand All @@ -78,7 +78,7 @@ curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/unacknowledge" \

`POST {{API_URL}}/api/v1/alert_groups/<ALERT_GROUP_ID>/unacknowledge`

# Resolve alert groups
# Resolve an alert group

```shell
curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/resolve" \
Expand All @@ -90,7 +90,7 @@ curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/resolve" \

`POST {{API_URL}}/api/v1/alert_groups/<ALERT_GROUP_ID>/resolve`

# Unresolve alert groups
# Unresolve an alert group

```shell
curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/unresolve" \
Expand All @@ -102,7 +102,7 @@ curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/unresolve" \

`POST {{API_URL}}/api/v1/alert_groups/<ALERT_GROUP_ID>/unresolve`

# Delete alert groups
# Delete an alert group

```shell
curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/" \
Expand All @@ -114,9 +114,9 @@ curl "{{API_URL}}/api/v1/alert_groups/I68T24C13IFW1/" \
}'
```

| Parameter | Required | Description |
| --------- | :------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `mode` | No | Default setting is `wipe`. `wipe` will remove the payload of all Grafana OnCall group alerts. This is useful if you sent sensitive data to OnCall. All metadata will remain. `DELETE` will trigger the removal of alert groups, alerts, and all related metadata. It will also remove alert group notifications in Slack and other destinations. |
| Parameter | Required | Description |
|-----------|:--------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mode` | No | The default value for this parameter is `wipe`. Using `wipe` will delete the content of the alert group but keep the metadata, which is helpful if you've sent sensitive information to OnCall. On the other hand, passing `delete` will fully erase the alert group and its metadata, as well as delete related messages in Slack and other platforms. |

> **NOTE:** `DELETE` can take a few moments to delete alert groups because Grafana OnCall interacts with 3rd party APIs
> such as Slack. Please check objects using `GET` to be sure the data is removed.
Expand Down
7 changes: 7 additions & 0 deletions engine/apps/alerts/tasks/wipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
)
def wipe(alert_group_pk, user_pk):
from apps.alerts.models import AlertGroup
from apps.api.serializers.alert import AlertFieldsCacheSerializerMixin
from apps.api.serializers.alert_group import AlertGroupFieldsCacheSerializerMixin
from apps.user_management.models import User

alert_group = AlertGroup.objects.filter(pk=alert_group_pk).first()
user = User.objects.filter(pk=user_pk).first()
alert_group.wipe_by_user(user)

# Clear internal API cache
AlertGroupFieldsCacheSerializerMixin.bust_object_caches(alert_group)
for alert in alert_group.alerts.all():
AlertFieldsCacheSerializerMixin.bust_object_caches(alert)
22 changes: 22 additions & 0 deletions engine/apps/alerts/tests/test_alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from apps.alerts.constants import ActionSource
from apps.alerts.incident_appearance.renderers.phone_call_renderer import AlertGroupPhoneCallRenderer
from apps.alerts.models import AlertGroup, AlertGroupLogRecord
from apps.alerts.tasks import wipe
from apps.alerts.tasks.delete_alert_group import delete_alert_group
from apps.slack.client import SlackClient
from apps.slack.errors import SlackAPIMessageNotFoundError, SlackAPIRatelimitError
Expand Down Expand Up @@ -50,6 +51,27 @@ def test_render_for_phone_call(
assert expected_verbose_name in rendered_text


@pytest.mark.django_db
def test_wipe(
make_organization_and_user,
make_alert_receive_channel,
make_alert_group,
make_alert,
):
organization, user = make_organization_and_user()
alert_receive_channel = make_alert_receive_channel(organization)
alert_group = make_alert_group(alert_receive_channel)
alert = make_alert(alert_group, raw_request_data={"test": 42})

wipe(alert_group.pk, user.pk)

alert_group.refresh_from_db()
alert.refresh_from_db()
assert alert_group.wiped_at is not None
assert alert_group.wiped_by == user
assert alert.raw_request_data == {}


@patch.object(SlackClient, "reactions_remove")
@patch.object(SlackClient, "chat_delete")
@pytest.mark.django_db
Expand Down
20 changes: 0 additions & 20 deletions engine/apps/alerts/tests/test_wipe.py

This file was deleted.

44 changes: 44 additions & 0 deletions engine/apps/api/tests/test_alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import patch

import pytest
from django.core.cache import cache
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
Expand All @@ -10,8 +11,11 @@

from apps.alerts.constants import ActionSource
from apps.alerts.models import AlertGroup, AlertGroupLogRecord
from apps.alerts.tasks import wipe
from apps.api.errors import AlertGroupAPIError
from apps.api.permissions import LegacyAccessControlRole
from apps.api.serializers.alert import AlertFieldsCacheSerializerMixin
from apps.api.serializers.alert_group import AlertGroupFieldsCacheSerializerMixin
from apps.base.models import UserNotificationPolicyLogRecord

alert_raw_request_data = {
Expand Down Expand Up @@ -1891,3 +1895,43 @@ def test_timeline_api_action(
assert response.status_code == status.HTTP_200_OK
assert response.json()["render_after_resolve_report_json"][0]["action"] == "acknowledged by {{author}}"
assert response.json()["render_after_resolve_report_json"][1]["action"] == "resolved by API"


@pytest.mark.django_db
def test_wipe_clears_cache(
make_organization_and_user_with_plugin_token,
make_alert_receive_channel,
make_channel_filter,
make_alert_group,
make_alert,
make_user_auth_headers,
):
"""Check that internal API cache is cleared when wiping an alert group"""
organization, user, token = make_organization_and_user_with_plugin_token()
alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(alert_receive_channel, is_default=True)
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)
alert = make_alert(alert_group=alert_group, raw_request_data=alert_raw_request_data)

# Populate cache
client = APIClient()
url = reverse("api-internal:alertgroup-detail", kwargs={"pk": alert_group.public_primary_key})
response = client.get(url, **make_user_auth_headers(user, token))
assert response.status_code == status.HTTP_200_OK

# Wipe alert group
wipe(alert_group.pk, user.pk)

# Check that cache is cleared for alert group
alert_group_cache_keys = [
AlertGroupFieldsCacheSerializerMixin.calculate_cache_key(field_name, alert_group)
for field_name in AlertGroupFieldsCacheSerializerMixin.ALL_FIELD_NAMES
]
assert not any([cache.get(key) for key in alert_group_cache_keys])

# Check that cache is cleared for alert
alert_cache_keys = [
AlertFieldsCacheSerializerMixin.calculate_cache_key(field_name, alert)
for field_name in AlertFieldsCacheSerializerMixin.ALL_FIELD_NAMES
]
assert not any([cache.get(key) for key in alert_cache_keys])
Loading