Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Nick Satterly
15ff1f2552 feature(plugin): add support for Fifo topics to SNS plugin 2021-01-28 23:59:23 +01:00
3 changed files with 49 additions and 24 deletions

View file

@ -1,7 +1,7 @@
AWS SNS Plugin
==============
Send alerts to AWS SNS topic.
Send alerts to AWS SNS topic, with support for Fifo.
For help, join [![Slack chat](https://img.shields.io/badge/chat-on%20slack-blue?logo=slack)](https://slack.alerta.dev)
@ -29,8 +29,6 @@ server configuration file or as environment variables.
```python
PLUGINS = ['sns']
AWS_ACCESS_KEY_ID = '' # default="not set"
AWS_SECRET_ACCESS_KEY = '' # default="not set"
```
**Default Configuration**
@ -53,4 +51,4 @@ References
License
-------
Copyright (c) 2016 Nick Satterly. Available under the MIT License.
Copyright (c) 2016,2021 Nick Satterly. Available under the MIT License.

View file

@ -1,6 +1,5 @@
import boto.exception
import boto.sns
import boto3
import logging
import os
@ -16,8 +15,6 @@ DEFAULT_AWS_REGION = 'eu-west-1'
DEFAULT_AWS_SNS_TOPIC = 'notify'
AWS_REGION = os.environ.get('AWS_REGION') or app.config.get('AWS_REGION', DEFAULT_AWS_REGION)
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID') or app.config.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY') or app.config.get('AWS_SECRET_ACCESS_KEY')
AWS_SNS_TOPIC = os.environ.get('AWS_SNS_TOPIC') or app.config.get('AWS_SNS_TOPIC', DEFAULT_AWS_SNS_TOPIC)
@ -25,22 +22,23 @@ class SnsTopicPublisher(PluginBase):
def __init__(self, name=None):
try:
self.connection = boto.sns.connect_to_region(
region_name=AWS_REGION,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
)
self.client = boto3.client('sns')
# region_name=AWS_REGION,
# )
except Exception as e:
LOG.error('Error connecting to SNS topic %s: %s', AWS_SNS_TOPIC, e)
raise RuntimeError
if not self.connection:
if not self.client:
LOG.error('Failed to connect to SNS topic %s - check AWS credentials and region', AWS_SNS_TOPIC)
raise RuntimeError
try:
response = self.connection.create_topic(AWS_SNS_TOPIC)
except boto.exception.BotoServerError as e:
response = self.client.create_topic(
Name=AWS_SNS_TOPIC,
FifoTopic=True if AWS_SNS_TOPIC.endswith('fifo') else False
)
except self.client.exception.BotoServerError as e:
LOG.error('Error creating SNS topic %s: %s', AWS_SNS_TOPIC, e)
raise RuntimeError
@ -54,16 +52,45 @@ class SnsTopicPublisher(PluginBase):
LOG.info('Configured SNS publisher on topic "%s"', self.topic_arn)
def pre_receive(self, alert):
def pre_receive(self, alert, **kwargs):
return alert
def post_receive(self, alert):
def post_receive(self, alert, **kwargs):
LOG.info('Sending message %s to SNS topic "%s"', alert.get_id(), self.topic_arn)
LOG.debug('Message: %s', alert.get_body())
response = self.connection.publish(topic=self.topic_arn, message=alert.get_body())
response = self.client.publish(
TopicArn=self.topic_arn,
Subject='Alert {} received'.format(alert.last_receive_id),
MessageStructure='json',
Message=alert.get_body(),
MessageAttributes={
'resource': {
'DataType': 'string',
'StringValue': alert.resource
},
'event': {
'DataType': 'string',
'StringValue': alert.event
},
'environment': {
'DataType': 'string',
'StringValue': alert.environment
},
'severity': {
'DataType': 'string',
'StringValue': alert.severity
},
'status': {
'DataType': 'string',
'StringValue': alert.status
},
},
MessageDeduplicationId=alert.last_receive_id,
MessageGroupId=alert.id
)
LOG.debug('Response: %s', response)
def status_change(self, alert, status, text):
def status_change(self, alert, status, text, **kwargs):
return

View file

@ -1,20 +1,20 @@
from setuptools import setup, find_packages
version = '5.3.1'
version = '6.0.0'
setup(
name="alerta-sns",
version=version,
description='Alerta plugin for AWS SNS',
description='Alerta plugin for AWS SNS (with Fifo)',
url='https://github.com/alerta/alerta-contrib',
license='MIT',
author='Nick Satterly',
author_email='nick.satterly@theguardian.com',
author_email='nick.satterly@gmail.com',
packages=find_packages(),
py_modules=['alerta_sns'],
install_requires=[
'boto'
'boto3'
],
include_package_data=True,
zip_safe=True,