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