1
2
3
4
5
6
7
8
9
10
11 """
12 Operations for Services.
13
14 Available at: /zport/dmd/service_router
15 """
16
17 from Products import Zuul
18 from Products.Zuul.routers import TreeRouter
19 from Products.Zuul.decorators import require
20 from Products.ZenUtils.Ext import DirectResponse
21 from Products.ZenUtils.jsonutils import unjson
22 from Products.ZenMessaging.audit import audit
26 """
27 A JSON/ExtDirect interface to operations on services
28 """
29
31 self.api = Zuul.getFacade('service')
32 self.context = context
33 self.request = request
34 super(ServiceRouter, self).__init__(context, request)
35
38
40
41 levels = len(uid.split('/'))
42 return levels > 5
43
47
48 @require('Manage DMD')
49 - def addClass(self, contextUid, id, posQuery=None):
50 """
51 Add a new service class.
52
53 @type contextUid: string
54 @param contextUid: Unique ID of the service ogranizer to add new class to
55 @type id: string
56 @param id: ID of the new service
57 @type posQuery: dictionary
58 @param posQuery: Object defining a query where the returned position will lie
59 @rtype: DirectResponse
60 @return: B{Properties}:
61 - newIndex: (integer) Index of the newly added class in the query
62 defined by posQuery
63 """
64 newUid = self.api.addClass(contextUid, id)
65 if isinstance(posQuery.get('params'), basestring):
66 posQuery['params'] = unjson(posQuery['params'])
67 result = self.api.getList(**posQuery)
68 for count, serviceInfo in enumerate(result['serviceInfos']):
69 if serviceInfo.uid == newUid:
70 newIndex = count
71 break
72 else:
73 raise Exception('The new service was added, but the system was '
74 'unable to add it to the list.')
75 audit('UI.Service.Add', contextUid + '/' + id)
76 return DirectResponse(newIndex=newIndex)
77
78 - def query(self, limit=None, start=None, sort=None, dir=None, params=None,
79 page=None, history=False, uid=None, criteria=()):
80 """
81 Retrieve a list of services based on a set of parameters.
82
83 @type limit: integer
84 @param limit: (optional) Number of items to return; used in pagination
85 (default: None)
86 @type start: integer
87 @param start: (optional) Offset to return the results from; used in
88 pagination (default: None)
89 @type sort: string
90 @param sort: (optional) Key on which to sort the return results (default:
91 None)
92 @type dir: string
93 @param dir: (optional) Sort order; can be either 'ASC' or 'DESC'
94 (default: None)
95 @type params: dictionary
96 @param params: (optional) Key-value pair of filters for this search.
97 @type history: boolean
98 @param history: not used
99 @type uid: string
100 @param uid: Service class UID to query
101 @type criteria: list
102 @param criteria: not used
103 @rtype: DirectResponse
104 @return: B{Properties}:
105 - services: ([dictionary]) List of objects representing services
106 - totalCount: (integer) Total number of services
107 - hash: (string) Hashcheck of the current services state
108 - disabled: (boolean) True if current user cannot manage services
109 """
110 if uid is None:
111 uid = "/".join(self.context.getPhysicalPath())
112
113 if isinstance(params, basestring):
114 params = unjson(params)
115 services = self.api.getList(limit, start, sort, dir, params, uid,
116 criteria)
117
118 disabled = not Zuul.checkPermission('Manage DMD')
119
120 data = Zuul.marshal(services['serviceInfos'], keys=('name','description', 'count', 'uid','port'))
121 return DirectResponse(services=data, totalCount=services['total'],
122 hash=services['hash'], disabled=disabled)
123
124
126 """
127 Returns the tree structure of an organizer hierarchy.
128
129 @type id: string
130 @param id: Id of the root node of the tree to be returned
131 @rtype: [dictionary]
132 @return: Object representing the tree
133 """
134 tree = self.api.getTree(id)
135 data = Zuul.marshal(tree)
136 return [data]
137
139 """
140 Returns the tree structure of an organizer hierarchy, only including
141 organizers.
142
143 @type id: string
144 @param id: Id of the root node of the tree to be returned
145 @rtype: [dictionary]
146 @return: Object representing the organizer tree
147 """
148 tree = self.api.getOrganizerTree(id)
149 data = Zuul.marshal(tree)
150 return [data]
151
152 - def getInfo(self, uid, keys=None):
153 """
154 Get the properties of a service.
155
156 @type uid: string
157 @param uid: Unique identifier of a service
158 @type keys: list
159 @param keys: (optional) List of keys to include in the returned
160 dictionary. If None then all keys will be returned
161 (default: None)
162 @rtype: DirectResponse
163 @return: B{Properties}
164 - data: (dictionary) Object representing a service's properties
165 - disabled: (boolean) True if current user cannot manage services
166 """
167 service = self.api.getInfo(uid)
168 data = Zuul.marshal(service, keys)
169 disabled = not Zuul.checkPermission('Manage DMD')
170 return DirectResponse.succeed(data=data, disabled=disabled)
171
172 @require('Manage DMD')
174 """
175 Set attributes on a service.
176 This method accepts any keyword argument for the property that you wish
177 to set. The only required property is "uid".
178
179 @type uid: string
180 @keyword uid: Unique identifier of a service
181 @rtype: DirectResponse
182 @return: Success message
183 """
184 serviceUid = data['uid']
185 service = self.api.getInfo(serviceUid)
186 if 'serviceKeys' in data and isinstance(data['serviceKeys'], str):
187 data['serviceKeys'] = tuple(l.strip() for l in data['serviceKeys'].split(','))
188 Zuul.unmarshal(data, service)
189 audit('UI.Service.Edit', serviceUid, data_=data)
190 return DirectResponse.succeed()
191
192 - def getInstances(self, uid, start=0, params=None, limit=50, sort='name',
193 page=None, dir='ASC'):
194 """
195 Get a list of instances for a service UID.
196
197 @type uid: string
198 @param uid: Service UID to get instances of
199 @type start: integer
200 @param start: (optional) Offset to return the results from; used in
201 pagination (default: 0)
202 @type params: dictionary
203 @param params: (optional) Key-value pair of filters for this search.
204 @type limit: integer
205 @param limit: (optional) Number of items to return; used in pagination
206 (default: 50)
207 @type sort: string
208 @param sort: (optional) Key on which to sort the return results (default:
209 'name')
210 @type dir: string
211 @param dir: (optional) Sort order; can be either 'ASC' or 'DESC'
212 (default: 'ASC')
213 @rtype: DirectResponse
214 @return: B{Properties}:
215 - data: ([dictionary]) List of objects representing service instances
216 - totalCount: (integer) Total number of instances
217 """
218 if isinstance(params, basestring):
219 params = unjson(params)
220 instances = self.api.getInstances(uid, start=start, params=params,
221 limit=limit, sort=sort, dir=dir)
222
223 keys = ['description', 'device', 'locking', 'monitored', 'name',
224 'pingStatus', 'uid']
225 data = Zuul.marshal(instances, keys)
226 return DirectResponse.succeed(data=data, totalCount=instances.total)
227
228 @require('Manage DMD')
230 """
231 Move service(s) from one organizer to another.
232
233 @type sourceUids: [string]
234 @param sourceUids: UID(s) of the service(s) to move
235 @type targetUid: string
236 @param targetUid: UID of the organizer to move to
237 @rtype: DirectResponse
238 @return: Success messsage
239 """
240 self.api.moveServices(sourceUids, targetUid)
241 for uid in sourceUids:
242 audit('UI.Service.Move', uid, target=targetUid)
243 return DirectResponse.succeed()
244
246 """
247 Get a list of unmonitored start modes for a Windows service.
248
249 @type uid: string
250 @param uid: Unique ID of a Windows service.
251 @rtype: DirectResponse
252 @return: B{Properties}:
253 - data: ([string]) List of unmonitored start modes for a Windows service
254 """
255 data = self.api.getUnmonitoredStartModes(uid)
256 return DirectResponse.succeed(data=Zuul.marshal(data))
257
259 """
260 Get a list of monitored start modes for a Windows service.
261
262 @type uid: string
263 @param uid: Unique ID of a Windows service.
264 @rtype: DirectResponse
265 @return: B{Properties}:
266 - data: ([string]) List of monitored start modes for a Windows service
267 """
268 data = self.api.getMonitoredStartModes(uid)
269 return DirectResponse.succeed(data=Zuul.marshal(data))
270