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 -class TriggersRouter(DirectRouter):
27 """ 28 Router for Triggers UI section under Events. 29 """ 30
31 - def _getFacade(self):
32 return Zuul.getFacade('triggers', self)
33 34 @serviceConnectionError
35 - def getTriggers(self, **kwargs):
36 return DirectResponse.succeed(data=self._getFacade().getTriggers())
37 38 @serviceConnectionError
39 - def getTriggerList(self, **unused):
40 return DirectResponse.succeed(data=self._getFacade().getTriggerList())
41 42 @serviceConnectionError
43 - def addTrigger(self, newId):
44 try: 45 data = self._getFacade().addTrigger(newId) 46 except DuplicateTriggerName, tnc: 47 log.debug("Exception DuplicateTriggerName: %s" % tnc) 48 return DirectResponse.fail(str(tnc)) 49 else: 50 audit('UI.Trigger.Add', newId) 51 return DirectResponse.succeed(data=data)
52 53 @serviceConnectionError
54 - def removeTrigger(self, uuid):
55 updated_count = self._getFacade().removeTrigger(uuid) 56 audit('UI.Trigger.Remove', uuid) 57 msg = "Trigger removed successfully. {count} {noun} {verb} updated.".format( 58 count = updated_count, 59 noun = 'notification' if updated_count == 1 else 'notifications', 60 verb = 'was' if updated_count == 1 else 'were' 61 ) 62 return DirectResponse.succeed(msg=msg, data=None)
63 64 @serviceConnectionError
65 - def getTrigger(self, uuid):
66 return DirectResponse.succeed(data=self._getFacade().getTrigger(uuid))
67 68 @serviceConnectionError
69 - def updateTrigger(self, **data):
70 data['rule']['api_version'] = 1 71 data['rule']['type'] = RULE_TYPE_JYTHON 72 triggerUid = data['uuid'] 73 response = self._getFacade().updateTrigger(**data) 74 audit('UI.Trigger.Edit', triggerUid, data_=data) 75 return DirectResponse.succeed(msg="Trigger updated successfully.", data=response)
76 77 @serviceConnectionError
78 - def parseFilter(self, source):
79 try: 80 response = self._getFacade().parseFilter(source) 81 return DirectResponse.succeed(data=response) 82 except Exception, e: 83 log.exception(e) 84 return DirectResponse.exception(e, 85 'Error parsing filter source. Please check your syntax.')
86 87 88 # notification subscriptions 89 @serviceConnectionError
90 - def getNotifications(self):
91 response = self._getFacade().getNotificationInfos() 92 return DirectResponse.succeed(data=Zuul.marshal(response))
93 94 @serviceConnectionError
95 - def addNotification(self, newId, action):
96 response = self._getFacade().addNotification(newId, action) 97 audit('UI.Notification.Add', newId) 98 return DirectResponse.succeed(data=Zuul.marshal(response))
99 100 @serviceConnectionError
101 - def removeNotification(self, uid):
102 response = self._getFacade().removeNotification(uid) 103 audit('UI.Notification.Remove', uid) 104 return DirectResponse.succeed(msg="Notification removed successfully.", data=response)
105 106 @serviceConnectionError
107 - def getNotificationTypes(self, query=''):
108 utils = getUtilitiesFor(IAction) 109 actionTypes = sorted((dict(id=id, name=util.name) for id, util in utils), key=itemgetter('id')) 110 log.debug('notification action types are: %s' % actionTypes) 111 return DirectResponse.succeed(data=actionTypes)
112 113 @serviceConnectionError
114 - def getNotification(self, uid):
115 response = self._getFacade().getNotification(uid) 116 return DirectResponse.succeed(data=Zuul.marshal(response))
117 118 @serviceConnectionError
119 - def updateNotification(self, **data):
120 notificationUid = data['uid'] 121 response = self._getFacade().updateNotification(**data) 122 audit('UI.Notification.Edit', notificationUid, data_=data, maskFields_='password') 123 return DirectResponse.succeed(msg="Notification updated successfully.", data=Zuul.marshal(response))
124 125 @serviceConnectionError
126 - def getRecipientOptions(self, **kwargs):
127 data = self._getFacade().getRecipientOptions() 128 return DirectResponse.succeed(data=data);
129 130 # subscription windows 131 @serviceConnectionError
132 - def getWindows(self, uid, **kwargs):
133 response = self._getFacade().getWindows(uid) 134 return DirectResponse.succeed(data=Zuul.marshal(response))
135 136 @serviceConnectionError
137 - def addWindow(self, contextUid, newId):
138 response = self._getFacade().addWindow(contextUid, newId) 139 audit('UI.NotificationWindow.Add', newId, notification=contextUid) 140 return DirectResponse.succeed(data=Zuul.marshal(response))
141 142 @serviceConnectionError
143 - def removeWindow(self, uid):
144 response = self._getFacade().removeWindow(uid) 145 audit('UI.NotificationWindow.Remove', uid) 146 return DirectResponse.succeed(data=Zuul.marshal(response))
147 148 @serviceConnectionError
149 - def getWindow(self, uid):
150 response = self._getFacade().getWindow(uid) 151 return DirectResponse.succeed(data=Zuul.marshal(response))
152 153 @serviceConnectionError
154 - def updateWindow(self, **data):
155 windowUid = data['uid'] 156 response = self._getFacade().updateWindow(data) 157 audit('UI.NotificationWindow.Edit', windowUid, data_=data) 158 return DirectResponse.succeed(data=Zuul.marshal(response))
159 160 @serviceConnectionError
161 - def exportConfiguration(self, triggerIds=None, notificationIds=None):
162 facade = self._getFacade() 163 triggers, notifications = facade.exportConfiguration(triggerIds, notificationIds) 164 msg = "Exported %d triggers and %d notifications" % ( 165 len(triggers), len(notifications)) 166 audit('UI.TriggerNotification.Export', msg) 167 return DirectResponse.succeed(triggers=Zuul.marshal(triggers), 168 notifications=Zuul.marshal(notifications), 169 msg=msg)
170 171 @serviceConnectionError
172 - def importConfiguration(self, triggers=None, notifications=None):
173 try: 174 tcount = len(triggers) if triggers is not None else 0 175 ncount = len(notifications) if notifications is not None else 0 176 facade = self._getFacade() 177 itcount, incount = facade.importConfiguration(triggers, notifications) 178 msg = "Imported %d of %d triggers and %d of %d notifications" % ( 179 tcount, itcount, ncount, incount) 180 audit('UI.TriggerNotification.Import', msg) 181 return DirectResponse.succeed(msg=msg) 182 except Exception as ex: 183 audit('UI.TriggerNotification.Import', "Failed to import trigger/notification data") 184 log.exception("Unable to import data:\ntriggers=%s\nnotifications=%s", 185 repr(triggers), repr(notifications)) 186 return DirectResponse.fail(str(ex))
187