"""Utils for Amazon Simple Queue Service operations.""" import json import boto3 from collaborator import config from collaborator.constants import header REQUIRED_PARAMS = { config.SQS_BULK_REPORT_TRIGGER_QUEUE_NAME: ["report_run_uuid"], } def _get_client(): """Get an AWS SQS client. Used to inject a mock client when testing. Returns: boto3.Service client """ return boto3.resource("sqs") def _verify_params(params: dict, required_params: list): """Verify that the parameters dict contains all required parameters. Args: params (dict): Parameter dict to verify. """ for param in required_params: if param not in params: raise ValueError(f"Missing parameter: {param}") def send_message(queue_name: str, correlation_id: str, **params): """Send a message to SQS. Example call: sqs.send_message( '868ae-83aa-8273-2874-1982', account_id='18805', account_type='label', period_ids='205', collaborator_id='123) Args: correlation_id (str): correlation_id in the header. params (dict): report specific params. Returns: Response: object containing sqs response """ _verify_params(params, REQUIRED_PARAMS[queue_name]) params[header.CORRELATION_ID] = "{}.1".format(correlation_id) client = _get_client() queue = client.get_queue_by_name(QueueName=queue_name) response = queue.send_message(MessageBody=json.dumps(params, sort_keys=True)) return response def send_bulk_report_trigger_message(correlation_id: str, **params): """Send a message to the bulk report trigger queue. Args: correlation_id (str): correlation_id in the header. params (dict): report specific params. Returns: Response: object containing sqs response """ return send_message( config.SQS_BULK_REPORT_TRIGGER_QUEUE_NAME, correlation_id, **params )