1
2
3
4
5
6
7
8
9
10
11 """
12 Operations for Processes.
13
14 Available at: /zport/dmd/process_router
15 """
16 import re
17 from sre_parse import parse_template
18
19 from Products import Zuul
20 from Products.Zuul.decorators import require
21 from Products.Zuul.routers import TreeRouter
22 from Products.ZenUtils.Ext import DirectResponse
23 from Products.ZenUtils.jsonutils import unjson
24 from Products.ZenMessaging.audit import audit
27 """
28 A JSON/ExtDirect interface to operations on processes
29 """
30
32 return Zuul.getFacade('process', self.context)
33
35 """
36 Returns the tree structure of an organizer hierarchy where
37 the root node is the organizer identified by the id parameter.
38
39 @type id: string
40 @param id: Id of the root node of the tree to be returned
41 @rtype: [dictionary]
42 @return: Object representing the tree
43 """
44 facade = self._getFacade()
45 tree = facade.getTree(id)
46 data = Zuul.marshal(tree)
47 return [data]
48
50 """
51 Move a process or organizer from one organizer to another.
52
53 @type uid: string
54 @param uid: UID of the process or organizer to move
55 @type targetUid: string
56 @param targetUid: UID of the organizer to move to
57 @rtype: DirectResponse
58 @return: B{Properties}:
59 - uid: (dictionary) The new uid for moved process or organizer
60 """
61 facade = self._getFacade()
62 old_uid = uid
63 primaryPath = facade.moveProcess(uid, targetUid)
64 id = '.'.join(primaryPath)
65 uid = '/'.join(primaryPath)
66
67
68 audit('UI.Process.Move', uid, old=old_uid)
69 return DirectResponse.succeed(uid=uid, id=id)
70
72 """
73 Get the properties of a process.
74
75 @type uid: string
76 @param uid: Unique identifier of a process
77 @type keys: list
78 @param keys: (optional) List of keys to include in the returned
79 dictionary. If None then all keys will be returned
80 (default: None)
81 @rtype: DirectResponse
82 @return: B{Properties}
83 - data: (dictionary) Object representing a process's properties
84 """
85 facade = self._getFacade()
86 process = facade.getInfo(uid)
87 data = Zuul.marshal(process, keys)
88 return DirectResponse.succeed(data=data)
89
90 @require('Manage DMD')
92 """
93 Set attributes on a process.
94 This method accepts any keyword argument for the property that you wish
95 to set. The only required property is "uid".
96
97 @type uid: string
98 @keyword uid: Unique identifier of a process
99 @rtype: DirectResponse
100 @return: B{Properties}
101 - data: (dictionary) Object representing a process's new properties
102 """
103 facade = self._getFacade()
104 processUid = data['uid']
105 for regexParam in ['includeRegex', 'excludeRegex', 'replaceRegex']:
106 regex = data.get(regexParam)
107 if regex:
108 try:
109 re.compile(regex)
110 except re.error as e:
111 m = "%s : %s" % (regexParam, e)
112 return DirectResponse.fail(msg=m)
113 replaceRegex = data.get('replaceRegex')
114 if replaceRegex:
115 replaceRegex = re.compile(replaceRegex)
116 replacement = data.get('replacement')
117 if replacement:
118 try:
119 groups, literals = parse_template(replacement,replaceRegex)
120 for index, group in groups:
121 if group > replaceRegex.groups:
122 m = "Group (%s) referenced in replacement must be defined in replaceRegex" % group
123 return DirectResponse.fail(msg=m)
124 except re.error as e:
125 m = "replacement : %s" % (e,)
126 return DirectResponse.fail(msg=m)
127
128 process = facade.getInfo(processUid)
129 audit('UI.Process.Edit', processUid, data_=data, skipFields_=('uid'))
130 return DirectResponse.succeed(data=Zuul.unmarshal(data, process))
131
132 - def getInstances(self, uid, start=0, params=None, limit=50, sort='name',
133 page=None, dir='ASC'):
134 """
135 Get a list of instances for a process UID.
136
137 @type uid: string
138 @param uid: Process UID to get instances of
139 @type start: integer
140 @param start: (optional) Offset to return the results from; used in
141 pagination (default: 0)
142 @type params: dictionary
143 @param params: (optional) Key-value pair of filters for this search.
144 @type limit: integer
145 @param limit: (optional) Number of items to return; used in pagination
146 (default: 50)
147 @type sort: string
148 @param sort: (optional) Key on which to sort the return results (default:
149 'name')
150 @type dir: string
151 @param dir: (optional) Sort order; can be either 'ASC' or 'DESC'
152 (default: 'ASC')
153 @rtype: DirectResponse
154 @return: B{Properties}:
155 - data: ([dictionary]) List of objects representing process instances
156 - total: (integer) Total number of instances
157 """
158 facade = self._getFacade()
159 instances = facade.getInstances(uid, start, limit, sort, dir, params)
160 keys = ['device', 'monitored', 'pingStatus', 'processName', 'name',
161 'uid', 'minProcessCount', 'maxProcessCount']
162 data = Zuul.marshal(instances, keys)
163 return DirectResponse.succeed(data=data, totalCount=instances.total)
164
166 """
167 Get the current processes sequence.
168
169 @rtype: DirectResponse
170 @return: B{Properties}:
171 - data: ([dictionary]) List of objects representing processes in
172 sequence order
173 """
174 facade = self._getFacade()
175 sequence = facade.getSequence()
176 data = Zuul.marshal(sequence)
177 return DirectResponse.succeed(data=data)
178
180 """
181 Set the current processes sequence.
182
183 @type uids: [string]
184 @param uids: The set of process uid's in the desired sequence
185 @rtype: DirectResponse
186 @return: Success message
187 """
188 facade = self._getFacade()
189 facade.setSequence(uids)
190 audit('UI.Process.SetSequence', sequence=uids)
191 return DirectResponse.succeed()
192
194 """
195 Get the current processes sequence.
196
197 @rtype: DirectResponse
198 @return: B{Properties}:
199 - data: ([dictionary]) List of objects representing processes in
200 sequence order
201 """
202 facade = self._getFacade()
203 sequence = facade.getSequence2()
204 data = Zuul.marshal(sequence)
205 return DirectResponse.succeed(data=data)
206
208 """
209 Get the current processes sequence.
210
211 @rtype: DirectResponse
212 @return: B{Properties}:
213 - data: ([dictionary]) List of objects representing processes in
214 sequence order
215 """
216 facade = self._getFacade()
217 sequence = facade.applyOSProcessClassMatchers(kwargs['uids'], kwargs['lines'])
218 data = Zuul.marshal(sequence)
219 return DirectResponse.succeed(data=data, total=len(data))
220
222 """
223 @type deviceGuid: string
224 @param deviceGuid: Service class UUID of the device to get process list
225 """
226 facade = self._getFacade()
227 processList = facade.getProcessList(kwargs['deviceGuid'])
228 data = Zuul.marshal(processList)
229 return DirectResponse.succeed(data=data)
230
231
232 - def query(self, limit=None, start=None, sort=None, dir=None, params=None,
233 page=None, history=False, uid=None, criteria=()):
234 """
235 Retrieve a list of processes based on a set of parameters.
236
237 @type limit: integer
238 @param limit: (optional) Number of items to return; used in pagination
239 (default: None)
240 @type start: integer
241 @param start: (optional) Offset to return the results from; used in
242 pagination (default: None)
243 @type sort: string
244 @param sort: (optional) Key on which to sort the return results (default:
245 None)
246 @type dir: string
247 @param dir: (optional) Sort order; can be either 'ASC' or 'DESC'
248 (default: None)
249 @type params: dictionary
250 @param params: (optional) Key-value pair of filters for this search.
251 @type history: boolean
252 @param history: not used
253 @type uid: string
254 @param uid: Service class UID to query
255 @type criteria: list
256 @param criteria: not used
257 @rtype: DirectResponse
258 @return: B{Properties}:
259 - processes: ([dictionary]) List of objects representing processes
260 - totalCount: (integer) Total number of processes
261 - hash: (string) Hashcheck of the current processes state
262 - disabled: (boolean) True if current user cannot manage processes
263 """
264 facade = self._getFacade()
265 if uid is None:
266 uid = self.context
267
268 if isinstance(params, basestring):
269 params = unjson(params)
270
271 processes = facade.getList(limit, start, sort, dir, params, uid,
272 criteria)
273 disabled = not Zuul.checkPermission('Manage DMD')
274
275 data = Zuul.marshal(processes)
276 return DirectResponse(processes=data, totalCount=processes.total,
277 hash=processes.hash_, disabled=disabled)
278