Skip to content

Commit

Permalink
Add a pre save reciever to ensure the data_source.type is a valid key…
Browse files Browse the repository at this point in the history
… in the registry.
  • Loading branch information
saponifi3d committed Dec 14, 2024
1 parent 3273e16 commit e2cfa38
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/sentry/workflow_engine/models/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import Generic, TypeVar

from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver

from sentry.backup.scopes import RelocationScope
from sentry.db.models import (
Expand All @@ -11,6 +13,7 @@
FlexibleForeignKey,
region_silo_model,
)
from sentry.utils.registry import NoRegistrationExistsError
from sentry.workflow_engine.models.data_source_detector import DataSourceDetector
from sentry.workflow_engine.registry import data_source_type_registry
from sentry.workflow_engine.types import DataSourceTypeHandler
Expand All @@ -33,7 +36,7 @@ class DataSource(DefaultFieldsModel):
# Should this be a string so we can support UUID / ints?
query_id = BoundedBigIntegerField()

# TODO - Add a type here
# This is a dynamic field, depending on the type in the data_source_type_registry
type = models.TextField()

detectors = models.ManyToManyField("workflow_engine.Detector", through=DataSourceDetector)
Expand All @@ -49,3 +52,19 @@ def type_handler(self) -> builtins.type[DataSourceTypeHandler]:
if not handler:
raise ValueError(f"Unknown data source type: {self.type}")
return handler


@receiver(pre_save, sender=DataSource)
def ensure_valid_type(sender, instance: DataSource, **kwargs):
"""
Ensure that the type of the data source is valid and registered in the data_source_type_registry
"""
data_source_type = instance.type

if not data_source_type:
raise ValueError(f"No group type found with type {instance.type}")

try:
data_source_type_registry.get(data_source_type)
except NoRegistrationExistsError:
raise ValueError(f"No data source type found with type {data_source_type}")

0 comments on commit e2cfa38

Please sign in to comment.