X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FGetPCUs.py;h=3280070374e817c197689ab5e5731a95f6f8e0dd;hb=2b8aadbf673abb2cd3ac27ef0d2e3037f6f0779b;hp=60aabbb1873b9e3bddc8f2181ec0ea851c869311;hpb=dba7fc1392844cc564be1a6ee0ece7686978aa96;p=plcapi.git diff --git a/PLC/Methods/GetPCUs.py b/PLC/Methods/GetPCUs.py index 60aabbb..3280070 100644 --- a/PLC/Methods/GetPCUs.py +++ b/PLC/Methods/GetPCUs.py @@ -1,39 +1,72 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter +from PLC.Sites import Site, Sites +from PLC.Persons import Person, Persons +from PLC.Nodes import Node, Nodes from PLC.PCUs import PCU, PCUs -from PLC.Auth import PasswordAuth +from PLC.Auth import Auth class GetPCUs(Method): """ - Return an array of structs containing details about PCUs. If - pcu_id_list is specified, only the specified PCUs will be queried. + Returns an array of structs containing details about power control + units (PCUs). If pcu_filter is specified and is an array of PCU + identifiers, or a struct of PCU attributes, only PCUs matching the + filter will be returned. If return_fields is specified, only the + specified details will be returned. Admin may query all PCUs. Non-admins may only query the PCUs at their sites. """ - roles = ['admin', 'pi', 'tech'] + roles = ['admin', 'pi', 'tech', 'node'] accepts = [ - PasswordAuth(), - [PCU.fields['pcu_id']] + Auth(), + Mixed([PCU.fields['pcu_id']], + Filter(PCU.fields)), + Parameter([str], "List of fields to return", nullok = True) ] returns = [PCU.fields] - def call(self, auth, pcu_ids = None): - # If we are not admin, make sure to only return our own PCUs - if 'admin' not in self.caller['roles']: - if not pcu_ids: - pcu_ids = [] - sites = Sites(self.api, self.caller['site_ids']).values() - for site in sites: - pcu_ids = set(pcu_ids).union(site['pcu_ids']) + def call(self, auth, pcu_filter = None, return_fields = None): + # If we are not admin + if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']): + # Return only the PCUs at our site + valid_pcu_ids = [] - pcus = PCUs(self.api, pcu_ids).values() + if isinstance(self.caller, Person): + site_ids = self.caller['site_ids'] + elif isinstance(self.caller, Node): + site_ids = [self.caller['site_id']] - # turn each pcu into a real dict - pcus = [dict(pcu.items()) for pcu in pcus] + for site in Sites(self.api, site_ids): + valid_pcu_ids += site['pcu_ids'] - return pcus + if not valid_pcu_ids: + return [] + + if pcu_filter is None: + pcu_filter = valid_pcu_ids + + # Must query at least slice_id (see below) + if return_fields is not None and 'pcu_id' not in return_fields: + return_fields.append('pcu_id') + added_fields = True + else: + added_fields = False + + pcus = PCUs(self.api, pcu_filter, return_fields) + + # Filter out PCUs that are not viewable + if not (isinstance(self.caller, Person) and 'admin' in self.caller['roles']): + pcus = filter(lambda pcu: pcu['pcu_id'] in valid_pcu_ids, pcus) + + # Remove pcu_id if not specified + if added_fields: + for pcu in pcus: + del pcu['pcu_id'] + + return pcus