1
2
3
4
5
6
7
8
9
10
11 """
12 Operations for Templates.
13
14 Available at: /zport/dmd/template_router
15 """
16
17 from Products import Zuul
18 from Products.ZenUtils.Ext import DirectResponse
19 from Products.ZenUtils.Utils import getDisplayType
20 from Products.Zuul.decorators import require
21 from Products.Zuul.form.interfaces import IFormBuilder
22 from Products.Zuul.routers import TreeRouter
23 from Products.ZenMessaging.audit import audit
24 from Products.ZenModel.ThresholdClass import ThresholdClass
28 """
29 A JSON/ExtDirect interface to operations on templates
30 """
31
33 return Zuul.getFacade('template', self.context)
34
36 """
37 Get all templates.
38
39 @type id: string
40 @param id: not used
41 @rtype: [dictionary]
42 @return: List of objects representing the templates in tree hierarchy
43 """
44 facade = self._getFacade()
45 templates = facade.getTemplates(id)
46 data = Zuul.marshal(templates)
47 return data
48
50 """
51 Get all templates by device class. This will return a tree where device
52 classes are nodes, and templates are leaves.
53
54 @type id: string
55 @param id: not used
56 @rtype: [dictionary]
57 @return: List of objects representing the templates in tree hierarchy
58 """
59 facade = self._getFacade()
60 templates = facade.getTree(id)
61 return [Zuul.marshal(templates)]
62
64 """
65 Get a list of available device classes where new templates can be added.
66
67 @type query: string
68 @param query: not used
69 @rtype: DirectResponse
70 @return: B{Properties}:
71 - data: ([dictionary]) List of objects containing an available device
72 class UID and a human-readable label for that class
73
74 """
75 facade = self._getFacade()
76 data = facade.getAddTemplateTargets(query)
77 return DirectResponse.succeed(data=data)
78
79 @require('Manage DMD')
81 """
82 Add a template to a device class.
83
84 @type id: string
85 @param id: Unique ID of the template to add
86 @type targetUid: string
87 @param targetUid: Unique ID of the device class to add template to
88 @rtype: DirectResponse
89 @return: B{Properties}:
90 - nodeConfig: (dictionary) Object representing the added template
91 """
92 facade = self._getFacade()
93 templateNode = facade.addTemplate(id, targetUid)
94 audit('UI.Template.Add', templateNode, deviceclass=targetUid)
95 return DirectResponse.succeed(nodeConfig=Zuul.marshal(templateNode))
96
97 @require('Manage DMD')
99 """
100 Delete a template.
101
102 @type uid: string
103 @param uid: Unique ID of the template to delete
104 @rtype: DirectResponse
105 @return: Success message
106 """
107 facade = self._getFacade()
108 facade.deleteTemplate(uid)
109 msg = "Deleted node '%s'" % uid
110 audit('UI.Template.Delete', uid)
111 return DirectResponse.succeed(msg=msg)
112
113 @require('View')
115 """
116 @type uid: string
117 @param uid: Identifier for the object we want templates on, must descend from RRDView
118 @rtype: DirectResponse
119 @return: List of templates
120 """
121 facade = self._getFacade()
122 templates = facade.getObjTemplates(uid)
123 data = Zuul.marshal(templates)
124 return DirectResponse.succeed(data=data)
125
126 @require('Manage DMD')
128 """
129 @type uid: string
130 @param uid: Identifer of the obj we wish to make the template local for
131 @type templateName: string
132 @param templateName: identifier of the template
133 """
134 facade = self._getFacade()
135 facade.makeLocalRRDTemplate(uid, templateName)
136 audit('UI.Template.MakeLocal', templateName, target=uid)
137 return DirectResponse.succeed()
138
139 @require('Manage DMD')
141 """
142 @type uid: string
143 @param uid: Identifer of the obj we wish to remove the local template
144 @type templateName: string
145 @param templateName: identifier of the local template
146 """
147 facade = self._getFacade()
148 facade.removeLocalRRDTemplate(uid, templateName)
149 audit('UI.Template.RemoveLocal', templateName, target=uid)
150 return DirectResponse.succeed()
151
153 """
154 Get the thresholds for a template.
155
156 @type uid: string
157 @param uid: Unique ID of a template
158 @type query: string
159 @param query: not used
160 @rtype: [dictionary]
161 @return: List of objects representing representing thresholds
162 """
163 facade = self._getFacade()
164 thresholds = facade.getThresholds(uid)
165 return DirectResponse.succeed(data=Zuul.marshal(thresholds))
166
168 """
169 Get a threshold's details.
170
171 @type uid: string
172 @param uid: Unique ID of a threshold
173 @rtype: dictionary
174 @return: B{Properties}:
175 - record: (dictionary) Object representing the threshold
176 - form: (dictionary) Object representing an ExtJS form for the threshold
177 """
178 facade = self._getFacade()
179 thresholdDetails = facade.getThresholdDetails(uid)
180 form = IFormBuilder(thresholdDetails).render(fieldsets=False)
181
182 data = Zuul.marshal(dict(record=thresholdDetails, form=form))
183 return data
184
186 """
187 Get a list of available data points for a template.
188
189 @type query: string
190 @param query: not used
191 @type uid: string
192 @param uid: Unique ID of a template
193 @rtype: DirectResponse
194 @return: B{Properties}:
195 - data: ([dictionary]) List of objects representing data points
196 """
197 datapoints = []
198 facade = self._getFacade()
199
200 datasources = facade.getDataSources(uid)
201 for datasource in datasources:
202 for datapoint in facade.getDataSources(datasource.uid):
203 datapoints.append(datapoint)
204 data = Zuul.marshal(datapoints)
205 return DirectResponse.succeed(data=data)
206
207 @require('Manage DMD')
209 """
210 Add a new data point to a data source.
211
212 @type dataSourceUid: string
213 @param dataSourceUid: Unique ID of the data source to add data point to
214 @type name: string
215 @param name: ID of the new data point
216 @rtype: DirectResponse
217 @return: Success message
218 """
219 facade = self._getFacade()
220 facade.addDataPoint(dataSourceUid, name)
221 audit('UI.DataPoint.Add', name, datasource=dataSourceUid)
222 return DirectResponse.succeed()
223
224 @require('Manage DMD')
226 """
227 Add a new data source to a template.
228
229 @type templateUid: string
230 @param templateUid: Unique ID of the template to add data source to
231 @type name: string
232 @param name: ID of the new data source
233 @type type: string
234 @param type: Type of the new data source. From getDataSourceTypes()
235 @rtype: DirectResponse
236 @return: Success message
237 """
238 facade = self._getFacade()
239 ds = facade.addDataSource(templateUid, name, type)
240 audit('UI.DataSource.Add', ds.getPrimaryId(), name=name, dstype=type,
241 template=templateUid)
242 return DirectResponse.succeed()
243
245 """
246 Get the data sources for a template.
247
248 @type id: string
249 @param id: Unique ID of a template
250 @rtype: [dictionary]
251 @return: List of objects representing representing data sources
252 """
253 facade = self._getFacade()
254 dataSources = facade.getDataSources(uid)
255 return DirectResponse.succeed(data=Zuul.marshal(dataSources))
256
258 """
259 Get a data source's details.
260
261 @type uid: string
262 @param uid: Unique ID of a data source
263 @rtype: dictionary
264 @return: B{Properties}:
265 - record: (dictionary) Object representing the data source
266 - form: (dictionary) Object representing an ExtJS form for the data
267 source
268 """
269 facade = self._getFacade()
270 details = facade.getDataSourceDetails(uid)
271 form = IFormBuilder(details).render()
272 data = Zuul.marshal(dict(record=details, form=form))
273 return data
274
276 """
277 Get a data point's details.
278
279 @type uid: string
280 @param uid: Unique ID of a data point
281 @rtype: dictionary
282 @return: B{Properties}:
283 - record: (dictionary) Object representing the data point
284 - form: (dictionary) Object representing an ExtJS form for the data
285 point
286 """
287 facade = self._getFacade()
288 details = facade.getDataPointDetails(uid)
289 form = IFormBuilder(details).render(fieldsets=False)
290 data = Zuul.marshal(dict(record=details, form=form))
291 return data
292
293 @require('Manage DMD')
295 """
296 Set attributes on an object.
297 This method accepts any keyword argument for the property that you wish
298 to set. The only required property is "uid".
299
300 @type uid: string
301 @keyword uid: Unique identifier of an object
302 @rtype: DirectResponse
303 @return: B{Properties}:
304 - data: (dictionary) The modified object
305 """
306 uid = data['uid']
307 del data['uid']
308 obj = self._getFacade()._getObject(uid)
309 oldData = self._getInfoData(obj, data)
310 info = self._getFacade().setInfo(uid, data)
311 newData = self._getInfoData(obj, data)
312
313 thresholdType = obj.getTypeName() if isinstance(obj, ThresholdClass) else None
314 audit(['UI', getDisplayType(obj), 'Edit'], obj, thresholdType=thresholdType,
315 data_=newData, oldData_=oldData,
316 skipFields_=('newId',))
317 return DirectResponse.succeed(data=Zuul.marshal(info))
318
320
321 info = self._getFacade()._getDataSourceInfoFromObject(obj)
322 values = {}
323 for key in keys.keys():
324 val = getattr(info, key, None)
325 if val is not None:
326 values[key] = str(val)
327
328 if key == 'dsnames' and values[key] == '':
329 values[key] = '[]'
330 values['name'] = info.getName()
331 return values
332
333 @require('Manage DMD')
335 """
336 Add a threshold.
337
338 @type uid: string
339 @keyword uid: Unique identifier of template to add threshold to
340 @type thresholdType: string
341 @keyword thresholdType: Type of the new threshold. From getThresholdTypes()
342 @type thresholdId: string
343 @keyword thresholdId: ID of the new threshold
344 @type dataPoints: [string]
345 @keyword dataPoints: List of data points to select for this threshold
346 @rtype: DirectResponse
347 @return: Success message
348 """
349 uid = data['uid']
350 thresholdType = data['thresholdType']
351 thresholdId = data['thresholdId']
352 dataPoints = data.get('dataPoints', None)
353 facade = self._getFacade()
354 facade.addThreshold(uid, thresholdType, thresholdId, dataPoints)
355 thresholdUid = uid + '/thresholds/' + thresholdId
356 audit('UI.Threshold.Add', thresholdUid, thresholdtype=thresholdType,
357 datapoints=dataPoints)
358 return DirectResponse.succeed()
359
360 @require('Manage DMD')
362 """
363 Remove a threshold.
364
365 @type uid: string
366 @param uid: Unique identifier of threshold to remove
367 @rtype: DirectResponse
368 @return: Success message
369 """
370 facade = self._getFacade()
371 thresholdType = facade._getThresholdClass(uid).getTypeName()
372 facade.removeThreshold(uid)
373 audit('UI.Threshold.Delete', uid, thresholdType=thresholdType)
374 return DirectResponse.succeed()
375
377 """
378 Get a list of available threshold types.
379
380 @type query: string
381 @param query: not used
382 @rtype: [dictionary]
383 @return: List of objects representing threshold types
384 """
385 facade = self._getFacade()
386 data = facade.getThresholdTypes()
387 return DirectResponse.succeed(data=data)
388
390 """
391 Get a list of available data source types.
392
393 @type query: string
394 @param query: not used
395 @rtype: [dictionary]
396 @return: List of objects representing data source types
397 """
398 facade = self._getFacade()
399 data = facade.getDataSourceTypes()
400 data = sorted(data, key=lambda row: row['type'].lower())
401 return DirectResponse.succeed(data=data)
402
404 """
405 Get the graph definitions for a template.
406
407 @type uid: string
408 @param uid: Unique ID of a template
409 @type query: string
410 @param query: not used
411 @rtype: [dictionary]
412 @return: List of objects representing representing graphs
413 """
414 facade = self._getFacade()
415 graphs = facade.getGraphs(uid)
416 return Zuul.marshal(graphs)
417
418 @require('Manage DMD')
420 """
421 Add a data point to a graph.
422
423 @type dataPointUid: string
424 @param dataPointUid: Unique ID of the data point to add to graph
425 @type graphUid: string
426 @param graphUid: Unique ID of the graph to add data point to
427 @type includeThresholds: boolean
428 @param includeThresholds: (optional) True to include related thresholds
429 (default: False)
430 @rtype: DirectResponse
431 @return: Success message
432 """
433 facade = self._getFacade()
434 facade.addDataPointToGraph(dataPointUid, graphUid, includeThresholds)
435 audit('UI.Graph.AddDataPoint', graphUid, datapoint=dataPointUid,
436 includeThresholds=includeThresholds)
437 return DirectResponse.succeed()
438
440 """
441 Get a list of available device classes to copy a template to.
442
443 @type uid: string
444 @param uid: Unique ID of the template to copy
445 @type query: string
446 @param query: (optional) Filter the returned targets' names based on this
447 parameter (default: '')
448 @rtype: DirectResponse
449 @return: B{Properties}:
450 - data: ([dictionary]) List of objects containing an available device
451 class UID and a human-readable label for that class
452 """
453 facade = self._getFacade()
454 data = Zuul.marshal( facade.getCopyTargets(uid, query) )
455 return DirectResponse.succeed(data=data)
456
457 @require('Manage DMD')
459 """
460 Copy a template to a device or device class.
461
462 @type uid: string
463 @param uid: Unique ID of the template to copy
464 @type targetUid: string
465 @param targetUid: Unique ID of the device or device class to bind to template
466 @rtype: DirectResponse
467 @return: Success message
468 """
469 facade = self._getFacade()
470 facade.copyTemplate(uid, targetUid)
471 audit('UI.Template.Copy', uid, target=targetUid)
472 return DirectResponse.succeed()
473
474 @require('Manage DMD')
476 """
477 Add a new graph definition to a template.
478
479 @type templateUid: string
480 @param templateUid: Unique ID of the template to add graph definition to
481 @type graphDefinitionId: string
482 @param graphDefinitionId: ID of the new graph definition
483 @rtype: DirectResponse
484 @return: Success message
485 """
486 facade = self._getFacade()
487 facade.addGraphDefinition(templateUid, graphDefinitionId)
488 audit('UI.GraphDefinition.Add', graphDefinitionId, template=templateUid)
489 return DirectResponse.succeed()
490
491 @require('Manage DMD')
493 """
494 Delete a data source.
495
496 @type uid: string
497 @param uid: Unique ID of the data source to delete
498 @rtype: DirectResponse
499 @return: Success message
500 """
501 facade = self._getFacade()
502 facade.deleteDataSource(uid)
503 audit('UI.DataSource.Delete', uid)
504 return DirectResponse.succeed()
505
506 @require('Manage DMD')
508 """
509 Delete a data point.
510
511 @type uid: string
512 @param uid: Unique ID of the data point to delete
513 @rtype: DirectResponse
514 @return: Success message
515 """
516 facade = self._getFacade()
517 facade.deleteDataPoint(uid)
518 audit('UI.DataPoint.Delete', uid)
519 return DirectResponse.succeed()
520
521 @require('Manage DMD')
523 """
524 Delete a graph definition.
525
526 @type uid: string
527 @param uid: Unique ID of the graph definition to delete
528 @rtype: DirectResponse
529 @return: Success message
530 """
531 facade = self._getFacade()
532 facade.deleteGraphDefinition(uid)
533 audit('UI.GraphDefinition.Delete', uid)
534 return DirectResponse.succeed()
535
536 @require('Manage DMD')
538 """
539 Delete a graph point.
540
541 @type uid: string
542 @param uid: Unique ID of the graph point to delete
543 @rtype: DirectResponse
544 @return: Success message
545 """
546 facade = self._getFacade()
547 facade.deleteGraphPoint(uid)
548 audit('UI.GraphPoint.Remove', uid)
549 return DirectResponse.succeed()
550
552 """
553 Get a list of graph points for a graph definition.
554
555 @type uid: string
556 @param uid: Unique ID of a graph definition
557 @rtype: DirectResponse
558 @return: B{Properties}:
559 - data: ([dictionary]) List of objects representing graph points
560 """
561 facade = self._getFacade()
562 graphPoints = facade.getGraphPoints(uid)
563 return DirectResponse.succeed(data=Zuul.marshal(graphPoints))
564
566 """
567 Get the properties of an object.
568
569 @type uid: string
570 @param uid: Unique identifier of an object
571 @rtype: DirectResponse
572 @return: B{Properties}
573 - data: (dictionary) Object properties
574 - form: (dictionary) Object representing an ExtJS form for the object
575 """
576 facade = self._getFacade()
577 info = facade.getInfo(uid)
578 form = IFormBuilder(info).render(fieldsets=False)
579 return DirectResponse(success=True, data=Zuul.marshal(info), form=form)
580
581 @require('Manage DMD')
583 """
584 Add a threshold to a graph definition.
585
586 @type graphUid: string
587 @param graphUid: Unique ID of the graph definition to add threshold to
588 @type thresholdUid: string
589 @param thresholdUid: Unique ID of the threshold to add
590 @rtype: DirectResponse
591 @return: Success message
592 """
593 facade = self._getFacade()
594 facade.addThresholdToGraph(graphUid, thresholdUid)
595 audit('UI.Graph.AddThreshold', graphUid, thresholdclass=thresholdUid)
596 return DirectResponse.succeed()
597
598 @require('Manage DMD')
600 """
601 Add a custom graph point to a graph definition.
602
603 @type graphUid: string
604 @param graphUid: Unique ID of the graph definition to add graph point to
605 @type customId: string
606 @param customId: ID of the new custom graph point
607 @type customType: string
608 @param customType: Type of the new graph point. From getGraphInstructionTypes()
609 @rtype: DirectResponse
610 @return: Success message
611 """
612 facade = self._getFacade()
613 facade.addCustomToGraph(graphUid, customId, customType)
614 audit('UI.Graph.AddCustomGraphPoint', graphUid, custom=customId)
615 return DirectResponse.succeed()
616
618 """
619 Get a list of available instruction types for graph points.
620
621 @type query: string
622 @param query: not used
623 @rtype: DirectResponse
624 @return: B{Properties}:
625 - data: ([dictionary]) List of objects representing instruction types
626 """
627 facade = self._getFacade()
628 types = facade.getGraphInstructionTypes()
629 return DirectResponse.succeed(data=Zuul.marshal(types))
630
631 @require('Manage DMD')
633 """
634 Sets the sequence of graph points in a graph definition.
635
636 @type uids: [string]
637 @param uids: List of graph point UID's in desired order
638 @rtype: DirectResponse
639 @return: Success message
640 """
641 facade = self._getFacade()
642 facade.setGraphPointSequence(uids)
643
644
645 audit('UI.GraphDefinition.SetGraphPointSequence', sequence=uids)
646 return DirectResponse.succeed()
647
649 """
650 Get a graph definition.
651
652 @type uid: string
653 @param uid: Unique ID of the graph definition to retrieve
654 @rtype: DirectResponse
655 @return: B{Properties}:
656 - data: (dictionary) Object representing a graph definition
657 """
658 facade = self._getFacade()
659 graphDef = facade.getGraphDefinition(uid)
660 return DirectResponse.succeed(data=Zuul.marshal(graphDef))
661
662 @require('Manage DMD')
664 """
665 Set attributes on an graph definition.
666 This method accepts any keyword argument for the property that you wish
667 to set. Properties are enumerated via getGraphDefinition(). The only
668 required property is "uid".
669
670 @type uid: string
671 @keyword uid: Unique identifier of an object
672 @rtype: DirectResponse
673 @return: B{Properties}:
674 - data: (dictionary) The modified object
675 """
676 uid = data['uid']
677 del data['uid']
678 for int_attr in ('miny', 'maxy'):
679 try:
680 x = int(data[int_attr])
681 except (ValueError, KeyError):
682 x = -1
683 data[int_attr] = x
684 obj = self._getFacade()._getObject(uid)
685 oldData = self._getInfoData(obj, data)
686 info = self._getFacade().setInfo(uid, data)
687 newData = self._getInfoData(obj, data)
688 audit(['UI', getDisplayType(obj), 'Edit'], data_=newData, oldData_=oldData,
689 skipFields_=('newId',))
690 return DirectResponse.succeed()
691
692 @require('Manage DMD')
694 """
695 Sets the sequence of graph definitions.
696
697 @type uids: [string]
698 @param uids: List of graph definition UID's in desired order
699 @rtype: DirectResponse
700 @return: Success message
701 """
702 facade = self._getFacade()
703 facade._setGraphDefinitionSequence(uids)
704
705
706 audit('UI.Template,SetGraphDefinitionSequence', sequence=uids)
707 return DirectResponse.succeed()
708