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 audit('UI.Service.Add', contextUid + '/' + id)
66 return DirectResponse()
67
68 - def query(self, limit=None, start=None, sort=None, dir=None, params=None,
69 page=None, history=False, uid=None, criteria=()):
70 """
71 Retrieve a list of services based on a set of parameters.
72
73 @type limit: integer
74 @param limit: (optional) Number of items to return; used in pagination
75 (default: None)
76 @type start: integer
77 @param start: (optional) Offset to return the results from; used in
78 pagination (default: None)
79 @type sort: string
80 @param sort: (optional) Key on which to sort the return results (default:
81 None)
82 @type dir: string
83 @param dir: (optional) Sort order; can be either 'ASC' or 'DESC'
84 (default: None)
85 @type params: dictionary
86 @param params: (optional) Key-value pair of filters for this search.
87 @type history: boolean
88 @param history: not used
89 @type uid: string
90 @param uid: Service class UID to query
91 @type criteria: list
92 @param criteria: not used
93 @rtype: DirectResponse
94 @return: B{Properties}:
95 - services: ([dictionary]) List of objects representing services
96 - totalCount: (integer) Total number of services
97 - hash: (string) Hashcheck of the current services state
98 - disabled: (boolean) True if current user cannot manage services
99 """
100 if uid is None:
101 uid = "/".join(self.context.getPhysicalPath())
102
103 if isinstance(params, basestring):
104 params = unjson(params)
105 services = self.api.getList(limit, start, sort, dir, params, uid,
106 criteria)
107
108 disabled = not Zuul.checkPermission('Manage DMD')
109
110 data = Zuul.marshal(services['serviceInfos'], keys=('name','description', 'count', 'uid','port'))
111 return DirectResponse(services=data, totalCount=services['total'],
112 hash=services['hash'], disabled=disabled)
113
114
116 """
117 Returns the tree structure of an organizer hierarchy.
118
119 @type id: string
120 @param id: Id of the root node of the tree to be returned
121 @rtype: [dictionary]
122 @return: Object representing the tree
123 """
124 tree = self.api.getTree(id)
125 data = Zuul.marshal(tree)
126 return [data]
127
129 """
130 Returns the tree structure of an organizer hierarchy, only including
131 organizers.
132
133 @type id: string
134 @param id: Id of the root node of the tree to be returned
135 @rtype: [dictionary]
136 @return: Object representing the organizer tree
137 """
138 tree = self.api.getOrganizerTree(id)
139 data = Zuul.marshal(tree)
140 return [data]
141
142 - def getInfo(self, uid, keys=None):
143 """
144 Get the properties of a service.
145
146 @type uid: string
147 @param uid: Unique identifier of a service
148 @type keys: list
149 @param keys: (optional) List of keys to include in the returned
150 dictionary. If None then all keys will be returned
151 (default: None)
152 @rtype: DirectResponse
153 @return: B{Properties}
154 - data: (dictionary) Object representing a service's properties
155 - disabled: (boolean) True if current user cannot manage services
156 """
157 service = self.api.getInfo(uid)
158 data = Zuul.marshal(service, keys)
159 if 'serviceKeys' in data and isinstance(data['serviceKeys'], (tuple, list)):
160 data['serviceKeys'] = ', '.join(data['serviceKeys'])
161 disabled = not Zuul.checkPermission('Manage DMD')
162 return DirectResponse.succeed(data=data, disabled=disabled)
163
164 @require('Manage DMD')
166 """
167 Set attributes on a service.
168 This method accepts any keyword argument for the property that you wish
169 to set. The only required property is "uid".
170
171 @type uid: string
172 @keyword uid: Unique identifier of a service
173 @rtype: DirectResponse
174 @return: Success message
175 """
176 serviceUid = data['uid']
177 service = self.api.getInfo(serviceUid)
178 if 'serviceKeys' in data and isinstance(data['serviceKeys'], str):
179 data['serviceKeys'] = tuple(l.strip() for l in data['serviceKeys'].split(','))
180 Zuul.unmarshal(data, service)
181 audit('UI.Service.Edit', serviceUid, data_=data)
182 return DirectResponse.succeed()
183
184 - def getInstances(self, uid, start=0, params=None, limit=50, sort='name',
185 page=None, dir='ASC'):
186 """
187 Get a list of instances for a service UID.
188
189 @type uid: string
190 @param uid: Service UID to get instances of
191 @type start: integer
192 @param start: (optional) Offset to return the results from; used in
193 pagination (default: 0)
194 @type params: dictionary
195 @param params: (optional) Key-value pair of filters for this search.
196 @type limit: integer
197 @param limit: (optional) Number of items to return; used in pagination
198 (default: 50)
199 @type sort: string
200 @param sort: (optional) Key on which to sort the return results (default:
201 'name')
202 @type dir: string
203 @param dir: (optional) Sort order; can be either 'ASC' or 'DESC'
204 (default: 'ASC')
205 @rtype: DirectResponse
206 @return: B{Properties}:
207 - data: ([dictionary]) List of objects representing service instances
208 - totalCount: (integer) Total number of instances
209 """
210 if isinstance(params, basestring):
211 params = unjson(params)
212 instances = self.api.getInstances(uid, start=start, params=params,
213 limit=limit, sort=sort, dir=dir)
214
215 keys = ['description', 'device', 'locking', 'monitored', 'name',
216 'pingStatus', 'uid']
217 data = Zuul.marshal(instances, keys)
218 return DirectResponse.succeed(data=data, totalCount=instances.total)
219
220 @require('Manage DMD')
222 """
223 Move service(s) from one organizer to another.
224
225 @type sourceUids: [string]
226 @param sourceUids: UID(s) of the service(s) to move
227 @type targetUid: string
228 @param targetUid: UID of the organizer to move to
229 @rtype: DirectResponse
230 @return: Success messsage
231 """
232 self.api.moveServices(sourceUids, targetUid)
233 for uid in sourceUids:
234 audit('UI.Service.Move', uid, target=targetUid)
235 return DirectResponse.succeed()
236
238 """
239 Get a list of unmonitored start modes for a Windows service.
240
241 @type uid: string
242 @param uid: Unique ID of a Windows service.
243 @rtype: DirectResponse
244 @return: B{Properties}:
245 - data: ([string]) List of unmonitored start modes for a Windows service
246 """
247 data = self.api.getUnmonitoredStartModes(uid)
248 return DirectResponse.succeed(data=Zuul.marshal(data))
249
251 """
252 Get a list of monitored start modes for a Windows service.
253
254 @type uid: string
255 @param uid: Unique ID of a Windows service.
256 @rtype: DirectResponse
257 @return: B{Properties}:
258 - data: ([string]) List of monitored start modes for a Windows service
259 """
260 data = self.api.getMonitoredStartModes(uid)
261 return DirectResponse.succeed(data=Zuul.marshal(data))
262