Skip to content
Snippets Groups Projects
Commit d8fcd896 authored by Jonas Braathen's avatar Jonas Braathen
Browse files

Allow configuring a routing key prefix

parent 385aab45
No related branches found
No related tags found
1 merge request!278Allow configuring a routing key prefix
Pipeline #114923 passed
...@@ -21,14 +21,23 @@ def get_notifications(limit): ...@@ -21,14 +21,23 @@ def get_notifications(limit):
def generate_event_type(n: Notification) -> str: def generate_event_type(n: Notification) -> str:
""" """
Generate CloudEvent compliant type field. Generate CloudEvent compliant type field.
This can be used as routing key as well so that a future change to amqp where
routing key is removed results in minor changes to the code.
""" """
object_type = camel_to_snake(n.object_type) object_type = camel_to_snake(n.object_type)
return f"{object_type}.{n.operation}" return f"{object_type}.{n.operation}"
def generate_routing_key(n: Notification) -> str:
"""
Generate an AMQP routing key.
By default this equals the payload type field, but is prefixed with the
value in settings.NOTIFICATION_ROUTING_KEY_PREFIX
"""
event_type = generate_event_type(n)
prefix = settings.NOTIFICATION_ROUTING_KEY_PREFIX
return f"{prefix}{event_type}"
def create_cloud_event_payload(n: Notification) -> str: def create_cloud_event_payload(n: Notification) -> str:
""" """
Produce a notification payload from a Notification object that matches CloudEvents Produce a notification payload from a Notification object that matches CloudEvents
...@@ -66,9 +75,7 @@ def handle_one_notification(notification: Notification, pcm: PCM, exchange: str) ...@@ -66,9 +75,7 @@ def handle_one_notification(notification: Notification, pcm: PCM, exchange: str)
and delete the Notification afterwards. and delete the Notification afterwards.
""" """
payload = create_cloud_event_payload(notification) payload = create_cloud_event_payload(notification)
# Use event_type for routing_key so that a future switch to amqp 1.0 that does not routing_key = generate_routing_key(notification)
# have routing keys is easier
routing_key = generate_event_type(notification)
if pcm.publish( if pcm.publish(
exchange, exchange,
......
...@@ -4,6 +4,7 @@ import pytest ...@@ -4,6 +4,7 @@ import pytest
from greg.management.commands.notification_publisher import ( from greg.management.commands.notification_publisher import (
handle_one_notification, handle_one_notification,
create_cloud_event_payload, create_cloud_event_payload,
generate_routing_key,
) )
from greg.models import Notification from greg.models import Notification
...@@ -41,3 +42,10 @@ def test_create_cloud_event_payload(role_type_notification): ...@@ -41,3 +42,10 @@ def test_create_cloud_event_payload(role_type_notification):
"type": "role_type.update", "type": "role_type.update",
"data": {"integer": 123, "string": "foo"}, "data": {"integer": 123, "string": "foo"},
} }
@pytest.mark.django_db
def test_generate_routing_key(role_type_notification, settings):
assert generate_routing_key(role_type_notification) == "role_type.update"
settings.NOTIFICATION_ROUTING_KEY_PREFIX = "foo."
assert generate_routing_key(role_type_notification) == "foo.role_type.update"
...@@ -266,6 +266,9 @@ structlog.configure( ...@@ -266,6 +266,9 @@ structlog.configure(
) )
# By default, the routing key format is "object_type.operation"
# Setting the prefix to "foo." will result in "foo.object_type.operation"
NOTIFICATION_ROUTING_KEY_PREFIX = ""
NOTIFICATION_PUBLISHER = { NOTIFICATION_PUBLISHER = {
"mq": { "mq": {
"connection": { "connection": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment