1
2
3
4
5
6
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')
28 """
29 Router for Triggers UI section under Events.
30 """
31
33 return Zuul.getFacade('triggers', self)
34
35 @serviceConnectionError
38
39 @serviceConnectionError
42
43 @serviceConnectionError
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
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
69
70 @serviceConnectionError
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
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
94 @serviceConnectionError
96 response = self._getFacade().getNotificationInfos()
97 return DirectResponse.succeed(data=Zuul.marshal(response))
98
99 @serviceConnectionError
101 response = self._getFacade().addNotification(newId, action)
102 audit('UI.Notification.Add', newId)
103 return DirectResponse.succeed(data=Zuul.marshal(response))
104
105 @serviceConnectionError
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
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
127
128 @serviceConnectionError
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
143
144
145 @serviceConnectionError
147 response = self._getFacade().getWindows(uid)
148 return DirectResponse.succeed(data=Zuul.marshal(response))
149
150 @serviceConnectionError
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
158 response = self._getFacade().removeWindow(uid)
159 audit('UI.NotificationWindow.Remove', uid)
160 return DirectResponse.succeed(data=Zuul.marshal(response))
161
162 @serviceConnectionError
164 response = self._getFacade().getWindow(uid)
165 return DirectResponse.succeed(data=Zuul.marshal(response))
166
167 @serviceConnectionError
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
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
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