13bd7dc6841e2e961eab1e597c2f114087b7d4f2
[plcapi.git] / PLC / Methods / GetPCUs.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Filter import Filter
5 from PLC.PCUs import PCU, PCUs
6 from PLC.Auth import Auth
7
8 class GetPCUs(Method):
9     """
10     Returns an array of structs containing details about power control
11     units (PCUs). If pcu_filter is specified and is an array of PCU
12     identifiers, or a struct of PCU attributes, only PCUs matching the
13     filter will be returned. If return_fields is specified, only the
14     specified details will be returned.
15
16     Admin may query all PCUs. Non-admins may only query the PCUs at
17     their sites.
18     """
19
20     roles = ['admin', 'pi', 'tech']
21
22     accepts = [
23         Auth(),
24         Mixed([PCU.fields['pcu_id']],
25               Filter(PCU.fields)),
26         Parameter([str], "List of fields to return", nullok = True)
27         ]
28
29     returns = [PCU.fields]
30
31     def call(self, auth, pcu_filter = None, return_fields = None):
32         # If we are not admin, make sure to only return our own PCUs
33         if 'admin' not in self.caller['roles']:
34             # Get list of PCUs that we are able to view
35             valid_pcu_ids = []
36             if self.caller['site_ids']:
37                 sites = Sites(self.api, self.caller['site_ids'])
38                 for site in sites:
39                     valid_pcu_ids += site['pcu_ids']
40
41             if not valid_pcu_ids:
42                 return []
43
44             if pcu_filter is None:
45                 pcu_filter = valid_pcu_ids
46
47         pcus = PCUs(self.api, pcu_filter, return_fields)
48
49         # Filter out PCUs that are not viewable
50         if 'admin' not in self.caller['roles']:
51             pcus = filter(lambda pcu: pcu['pcu_id'] in valid_pcu_ids, pcus)
52
53         return pcus