Skip to content

Commit

Permalink
Add filtering_term_as_jinja2 route field, add convert_from_regex_to_j…
Browse files Browse the repository at this point in the history
…inja2 action to route on private api
  • Loading branch information
iskhakov committed Apr 11, 2023
1 parent 625a023 commit f9473f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions engine/apps/api/serializers/channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ChannelFilterSerializer(OrderedModelSerializerMixin, EagerLoadingMixin, se
queryset=TelegramToOrganizationConnector.objects, filter_field="organization", allow_null=True, required=False
)
order = serializers.IntegerField(required=False)
filtering_term_as_jinja2 = serializers.SerializerMethodField()

SELECT_RELATED = ["escalation_chain", "alert_receive_channel"]

Expand All @@ -45,6 +46,7 @@ class Meta:
"notify_in_slack",
"notify_in_telegram",
"notification_backends",
"filtering_term_as_jinja2",
]
read_only_fields = ["created_at", "is_default"]
extra_kwargs = {"filtering_term": {"required": True, "allow_null": False}}
Expand Down Expand Up @@ -107,6 +109,16 @@ def validate_notification_backends(self, notification_backends):
notification_backends = updated
return notification_backends

def get_filtering_term_as_jinja2(self, obj):
"""
Returns the filtering term as a jinja2 template, we need it during migration from regex to jinja2"""
if obj.filtering_term_type == ChannelFilter.FILTERING_TERM_TYPE_JINJA2:
return obj.filtering_term
elif obj.filtering_term_type == ChannelFilter.FILTERING_TERM_TYPE_REGEX:
# Four curly braces will result in two curly braces in the final string
return f'{{{{ payload | json_dumps | regex_search("{obj.filtering_term}") }}}}'
return obj.filtering_term


class ChannelFilterCreateSerializer(ChannelFilterSerializer):
alert_receive_channel = OrganizationFilteredPrimaryKeyRelatedField(queryset=AlertReceiveChannel.objects)
Expand Down
13 changes: 13 additions & 0 deletions engine/apps/api/views/channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ChannelFilterView(
"destroy": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
"move_to_position": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
"send_demo_alert": [RBACPermission.Permissions.INTEGRATIONS_TEST],
"convert_from_regex_to_jinja2": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
}

model = ChannelFilter
Expand Down Expand Up @@ -143,3 +144,15 @@ def send_demo_alert(self, request, pk):
except UnableToSendDemoAlert as e:
raise BadRequest(detail=str(e))
return Response(status=status.HTTP_200_OK)

@action(detail=True, methods=["post"])
def convert_from_regex_to_jinja2(self, request, pk):
# instance = ChannelFilter.objects.get(public_primary_key=pk)
instance = self.get_queryset().get(public_primary_key=pk)
if not instance.filtering_term_type == ChannelFilter.FILTERING_TERM_TYPE_REGEX:
raise BadRequest(detail="Only regex filtering term type is supported")
data = self.serializer_class(instance).data
instance.filtering_term = data["filtering_term_as_jinja2"]
instance.filtering_term_type = ChannelFilter.FILTERING_TERM_TYPE_JINJA2
instance.save()
return Response(status=status.HTTP_200_OK, data=self.serializer_class(instance).data)

0 comments on commit f9473f2

Please sign in to comment.