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