forked from alerta/alerta-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerta_pubsub.py
58 lines (44 loc) · 1.9 KB
/
alerta_pubsub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import logging
import base64
import json
try:
from alerta.plugins import app # alerta >= 5.0
except ImportError:
from alerta.app import app # alerta < 5.0
from alerta.plugins import PluginBase
from google.cloud import pubsub
from google.oauth2 import service_account
LOG = logging.getLogger('alerta.plugins.pubsub')
PROJECT_ID = os.environ.get('PROJECT_ID') or app.config.get('PROJECT_ID')
TOPIC_NAME = os.environ.get('TOPIC_NAME') or app.config.get('TOPIC_NAME')
SERVICE_ACCOUNT_JSON = os.environ.get('SERVICE_ACCOUNT_JSON') or app.config.get('SERVICE_ACCOUNT_JSON', None)
PUBSUB_SCOPES = ["https://www.googleapis.com/auth/pubsub"]
class SendToPubsub(PluginBase):
def __init__(self, name=None):
LOG.info('creating pubsub client')
self.publisher = self.get_client()
self.topic = 'projects/{}/topics/{}'.format(PROJECT_ID, TOPIC_NAME)
super(SendToPubsub, self).__init__(name)
def get_client(self):
if SERVICE_ACCOUNT_JSON:
json_dict = json.loads(SERVICE_ACCOUNT_JSON)
LOG.info('using service account JSON : %s', json_dict)
credential = service_account.Credentials.from_service_account_info(json_dict)
scoped_credential = credential.with_scopes(PUBSUB_SCOPES)
return pubsub.PublisherClient(credentials=scoped_credential)
LOG.info('default')
return pubsub.PublisherClient()
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
body = alert.get_body()
try:
encoded_message = base64.b64encode(json.dumps(body))
future = self.publisher.publish(self.topic, str(encoded_message))
future.result()
except Exception as excp:
LOG.exception(excp)
raise RuntimeError("pubsub exception: %s - %s" % (excp, body))
def status_change(self, alert, status, text):
return