Skip to content

Commit 6933833

Browse files
committed
use factory pattern for publishing notification
1 parent fc31344 commit 6933833

6 files changed

Lines changed: 56 additions & 112 deletions

File tree

infrastructure/instance/modules/mns_publisher/sqs_test_publish_mns.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
resource "aws_sqs_queue" "mns_test_notification" {
55
name = "${var.mns_test_notification_name_prefix}-queue"
66
fifo_queue = false
7-
message_retention_seconds = 14400
7+
message_retention_seconds = 86400
88
visibility_timeout_seconds = 300
99
}
1010

lambdas/mns_publisher/src/mns_test_queue.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

lambdas/mns_publisher/src/process_records.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
from common.api_clients.mns_service import MnsService
88
from common.api_clients.mns_setup import get_mns_service
9+
from common.api_clients.mock_mns_service import MockMnsService
910
from common.clients import logger
1011
from create_notification import create_mns_notification
11-
from mns_test_queue import send_notification_to_test_queue
1212

1313
mns_env = os.getenv("MNS_ENV", "int")
1414
MNS_TEST_QUEUE_URL = os.getenv("MNS_TEST_QUEUE_URL")
@@ -39,7 +39,7 @@ def process_records(records: list[SQSMessage]) -> dict[str, list]:
3939
return {"batchItemFailures": batch_item_failures}
4040

4141

42-
def process_record(record: SQSMessage, mns_service: MnsService) -> None:
42+
def process_record(record: SQSMessage, mns_service: MnsService | MockMnsService) -> None:
4343
"""
4444
Process a single SQS record.
4545
Args:
@@ -64,14 +64,8 @@ def process_record(record: SQSMessage, mns_service: MnsService) -> None:
6464
},
6565
)
6666

67-
# TODO: Remove when MNS platform authorizes imms-vaccinations-1 event type
68-
# Temporary SQS queue for testing MNS notifications until MNS HTTP endpoint is available
69-
if MNS_TEST_QUEUE_URL:
70-
send_notification_to_test_queue(mns_notification_payload)
71-
logger.info("Notification Successfully sent to SQS", extra={"notification_id": notification_id})
72-
else:
73-
mns_service.publish_notification(mns_notification_payload)
74-
logger.info("Successfully created MNS notification", extra={"mns_notification_id": notification_id})
67+
mns_service.publish_notification(mns_notification_payload)
68+
logger.info("Successfully created MNS notification", extra={"mns_notification_id": notification_id})
7569

7670

7771
def extract_trace_ids(record: SQSMessage) -> Tuple[str, str | None]:

lambdas/mns_publisher/tests/test_mns_test_queue.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

lambdas/shared/src/common/api_clients/mns_setup.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55

66
from common.api_clients.authentication import AppRestrictedAuth, Service
77
from common.api_clients.mns_service import MnsService
8+
from common.api_clients.mock_mns_service import MockMnsService
89
from common.cache import Cache
910

1011
logging.basicConfig(level=logging.INFO)
1112

1213

1314
def get_mns_service(mns_env: str = "int"):
14-
boto_config = Config(region_name="eu-west-2")
15-
cache = Cache(directory="/tmp") # NOSONAR(S5443)
16-
logging.info("Creating authenticator...")
17-
authenticator = AppRestrictedAuth(
18-
service=Service.PDS,
19-
secret_manager_client=boto3.client("secretsmanager", config=boto_config),
20-
environment=mns_env,
21-
cache=cache,
22-
)
15+
if mns_env == "dev":
16+
return MockMnsService()
17+
else:
18+
boto_config = Config(region_name="eu-west-2")
19+
cache = Cache(directory="/tmp") # NOSONAR(S5443)
20+
logging.info("Creating authenticator...")
21+
authenticator = AppRestrictedAuth(
22+
service=Service.PDS,
23+
secret_manager_client=boto3.client("secretsmanager", config=boto_config),
24+
environment=mns_env,
25+
cache=cache,
26+
)
2327

24-
logging.info("Authentication Initiated...")
25-
return MnsService(authenticator)
28+
logging.info("Authentication Initiated...")
29+
return MnsService(authenticator)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import os
3+
4+
import boto3
5+
6+
from common.clients import logger
7+
8+
MNS_TEST_QUEUE_URL = os.getenv("MNS_TEST_QUEUE_URL")
9+
10+
11+
class MockMnsService:
12+
def __init__(self, queue_url):
13+
self.queue_url = queue_url
14+
self.sqs_client = self._get_sqs_client()
15+
logger.info(f"MockMnsService initialized with queue: {queue_url}")
16+
17+
def _get_sqs_client(self):
18+
return boto3.client("sqs", region_name="eu-west-2")
19+
20+
def publish_notification(self, mns_payload: dict) -> None:
21+
"""
22+
Send MNS notification payload to test SQS queue as fallback.
23+
Args: payload: MNS notification payload
24+
"""
25+
try:
26+
response = self._get_sqs_client.send_message(
27+
QueueUrl=MNS_TEST_QUEUE_URL,
28+
MessageBody=json.dumps(mns_payload),
29+
MessageAttributes={"source": {"StringValue": "mns-publisher-lambda", "DataType": "String"}},
30+
)
31+
logger.info(
32+
"Mock MNS: Successfully sent notification to test queue", extra={"message_id": response["MessageId"]}
33+
)
34+
except Exception:
35+
logger.exception("Mock MNS: Failed to send to test SQS queue")
36+
raise

0 commit comments

Comments
 (0)