use Role
[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                 event_type_list = [event_type.title() for event_type in event_type_list]
45                 valid_event_types = ['Add', 'Get', 'Update', 'Delete', '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                 object_type_list = [object_type.title() for object_type in object_type_list]
53                 valid_object_types = ['Person', 'Site', 'Node', 'Slice', 'Address', \
54                                       'Attribute', 'Key', 'Nodegroup', 'Unknown']
55                 if filter(lambda field: field not in valid_object_types, object_type_list):
56                         raise PLCInvalidArgument, "Invalid object type. Must be in %s" % \
57                                                   valid_object_types
58         
59         # if object ids are specified only 1 object type can be specified
60         if object_id_list:
61                 if not object_type_list:
62                         raise PLCInvalidArgument, "Object type must be specified"
63                 elif len(object_type_list) > 1:
64                         raise PLCInvalidArgument, "Cannot specify multiple object types when object_ids are specified"
65         
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).values()
70
71         # turn each node into a real dict.
72         events = [dict(event) for event in events]
73                     
74         return events