-
Notifications
You must be signed in to change notification settings - Fork 4
VED:1099: Create Test SQS Queue for MNS #1268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
10730f9
d236e98
8b54902
315ca08
e6a22c9
dcbde02
fc31344
6933833
55c18d3
e0c2134
337e745
0657096
8f8fc87
0520b95
163082a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| resource "aws_sqs_queue" "mns_test_notification" { | ||
|
dlzhry2nhs marked this conversation as resolved.
|
||
| count = var.mns_environment == "dev" ? 1 : 0 | ||
| name = "${var.mns_test_notification_name_prefix}-queue" | ||
| fifo_queue = false | ||
| message_retention_seconds = 86400 | ||
| visibility_timeout_seconds = 300 | ||
| } | ||
|
|
||
|
|
||
| data "aws_iam_policy_document" "mns_test_notification_sqs_policy" { | ||
| count = var.mns_environment == "dev" ? 1 : 0 | ||
| statement { | ||
| sid = "mns-test-notification-allow-lambda-access" | ||
| effect = "Allow" | ||
|
|
||
| principals { | ||
| type = "AWS" | ||
| identifiers = [aws_iam_role.mns_publisher_lambda_exec_role.arn] | ||
|
dlzhry2nhs marked this conversation as resolved.
|
||
| } | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage", | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_sqs_queue.mns_test_notification[0].arn | ||
|
dlzhry2nhs marked this conversation as resolved.
|
||
| ] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_sqs_queue_policy" "mns_test_notification_sqs" { | ||
| count = var.mns_environment == "dev" ? 1 : 0 | ||
| queue_url = aws_sqs_queue.mns_test_notification[0].id | ||
| policy = data.aws_iam_policy_document.mns_test_notification_sqs_policy[0].json | ||
| } | ||
|
|
||
| output "mns_test_queue_url" { | ||
| value = var.mns_environment == "dev" ? aws_sqs_queue.mns_test_notification[0].url : null | ||
| description = "URL of the MNS test notifications queue" | ||
| } | ||
|
|
||
| output "mns_test_queue_arn" { | ||
| value = var.mns_environment == "dev" ? aws_sqs_queue.mns_test_notification[0].arn : 0 | ||
| description = "ARN of the MNS test notifications queue" | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,12 @@ | |
|
|
||
| from common.api_clients.mns_service import MnsService | ||
| from common.api_clients.mns_setup import get_mns_service | ||
| from common.api_clients.mock_mns_service import MockMnsService | ||
| from common.clients import logger | ||
| from create_notification import create_mns_notification | ||
|
|
||
| mns_env = os.getenv("MNS_ENV", "int") | ||
| MNS_TEST_QUEUE_URL = os.getenv("MNS_TEST_QUEUE_URL") | ||
|
|
||
|
|
||
| def process_records(records: list[SQSMessage]) -> dict[str, list]: | ||
|
|
@@ -37,7 +39,7 @@ def process_records(records: list[SQSMessage]) -> dict[str, list]: | |
| return {"batchItemFailures": batch_item_failures} | ||
|
|
||
|
|
||
| def process_record(record: SQSMessage, mns_service: MnsService) -> None: | ||
| def process_record(record: SQSMessage, mns_service: MnsService | MockMnsService) -> None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially even better if we implement an abstract class, or vanilla Python base class that defines the interface that includes a publish_notification(self, notification: MnsPayloadBla) -> None in the contract. Optional though, this is good enough for now...maybe. |
||
| """ | ||
| Process a single SQS record. | ||
| Args: | ||
|
|
@@ -65,8 +67,6 @@ def process_record(record: SQSMessage, mns_service: MnsService) -> None: | |
| mns_service.publish_notification(mns_notification_payload) | ||
| logger.info("Successfully created MNS notification", extra={"mns_notification_id": notification_id}) | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def extract_trace_ids(record: SQSMessage) -> Tuple[str, str | None]: | ||
| """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,31 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| import boto3 | ||
| from botocore.config import Config | ||
|
|
||
| from common.api_clients.authentication import AppRestrictedAuth, Service | ||
| from common.api_clients.mns_service import MnsService | ||
| from common.api_clients.mock_mns_service import MockMnsService | ||
| from common.cache import Cache | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
| MNS_TEST_QUEUE_URL = os.getenv("MNS_TEST_QUEUE_URL") | ||
|
|
||
|
|
||
| def get_mns_service(mns_env: str = "int"): | ||
| boto_config = Config(region_name="eu-west-2") | ||
| cache = Cache(directory="/tmp") # NOSONAR(S5443) | ||
| logging.info("Creating authenticator...") | ||
| authenticator = AppRestrictedAuth( | ||
| service=Service.PDS, | ||
| secret_manager_client=boto3.client("secretsmanager", config=boto_config), | ||
| environment=mns_env, | ||
| cache=cache, | ||
| ) | ||
|
|
||
| logging.info("Authentication Initiated...") | ||
| return MnsService(authenticator) | ||
| if mns_env == "dev": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For flexibility, might it be better to have pass in a flag for |
||
| logging.info("Dev environment: Using MockMnsService") | ||
| return MockMnsService(MNS_TEST_QUEUE_URL) | ||
| else: | ||
| boto_config = Config(region_name="eu-west-2") | ||
| cache = Cache(directory="/tmp") # NOSONAR(S5443) | ||
| logging.info("Creating authenticator...") | ||
| authenticator = AppRestrictedAuth( | ||
| service=Service.PDS, | ||
| secret_manager_client=boto3.client("secretsmanager", config=boto_config), | ||
| environment=mns_env, | ||
| cache=cache, | ||
| ) | ||
| logging.info("Authentication Initiated...") | ||
| return MnsService(authenticator) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import json | ||
| import os | ||
|
|
||
| import boto3 | ||
|
|
||
| from common.clients import logger | ||
|
|
||
| REGION_NAME = os.getenv("AWS_REGION", "eu-west-2") | ||
|
|
||
|
|
||
| class MockMnsService: | ||
| def __init__(self, queue_url): | ||
| self.queue_url = queue_url | ||
| self.sqs_client = self._get_sqs_client() | ||
| logger.info(f"MockMnsService initialized with queue: {queue_url}") | ||
|
|
||
| def _get_sqs_client(self): | ||
|
dlzhry2nhs marked this conversation as resolved.
Outdated
|
||
| return boto3.client("sqs", region_name=REGION_NAME) | ||
|
|
||
| def publish_notification(self, mns_payload: dict) -> None: | ||
|
dlzhry2nhs marked this conversation as resolved.
Outdated
|
||
| """ | ||
| Send MNS notification payload to test SQS queue as fallback. | ||
| Args: payload: MNS notification payload | ||
| """ | ||
| try: | ||
| response = self.sqs_client.send_message( | ||
| QueueUrl=self.queue_url, | ||
| MessageBody=json.dumps(mns_payload), | ||
| MessageAttributes={"source": {"StringValue": "mns-publisher-lambda", "DataType": "String"}}, | ||
| ) | ||
| logger.info( | ||
| "Mock MNS: Successfully sent notification to test queue", extra={"message_id": response["MessageId"]} | ||
| ) | ||
| except Exception: | ||
| logger.exception("Mock MNS: Failed to send to test SQS queue") | ||
| raise | ||
Uh oh!
There was an error while loading. Please reload this page.