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 MetricMixin
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 tplUid = facade.makeLocalRRDTemplate(uid, templateName)
136 audit('UI.Template.MakeLocal', templateName, target=uid)
137 return DirectResponse.succeed(tplUid=tplUid)
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 tplUid = facade.removeLocalRRDTemplate(uid, templateName)
149 audit('UI.Template.RemoveLocal', templateName, target=uid)
150 return DirectResponse.succeed(tplUid=tplUid)
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()
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
439 @require('Manage DMD')
441 """
442 Add all the datapoints in a datasource to the graph definition.
443
444 @type dataSourceUid: string
445 @param dataSourceUid: Unique ID of the data source to add to graph
446 @type graphUid: string
447 @param graphUid: Unique ID of the graph to add data source to
448 @type includeThresholds: boolean
449 @param includeThresholds: (optional) True to include related thresholds
450 (default: False)
451 @rtype: DirectResponse
452 @return: Success message
453 """
454 facade = self._getFacade()
455 facade.addDataSourceToGraph(dataSourceUid, graphUid, includeThresholds)
456 audit('UI.Graph.AddDataSource', graphUid, datapoint=dataSourceUid,
457 includeThresholds=includeThresholds)
458 return DirectResponse.succeed()
459
461 """
462 Get a list of available device classes to copy a template to.
463
464 @type uid: string
465 @param uid: Unique ID of the template to copy
466 @type query: string
467 @param query: (optional) Filter the returned targets' names based on this
468 parameter (default: '')
469 @rtype: DirectResponse
470 @return: B{Properties}:
471 - data: ([dictionary]) List of objects containing an available device
472 class UID and a human-readable label for that class
473 """
474 facade = self._getFacade()
475 data = Zuul.marshal( facade.getCopyTargets(uid, query) )
476 return DirectResponse.succeed(data=data)
477
478 @require('Manage DMD')
480 """
481 Copy a template to a device or device class.
482
483 @type uid: string
484 @param uid: Unique ID of the template to copy
485 @type targetUid: string
486 @param targetUid: Unique ID of the device or device class to bind to template
487 @rtype: DirectResponse
488 @return: Success message
489 """
490 facade = self._getFacade()
491 facade.copyTemplate(uid, targetUid)
492 audit('UI.Template.Copy', uid, target=targetUid)
493 return DirectResponse.succeed()
494
495 @require('Manage DMD')
497 """
498 Add a new graph definition to a template.
499
500 @type templateUid: string
501 @param templateUid: Unique ID of the template to add graph definition to
502 @type graphDefinitionId: string
503 @param graphDefinitionId: ID of the new graph definition
504 @rtype: DirectResponse
505 @return: Success message
506 """
507 facade = self._getFacade()
508 facade.addGraphDefinition(templateUid, graphDefinitionId)
509 audit('UI.GraphDefinition.Add', graphDefinitionId, template=templateUid)
510 return DirectResponse.succeed()
511
512 @require('Manage DMD')
514 """
515 Delete a data source.
516
517 @type uid: string
518 @param uid: Unique ID of the data source to delete
519 @rtype: DirectResponse
520 @return: Success message
521 """
522 facade = self._getFacade()
523 facade.deleteDataSource(uid)
524 audit('UI.DataSource.Delete', uid)
525 return DirectResponse.succeed()
526
527 @require('Manage DMD')
529 """
530 Delete a data point.
531
532 @type uid: string
533 @param uid: Unique ID of the data point to delete
534 @rtype: DirectResponse
535 @return: Success message
536 """
537 facade = self._getFacade()
538 facade.deleteDataPoint(uid)
539 audit('UI.DataPoint.Delete', uid)
540 return DirectResponse.succeed()
541
542 @require('Manage DMD')
544 """
545 Delete a graph definition.
546
547 @type uid: string
548 @param uid: Unique ID of the graph definition to delete
549 @rtype: DirectResponse
550 @return: Success message
551 """
552 facade = self._getFacade()
553 facade.deleteGraphDefinition(uid)
554 audit('UI.GraphDefinition.Delete', uid)
555 return DirectResponse.succeed()
556
557 @require('Manage DMD')
559 """
560 Delete a graph point.
561
562 @type uid: string
563 @param uid: Unique ID of the graph point to delete
564 @rtype: DirectResponse
565 @return: Success message
566 """
567 facade = self._getFacade()
568 facade.deleteGraphPoint(uid)
569 audit('UI.GraphPoint.Remove', uid)
570 return DirectResponse.succeed()
571
573 """
574 Get a list of graph points for a graph definition.
575
576 @type uid: string
577 @param uid: Unique ID of a graph definition
578 @rtype: DirectResponse
579 @return: B{Properties}:
580 - data: ([dictionary]) List of objects representing graph points
581 """
582 facade = self._getFacade()
583 graphPoints = facade.getGraphPoints(uid)
584 return DirectResponse.succeed(data=Zuul.marshal(graphPoints))
585
587 """
588 Get the properties of an object.
589
590 @type uid: string
591 @param uid: Unique identifier of an object
592 @rtype: DirectResponse
593 @return: B{Properties}
594 - data: (dictionary) Object properties
595 - form: (dictionary) Object representing an ExtJS form for the object
596 """
597 facade = self._getFacade()
598 info = facade.getInfo(uid)
599 form = IFormBuilder(info).render(fieldsets=False)
600 return DirectResponse(success=True, data=Zuul.marshal(info), form=form)
601
602 @require('Manage DMD')
604 """
605 Add a threshold to a graph definition.
606
607 @type graphUid: string
608 @param graphUid: Unique ID of the graph definition to add threshold to
609 @type thresholdUid: string
610 @param thresholdUid: Unique ID of the threshold to add
611 @rtype: DirectResponse
612 @return: Success message
613 """
614 facade = self._getFacade()
615 facade.addThresholdToGraph(graphUid, thresholdUid)
616 audit('UI.Graph.AddThreshold', graphUid, thresholdclass=thresholdUid)
617 return DirectResponse.succeed()
618
619 @require('Manage DMD')
621 """
622 Add a custom graph point to a graph definition.
623
624 @type graphUid: string
625 @param graphUid: Unique ID of the graph definition to add graph point to
626 @type customId: string
627 @param customId: ID of the new custom graph point
628 @type customType: string
629 @param customType: Type of the new graph point. From getGraphInstructionTypes()
630 @rtype: DirectResponse
631 @return: Success message
632 """
633 facade = self._getFacade()
634 facade.addCustomToGraph(graphUid, customId, customType)
635 audit('UI.Graph.AddCustomGraphPoint', graphUid, custom=customId)
636 return DirectResponse.succeed()
637
639 """
640 Get a list of available instruction types for graph points.
641
642 @type query: string
643 @param query: not used
644 @rtype: DirectResponse
645 @return: B{Properties}:
646 - data: ([dictionary]) List of objects representing instruction types
647 """
648 facade = self._getFacade()
649 types = facade.getGraphInstructionTypes()
650 return DirectResponse.succeed(data=Zuul.marshal(types))
651
652 @require('Manage DMD')
654 """
655 Sets the sequence of graph points in a graph definition.
656
657 @type uids: [string]
658 @param uids: List of graph point UID's in desired order
659 @rtype: DirectResponse
660 @return: Success message
661 """
662 facade = self._getFacade()
663 facade.setGraphPointSequence(uids)
664
665
666 audit('UI.GraphDefinition.SetGraphPointSequence', sequence=uids)
667 return DirectResponse.succeed()
668
670 """
671 Get a graph definition.
672
673 @type uid: string
674 @param uid: Unique ID of the graph definition to retrieve
675 @rtype: DirectResponse
676 @return: B{Properties}:
677 - data: (dictionary) Object representing a graph definition
678 """
679 facade = self._getFacade()
680 graphDef = facade.getGraphDefinition(uid)
681 return DirectResponse.succeed(data=Zuul.marshal(graphDef))
682
683 @require('Manage DMD')
685 """
686 Set attributes on an graph definition.
687 This method accepts any keyword argument for the property that you wish
688 to set. Properties are enumerated via getGraphDefinition(). The only
689 required property is "uid".
690
691 @type uid: string
692 @keyword uid: Unique identifier of an object
693 @rtype: DirectResponse
694 @return: B{Properties}:
695 - data: (dictionary) The modified object
696 """
697 uid = data['uid']
698 del data['uid']
699 for int_attr in ('miny', 'maxy'):
700 try:
701 x = int(data[int_attr])
702 except (ValueError, KeyError):
703 x = -1
704 data[int_attr] = x
705 obj = self._getFacade()._getObject(uid)
706 oldData = self._getInfoData(obj, data)
707 self._getFacade().setInfo(uid, data)
708 newData = self._getInfoData(obj, data)
709 audit(['UI', getDisplayType(obj), 'Edit'], data_=newData, oldData_=oldData,
710 skipFields_=('newId',))
711 return DirectResponse.succeed()
712
713 @require('Manage DMD')
715 """
716 Sets the sequence of graph definitions.
717
718 @type uids: [string]
719 @param uids: List of graph definition UID's in desired order
720 @rtype: DirectResponse
721 @return: Success message
722 """
723 facade = self._getFacade()
724 facade._setGraphDefinitionSequence(uids)
725
726
727 audit('UI.Template.SetGraphDefinitionSequence', sequence=uids)
728 return DirectResponse.succeed()
729
730 @require('Manage DMD')
735