Package Products :: Package Zuul :: Package routers :: Module triggers
[hide private]
[frames] | no frames]

Source Code for Module Products.Zuul.routers.triggers

  1  ############################################################################## 
  2  # 
  3  # Copyright (C) Zenoss, Inc. 2010, all rights reserved. 
  4  # 
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  7  # 
  8  ############################################################################## 
  9   
 10   
 11  from operator import itemgetter 
 12  from Products import Zuul 
 13  from zope.component import getUtilitiesFor 
 14  from Products.ZenModel.interfaces import IAction 
 15  from Products.ZenUtils.Ext import DirectRouter 
 16  from Products.ZenUtils.extdirect.router import DirectResponse 
 17  from Products.Zuul.decorators import serviceConnectionError 
 18  from zenoss.protocols.protobufs.zep_pb2 import RULE_TYPE_JYTHON 
 19  from Products.ZenMessaging.audit import audit 
 20  from Products.ZenModel.Trigger import DuplicateTriggerName 
 21   
 22  import logging 
 23   
 24  log = logging.getLogger('zen.triggers') 
25 26 27 -class TriggersRouter(DirectRouter):
28 """ 29 Router for Triggers UI section under Events. 30 """ 31
32 - def _getFacade(self):
33 return Zuul.getFacade('triggers', self)
34 35 @serviceConnectionError
36 - def getTriggers(self, **kwargs):
37 return DirectResponse.succeed(data=self._getFacade().getTriggers())
38 39 @serviceConnectionError
40 - def getTriggerList(self, **unused):
41 return DirectResponse.succeed(data=self._getFacade().getTriggerList())
42 43 @serviceConnectionError
44 - def addTrigger(self, newId):
45 try: 46 data = self._getFacade().addTrigger(newId) 47 except DuplicateTriggerName as tnc: 48 log.debug("Exception DuplicateTriggerName: %s" % tnc) 49 return DirectResponse.fail(str(tnc)) 50 else: 51 audit('UI.Trigger.Add', newId) 52 return DirectResponse.succeed(data=data)
53 54 @serviceConnectionError
55 - def removeTrigger(self, uuid):
56 trigger = self._getFacade().getTrigger(uuid) 57 updated_count = self._getFacade().removeTrigger(uuid) 58 audit('UI.Trigger.Remove', trigger['name']) 59 msg = "Trigger removed successfully. {count} {noun} {verb} updated.".format( 60 count=updated_count, 61 noun='notification' if updated_count == 1 else 'notifications', 62 verb='was' if updated_count == 1 else 'were' 63 ) 64 return DirectResponse.succeed(msg=msg, data=None)
65 66 @serviceConnectionError
67 - def getTrigger(self, uuid):
68 return DirectResponse.succeed(data=self._getFacade().getTrigger(uuid))
69 70 @serviceConnectionError
71 - def updateTrigger(self, **data):
72 data['rule']['api_version'] = 1 73 data['rule']['type'] = RULE_TYPE_JYTHON 74 triggerUid = data['uuid'] 75 response = self._getFacade().updateTrigger(**data) 76 audit('UI.Trigger.Edit', triggerUid, data_=data) 77 return DirectResponse.succeed( 78 msg="Trigger updated successfully.", data=response 79 )
80 81 @serviceConnectionError
82 - def parseFilter(self, source):
83 try: 84 response = self._getFacade().parseFilter(source) 85 return DirectResponse.succeed(data=response) 86 except Exception as err: 87 log.exception() 88 return DirectResponse.exception( 89 err, 90 'Error parsing filter source. Please check your syntax.' 91 )
92 93 # notification subscriptions 94 @serviceConnectionError
95 - def getNotifications(self):
96 response = self._getFacade().getNotificationInfos() 97 return DirectResponse.succeed(data=Zuul.marshal(response))
98 99 @serviceConnectionError
100 - def addNotification(self, newId, action):
101 response = self._getFacade().addNotification(newId, action) 102 audit('UI.Notification.Add', newId) 103 return DirectResponse.succeed(data=Zuul.marshal(response))
104 105 @serviceConnectionError
106 - def removeNotification(self, uid):
107 response = self._getFacade().removeNotification(uid) 108 audit('UI.Notification.Remove', uid) 109 return DirectResponse.succeed( 110 msg="Notification removed successfully.", data=response 111 )
112 113 @serviceConnectionError
114 - def getNotificationTypes(self, query=''):
115 utils = getUtilitiesFor(IAction) 116 actionTypes = sorted( 117 (dict(id=id, name=util.name) for id, util in utils), 118 key=itemgetter('id') 119 ) 120 log.debug('notification action types are: %s' % actionTypes) 121 return DirectResponse.succeed(data=actionTypes)
122 123 @serviceConnectionError
124 - def getNotification(self, uid):
125 response = self._getFacade().getNotification(uid) 126 return DirectResponse.succeed(data=Zuul.marshal(response))
127 128 @serviceConnectionError
129 - def updateNotification(self, **data):
130 notificationUid = data['uid'] 131 response = self._getFacade().updateNotification(**data) 132 audit('UI.Notification.Edit', notificationUid, 133 data=data, maskFields='password') 134 return DirectResponse.succeed( 135 msg="Notification updated successfully.", 136 data=Zuul.marshal(response) 137 )
138 139 @serviceConnectionError
140 - def getRecipientOptions(self, **kwargs):
141 data = self._getFacade().getRecipientOptions() 142 return DirectResponse.succeed(data=data)
143 144 # subscription windows 145 @serviceConnectionError
146 - def getWindows(self, uid, **kwargs):
147 response = self._getFacade().getWindows(uid) 148 return DirectResponse.succeed(data=Zuul.marshal(response))
149 150 @serviceConnectionError
151 - def addWindow(self, contextUid, newId):
152 response = self._getFacade().addWindow(contextUid, newId) 153 audit('UI.NotificationWindow.Add', newId, notification=contextUid) 154 return DirectResponse.succeed(data=Zuul.marshal(response))
155 156 @serviceConnectionError
157 - def removeWindow(self, uid):
158 response = self._getFacade().removeWindow(uid) 159 audit('UI.NotificationWindow.Remove', uid) 160 return DirectResponse.succeed(data=Zuul.marshal(response))
161 162 @serviceConnectionError
163 - def getWindow(self, uid):
164 response = self._getFacade().getWindow(uid) 165 return DirectResponse.succeed(data=Zuul.marshal(response))
166 167 @serviceConnectionError
168 - def updateWindow(self, **data):
169 windowUid = data['uid'] 170 response = self._getFacade().updateWindow(data) 171 audit('UI.NotificationWindow.Edit', windowUid, data_=data) 172 return DirectResponse.succeed(data=Zuul.marshal(response))
173 174 @serviceConnectionError
175 - def exportConfiguration(self, triggerIds=None, notificationIds=None):
176 facade = self._getFacade() 177 triggers, notifications = facade.exportConfiguration( 178 triggerIds, notificationIds 179 ) 180 msg = "Exported %d triggers and %d notifications" % ( 181 len(triggers), len(notifications) 182 ) 183 audit('UI.TriggerNotification.Export', msg) 184 return DirectResponse.succeed( 185 triggers=Zuul.marshal(triggers), 186 notifications=Zuul.marshal(notifications), 187 msg=msg 188 )
189 190 @serviceConnectionError
191 - def importConfiguration(self, triggers=None, notifications=None):
192 try: 193 tcount = len(triggers) if triggers is not None else 0 194 ncount = len(notifications) if notifications is not None else 0 195 facade = self._getFacade() 196 itcount, incount = facade.importConfiguration( 197 triggers, notifications 198 ) 199 msg = "Imported %d of %d triggers and %d of %d notifications" % ( 200 tcount, itcount, ncount, incount 201 ) 202 audit('UI.TriggerNotification.Import', msg) 203 return DirectResponse.succeed(msg=msg) 204 except Exception as ex: 205 audit('UI.TriggerNotification.Import', 206 "Failed to import trigger/notification data") 207 log.exception( 208 "Unable to import data:\ntriggers=%s\nnotifications=%s", 209 repr(triggers), repr(notifications) 210 ) 211 return DirectResponse.fail(str(ex))
212