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.Sites import Site, Sites
6 from PLC.Persons import Person, Persons
7 from PLC.Nodes import Node, Nodes
8 from PLC.PCUs import PCU, PCUs
9 from PLC.Auth import Auth
11 class GetPCUs(Method):
13 Returns an array of structs containing details about power control
14 units (PCUs). If pcu_filter is specified and is an array of PCU
15 identifiers, or a struct of PCU attributes, only PCUs matching the
16 filter will be returned. If return_fields is specified, only the
17 specified details will be returned.
19 Admin may query all PCUs. Non-admins may only query the PCUs at
23 roles = ['admin', 'pi', 'tech', 'node']
27 Mixed([PCU.fields['pcu_id']],
29 Parameter([str], "List of fields to return", nullok = True)
32 returns = [PCU.fields]
34 def call(self, auth, pcu_filter = None, return_fields = None):
36 if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']):
37 # Return only the PCUs at our site
40 if isinstance(self.caller, Person):
41 site_ids = self.caller['site_ids']
42 elif isinstance(self.caller, Node):
43 site_ids = [self.caller['site_id']]
45 for site in Sites(self.api, site_ids):
46 valid_pcu_ids += site['pcu_ids']
51 if pcu_filter is None:
52 pcu_filter = valid_pcu_ids
54 # Must query at least slice_id (see below)
55 if return_fields is not None and 'pcu_id' not in return_fields:
56 return_fields.append('pcu_id')
61 pcus = PCUs(self.api, pcu_filter, return_fields)
63 # Filter out PCUs that are not viewable
64 if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']):
65 pcus = [pcu for pcu in pcus if pcu['pcu_id'] in valid_pcu_ids]
67 # Remove pcu_id if not specified