3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Filter import Filter
7 from PLC.Sites import Site, Sites
8 from PLC.Persons import Person, Persons
9 from PLC.Nodes import Node, Nodes
10 from PLC.PCUs import PCU, PCUs
11 from PLC.Auth import Auth
13 class GetPCUs(Method):
15 Returns an array of structs containing details about power control
16 units (PCUs). If pcu_filter is specified and is an array of PCU
17 identifiers, or a struct of PCU attributes, only PCUs matching the
18 filter will be returned. If return_fields is specified, only the
19 specified details will be returned.
21 Admin may query all PCUs. Non-admins may only query the PCUs at
25 roles = ['admin', 'pi', 'tech', 'node']
29 Mixed([PCU.fields['pcu_id']],
31 Parameter([str], "List of fields to return", nullok = True)
34 returns = [PCU.fields]
36 def call(self, auth, pcu_filter = None, return_fields = None):
38 if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']):
39 # Return only the PCUs at our site
42 if isinstance(self.caller, Person):
43 site_ids = self.caller['site_ids']
44 elif isinstance(self.caller, Node):
45 site_ids = [self.caller['site_id']]
47 for site in Sites(self.api, site_ids):
48 valid_pcu_ids += site['pcu_ids']
53 if pcu_filter is None:
54 pcu_filter = valid_pcu_ids
56 # Must query at least slice_id (see below)
57 if return_fields is not None and 'pcu_id' not in return_fields:
58 return_fields.append('pcu_id')
63 pcus = PCUs(self.api, pcu_filter, return_fields)
65 # Filter out PCUs that are not viewable
66 if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']):
67 pcus = filter(lambda pcu: pcu['pcu_id'] in valid_pcu_ids, pcus)
69 # Remove pcu_id if not specified