05a011a6cd72aa727f7da620bcdd5de127bb708c
[plcapi.git] / PLC / Methods / GetEvents.py
1 import os
2
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Events import Event, Events
7 from PLC.Auth import PasswordAuth
8
9 class GetEvents(Method):
10     """
11     Return an array of dictionaries containing details about the
12     specified events.
13
14     if object_ids is specified, and object_types must be specified
15     with only 1 object type
16     """
17
18     roles = ['admin', 'pi', 'user', 'tech']
19
20     accepts = [
21         PasswordAuth(),
22         [Event.fields['event_id']],
23         [Event.fields['person_id']],
24         Event.fields['object_ids'],
25         [Event.fields['event_type']],           
26         [Event.fields['object_type']],
27         [Event.fields['fault_code']]
28         ]
29
30     returns = [Event.fields]
31
32     def __init__(self, *args, **kwds):
33         Method.__init__(self, *args, **kwds)
34         # Update documentation with list of default fields returned
35         self.__doc__ += os.linesep.join(Event.fields.keys())
36
37     def call(self, auth, event_id_list = None, person_id_list = None, event_type_list = None, object_type_list = None, object_id_list = None, fault_code_list = None):
38         
39         # Authenticated function
40         assert self.caller is not None
41
42         # filter out invalid event types
43         if event_type_list:
44                 valid_event_types = ['Add', 'AddTo', 'Get', 'Update', 'Delete', \
45                                      'DeleteFrom', 'Unknown']
46                 if filter(lambda field: field not in valid_event_types, event_type_list):
47                         raise PLCInvalidArgument, "Invalid event type. Must be in %s" % \
48                                                   valid_event_types
49         
50         # filter out invalid object types
51         if object_type_list:
52                 valid_object_types = ['AddreessType', 'Address', 'BootState', 'ConfFile', \
53                                       'KeyType', 'Key', 'NetworkType', 'NodeGroup',\
54                                       'NodeNetwork', 'Node', 'PCU', 'Perons', 'Site', \
55                                       'SliceAttributeType', 'SliceAttribute', 'Slice', 'Unknown']
56                 if filter(lambda field: field not in valid_object_types, object_type_list):
57                         raise PLCInvalidArgument, "Invalid object type. Must be in %s" % \
58                                                   valid_object_types
59         
60         # if object ids are specified only 1 object type can be specified
61         if object_id_list:
62                 if not object_type_list:
63                         raise PLCInvalidArgument, "Object type must be specified"
64                 elif len(object_type_list) > 1:
65                         raise PLCInvalidArgument, "Cannot specify multiple object types when object_ids are specified"
66         
67         # Get node information
68         events = Events(self.api, event_id_list, person_id_list, event_type_list, \
69                         object_type_list, object_id_list, fault_code_list).values()
70
71         # turn each node into a real dict.
72         events = [dict(event) for event in events]
73                     
74         return events