"""Connector for SNS service.""" from botocore import exceptions from accounting.flows.reserve_payouts import connectors from accounting.flows.reserve_payouts import setting from accounting.flows.reserve_payouts.constants import sns as sns_constants from accounting.util import sns as sns_util def health_check(): """Perform a simple query to do the health check. Returns: namedtuple: with bool and message attributes, (True, '') if connection is ok, (False, 'Error message') otherwise. """ success_topic = '' fail_topic = '' try: sns = sns_util.get_sns_resource() result = sns.topics.all() for item in result: if item.arn == setting.SNS_SUCCESS_TOPIC: success_topic = True if item.arn == setting.SNS_FAIL_TOPIC: fail_topic = True if not success_topic or not fail_topic: raise Exception("SNS Topics don't exist") except (exceptions.BotoCoreError, exceptions.ClientError, Exception) as e: result = connectors.HealthCheckResult(False, str(e)) else: result = connectors.HealthCheckResult(True, '') return result def send_status_message(status, status_message): """Send a status message to SNS. Args: status (str): a status that defines what SNS topic will be used status_message(str): a message that contains details of the failed or succeeded job Returns: bool: check if HTTPStatusCode = 200 """ if status == 'success': status = sns_util.publish_sns_message_to_email( sns_constants.SNS_SUCCESS_MESSAGE.format(status_message), setting.SNS_SUCCESS_TOPIC) else: status = sns_util.publish_sns_message_to_email( sns_constants.SNS_FAIL_MESSAGE.format(status_message), setting.SNS_FAIL_TOPIC) return status['ResponseMetadata']['HTTPStatusCode'] == 200