Merge pull request #359 from grafana/matiasb/messaging-backends-explicit-enum

Make messaging backends enum be defined explicitly
This commit is contained in:
Matias Bordese 2022-08-22 12:14:46 -03:00 committed by GitHub
commit c73df126cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 17 deletions

View file

@ -176,7 +176,8 @@ class UserNotificationPolicyView(UpdateSerializerMixin, ModelViewSet):
continue
# extra backends may be enabled per organization
if notification_channel.name not in BUILT_IN_BACKENDS:
built_in_backend_names = {b[0] for b in BUILT_IN_BACKENDS}
if notification_channel.name not in built_in_backend_names:
extra_messaging_backend = get_messaging_backend_from_id(notification_channel.name)
if extra_messaging_backend is None:
continue

View file

@ -9,6 +9,9 @@ class BaseMessagingBackend:
available_for_use = False
templater = None
def __init__(self, *args, **kwargs):
self.notification_channel_id = kwargs.get("notification_channel_id")
def get_templater_class(self):
if self.templater:
return import_string(self.templater)
@ -46,16 +49,16 @@ class BaseMessagingBackend:
raise NotImplementedError("notify_user method missing implementation")
def load_backend(path):
return import_string(path)()
def load_backend(path, *args, **kwargs):
return import_string(path)(*args, **kwargs)
def get_messaging_backends():
global _messaging_backends
if _messaging_backends is None:
_messaging_backends = {}
for backend_path in settings.EXTRA_MESSAGING_BACKENDS:
backend = load_backend(backend_path)
for (backend_path, notification_channel_id) in settings.EXTRA_MESSAGING_BACKENDS:
backend = load_backend(backend_path, notification_channel_id=notification_channel_id)
_messaging_backends[backend.backend_id] = backend
return _messaging_backends.items()

View file

@ -1,3 +1,4 @@
from enum import unique
from typing import Tuple
from django.conf import settings
@ -30,13 +31,13 @@ def generate_public_primary_key_for_notification_policy():
# base supported notification backends
BUILT_IN_BACKENDS = (
"SLACK",
"SMS",
"PHONE_CALL",
"TELEGRAM",
"EMAIL",
"MOBILE_PUSH_GENERAL",
"MOBILE_PUSH_CRITICAL",
("SLACK", 0),
("SMS", 1),
("PHONE_CALL", 2),
("TELEGRAM", 3),
("EMAIL", 4),
("MOBILE_PUSH_GENERAL", 5),
("MOBILE_PUSH_CRITICAL", 6),
)
@ -49,10 +50,10 @@ def _notification_channel_choices():
# use NotificationChannelOptions.AVAILABLE_FOR_USE instead.
supported_backends = list(BUILT_IN_BACKENDS)
for backend_id, _ in get_messaging_backends():
supported_backends.append(backend_id)
for backend_id, backend in get_messaging_backends():
supported_backends.append((backend_id, backend.notification_channel_id))
channels_enum = models.IntegerChoices("NotificationChannel", supported_backends, start=0)
channels_enum = unique(models.IntegerChoices("NotificationChannel", supported_backends))
return channels_enum

View file

@ -39,4 +39,4 @@ SENDGRID_SECRET_KEY = "dummy_sendgrid_secret_key"
TWILIO_ACCOUNT_SID = "dummy_twilio_account_sid"
TWILIO_AUTH_TOKEN = "dummy_twilio_auth_token"
EXTRA_MESSAGING_BACKENDS = ["apps.base.tests.messaging_backend.TestOnlyBackend"]
EXTRA_MESSAGING_BACKENDS = [("apps.base.tests.messaging_backend.TestOnlyBackend", 42)]

View file

@ -91,6 +91,6 @@ SWAGGER_SETTINGS = {
}
if TESTING:
EXTRA_MESSAGING_BACKENDS = ["apps.base.tests.messaging_backend.TestOnlyBackend"]
EXTRA_MESSAGING_BACKENDS = [("apps.base.tests.messaging_backend.TestOnlyBackend", 42)]
TELEGRAM_TOKEN = "0000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXX"
TWILIO_AUTH_TOKEN = "twilio_auth_token"