From 51577ccbfa01fffba04b84e502a35f6a3915b68b Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Wed, 8 Nov 2006 23:11:49 +0000 Subject: [PATCH] allow filters to be specified in most Get() calls --- PLC/Methods/GetAddressTypes.py | 19 ++++++++++--------- PLC/Methods/GetAddresses.py | 15 +++++++++------ PLC/Methods/GetBootStates.py | 2 +- PLC/Methods/GetConfFiles.py | 16 ++++++++++------ PLC/Methods/GetForeignNodes.py | 27 ++++++++++++++++----------- PLC/Methods/GetKeyTypes.py | 2 +- PLC/Methods/GetKeys.py | 4 ++-- PLC/Methods/GetNodeGroups.py | 17 ++++++++++------- PLC/Methods/GetNodeNetworks.py | 17 ++++++++++------- PLC/Methods/GetNodes.py | 17 ++++++++++------- PLC/Methods/GetPCUs.py | 26 +++++++++++++++++++------- PLC/Methods/GetPeers.py | 21 +++++++++++++-------- PLC/Methods/GetPersons.py | 22 ++++++++++++---------- PLC/Methods/GetSites.py | 10 ++++++---- PLC/Methods/GetSliceAttributeTypes.py | 10 ++++++---- PLC/Methods/GetSliceAttributes.py | 23 +++++++++++++++++------ PLC/Methods/GetSlices.py | 21 ++++++++++++--------- 17 files changed, 164 insertions(+), 105 deletions(-) diff --git a/PLC/Methods/GetAddressTypes.py b/PLC/Methods/GetAddressTypes.py index df3a1b6..419027e 100644 --- a/PLC/Methods/GetAddressTypes.py +++ b/PLC/Methods/GetAddressTypes.py @@ -1,27 +1,28 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.AddressTypes import AddressType, AddressTypes from PLC.Auth import Auth class GetAddressTypes(Method): """ - Get an array of structs containing details about valid address - types. If address_type_id_or_name_list is specified, only the - specified address types will be queried. + Returns an array of structs containing details about address + types. If address_type_filter is specified and is an array of + address type identifiers, or a struct of address type attributes, + only address types matching the filter will be returned. """ roles = ['admin', 'pi', 'user', 'tech'] accepts = [ Auth(), - [Mixed(AddressType.fields['address_type_id'], - AddressType.fields['name'])] + Mixed([Mixed(AddressType.fields['address_type_id'], + AddressType.fields['name'])], + Filter(AddressType.fields)) ] returns = [AddressType.fields] - def call(self, auth, address_type_id_or_name_list = None): - address_types = AddressTypes(self.api, address_type_id_or_name_list).values() - - return address_types + def call(self, auth, address_type_filter = None): + return AddressTypes(self.api, address_type_filter).values() diff --git a/PLC/Methods/GetAddresses.py b/PLC/Methods/GetAddresses.py index 75b8ad6..567cd98 100644 --- a/PLC/Methods/GetAddresses.py +++ b/PLC/Methods/GetAddresses.py @@ -1,24 +1,27 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Addresses import Address, Addresses from PLC.Auth import Auth class GetAddresses(Method): """ - Get an array of structs containing details about addresses. If - address_id_list is specified, only the specified addresses will be - queried. + Returns an array of structs containing details about addresses. If + address_filter is specified and is an array of address + identifiers, or a struct of address attributes, only addresses + matching the filter will be returned. """ roles = ['admin', 'pi', 'user', 'tech'] accepts = [ Auth(), - [Address.fields['address_id']], + Mixed([Address.fields['address_id']], + Filter(Address.fields)) ] returns = [Address.fields] - def call(self, auth, address_id_list = None): - return Addresses(self.api, address_id_list).values() + def call(self, auth, address_filter = None): + return Addresses(self.api, address_filter).values() diff --git a/PLC/Methods/GetBootStates.py b/PLC/Methods/GetBootStates.py index 71e2cd4..25c6c7d 100644 --- a/PLC/Methods/GetBootStates.py +++ b/PLC/Methods/GetBootStates.py @@ -6,7 +6,7 @@ from PLC.Auth import Auth class GetBootStates(Method): """ - Returns a list of all valid node boot states. + Returns an array of all valid node boot states. """ roles = ['admin', 'pi', 'user', 'tech'] diff --git a/PLC/Methods/GetConfFiles.py b/PLC/Methods/GetConfFiles.py index fe3cb79..d12365e 100644 --- a/PLC/Methods/GetConfFiles.py +++ b/PLC/Methods/GetConfFiles.py @@ -1,24 +1,28 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.ConfFiles import ConfFile, ConfFiles from PLC.Auth import Auth class GetConfFiles(Method): """ - Returns an array of structs containing details about node - configuration files. If conf_file_ids is specified, only the - specified configuration files will be queried. + Returns an array of structs containing details about configuration + files. If conf_file_filter is specified and is an array of + configuration file identifiers, or a struct of configuration file + attributes, only configuration files matching the filter will be + returned. """ roles = ['admin'] accepts = [ Auth(), - [ConfFile.fields['conf_file_id']] + Mixed([ConfFile.fields['conf_file_id']], + Filter(ConfFile.fields)) ] returns = [ConfFile.fields] - def call(self, auth, conf_file_ids = None): - return ConfFiles(self.api, conf_file_ids).values() + def call(self, auth, conf_file_filter = None): + return ConfFiles(self.api, conf_file_filter).values() diff --git a/PLC/Methods/GetForeignNodes.py b/PLC/Methods/GetForeignNodes.py index e9c2321..9676da4 100644 --- a/PLC/Methods/GetForeignNodes.py +++ b/PLC/Methods/GetForeignNodes.py @@ -5,25 +5,30 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Auth import Auth from PLC.ForeignNodes import ForeignNode, ForeignNodes -class GetForeignNodes (Method): +class GetForeignNodes(Method): """ - returns information on foreign nodes + Returns an array of structs containing details about foreign + nodes. If foreign_node_filter is specified and is an array of + foreign node identifiers or hostnames, or a struct of foreign node + attributes, only foreign nodes matching the filter will be + returned. """ roles = ['admin'] - accepts = [ Auth(), - [ Mixed(ForeignNode.fields['node_id'], - ForeignNode.fields['hostname'])] - ] + accepts = [ + Auth(), + Mixed([Mixed(ForeignNode.fields['node_id'], + ForeignNode.fields['hostname'])], + Filter(ForeignNode.fields)) + ] - returns = [ ForeignNode.fields] + returns = [ForeignNode.fields] - def call (self, auth, foreign_id_or_hostname_list = None): - - return ForeignNodes (self.api, foreign_id_or_hostname_list).values() - + def call(self, auth, foreign_node_filter = None): + return ForeignNodes(self.api, foreign_node_filter).values() diff --git a/PLC/Methods/GetKeyTypes.py b/PLC/Methods/GetKeyTypes.py index 50828a0..f005b99 100644 --- a/PLC/Methods/GetKeyTypes.py +++ b/PLC/Methods/GetKeyTypes.py @@ -6,7 +6,7 @@ from PLC.Auth import Auth class GetKeyTypes(Method): """ - Returns a list of all valid key types. + Returns an array of all valid key types. """ roles = ['admin', 'pi', 'user', 'tech'] diff --git a/PLC/Methods/GetKeys.py b/PLC/Methods/GetKeys.py index 910707b..9f8f7f4 100644 --- a/PLC/Methods/GetKeys.py +++ b/PLC/Methods/GetKeys.py @@ -6,8 +6,8 @@ from PLC.Auth import Auth class GetKeys(Method): """ - Return an array of structs containing details about keys. If - key_id_list is specified, only the specified keys will be queried. + Returns an array of structs containing details about keys. If + key_ids is specified, only the specified keys will be queried. Admin may query all keys. Non-admins may only query their own keys. diff --git a/PLC/Methods/GetNodeGroups.py b/PLC/Methods/GetNodeGroups.py index 8601a05..c41b8b9 100644 --- a/PLC/Methods/GetNodeGroups.py +++ b/PLC/Methods/GetNodeGroups.py @@ -1,25 +1,28 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Auth import Auth from PLC.NodeGroups import NodeGroup, NodeGroups class GetNodeGroups(Method): """ - Returns an array of structs containing details about all node - groups. If nodegroup_id_or_name_list is specified, only the - specified node groups will be queried. + Returns an array of structs containing details about node groups. + If nodegroup_filter is specified and is an array of node group + identifiers or names, or a struct of node group attributes, only + node groups matching the filter will be returned. """ roles = ['admin', 'pi', 'user', 'tech'] accepts = [ Auth(), - [Mixed(NodeGroup.fields['nodegroup_id'], - NodeGroup.fields['name'])] + Mixed([Mixed(NodeGroup.fields['nodegroup_id'], + NodeGroup.fields['name'])], + Filter(NodeGroup.fields)) ] returns = [NodeGroup.fields] - def call(self, auth, nodegroup_id_or_name_list = None): - return NodeGroups(self.api, nodegroup_id_or_name_list).values() + def call(self, auth, nodegroup_filter = None): + return NodeGroups(self.api, nodegroup_filter).values() diff --git a/PLC/Methods/GetNodeNetworks.py b/PLC/Methods/GetNodeNetworks.py index e20487f..e2a8c85 100644 --- a/PLC/Methods/GetNodeNetworks.py +++ b/PLC/Methods/GetNodeNetworks.py @@ -1,25 +1,28 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.NodeNetworks import NodeNetwork, NodeNetworks from PLC.Auth import Auth class GetNodeNetworks(Method): """ - Return an array of structs contain details about node network - interfaces. If nodenetwork_id_or_ip_list is specified, only - the specified node network interfaces will be queried. + Returns an array of structs containing details about node network + interfacess. If nodenetworks_filter is specified and is an array + of node network identifiers, or a struct of node network + attributes, only node network interfaces matching the filter will + be returned. """ roles = ['admin', 'pi', 'user', 'tech'] accepts = [ Auth(), - [Mixed(NodeNetwork.fields['nodenetwork_id'], - NodeNetwork.fields['ip'])] + Mixed([NodeNetwork.fields['nodenetwork_id']], + Filter(NodeNetwork.fields)) ] returns = [NodeNetwork.fields] - def call(self, auth, nodenetwork_id_or_ip_list = None): - return NodeNetworks(self.api, nodenetwork_id_or_ip_list).values() + def call(self, auth, nodenetwork_filter = None): + return NodeNetworks(self.api, nodenetwork_filter).values() diff --git a/PLC/Methods/GetNodes.py b/PLC/Methods/GetNodes.py index d552bcd..a2c2566 100644 --- a/PLC/Methods/GetNodes.py +++ b/PLC/Methods/GetNodes.py @@ -1,14 +1,16 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Nodes import Node, Nodes from PLC.Auth import Auth class GetNodes(Method): """ - Return an array of structs containing details about nodes. If - node_id_or_hostname_list is specified, only the specified nodes - will be queried. + Returns an array of structs containing details about nodes. If + node_filter is specified and is an array of node identifiers or + hostnames, or a struct of node attributes, only nodes matching the + filter will be returned. Some fields may only be viewed by admins. """ @@ -17,15 +19,16 @@ class GetNodes(Method): accepts = [ Auth(), - [Mixed(Node.fields['node_id'], - Node.fields['hostname'])], + Mixed([Mixed(Node.fields['node_id'], + Node.fields['hostname'])], + Filter(Node.fields)) ] returns = [Node.fields] - def call(self, auth, node_id_or_hostname_list = None): + def call(self, auth, node_filter = None): # Get node information - nodes = Nodes(self.api, node_id_or_hostname_list).values() + nodes = Nodes(self.api, node_filter).values() # Remove admin only fields if 'admin' not in self.caller['roles']: diff --git a/PLC/Methods/GetPCUs.py b/PLC/Methods/GetPCUs.py index 881a85f..dbf371e 100644 --- a/PLC/Methods/GetPCUs.py +++ b/PLC/Methods/GetPCUs.py @@ -1,13 +1,16 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.PCUs import PCU, PCUs 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. Admin may query all PCUs. Non-admins may only query the PCUs at their sites. @@ -17,12 +20,13 @@ class GetPCUs(Method): accepts = [ Auth(), - [PCU.fields['pcu_id']] + Mixed([PCU.fields['pcu_id']], + Filter(PCU.fields)) ] returns = [PCU.fields] - def call(self, auth, pcu_ids = None): + def call(self, auth, pcu_filter = None): # If we are not admin, make sure to only return our own PCUs if 'admin' not in self.caller['roles']: # Get list of PCUs that we are able to view @@ -32,8 +36,16 @@ class GetPCUs(Method): for site in sites: valid_pcu_ids += site['pcu_ids'] - pcu_ids = set(pcu_ids).intersection(valid_pcu_ids) - if not pcu_ids: + if not valid_pcu_ids: return [] - return PCUs(self.api, pcu_ids).values() + if pcu_filter is None: + pcu_filter = valid_pcu_ids + + pcus = PCUs(self.api, pcu_filter).values() + + # Filter out PCUs that are not viewable + if 'admin' not in self.caller['roles']: + pcus = filter(lambda pcu: pcu['pcu_id'] in valid_pcu_ids, pcus) + + return pcus diff --git a/PLC/Methods/GetPeers.py b/PLC/Methods/GetPeers.py index cc85ef1..e4fd459 100644 --- a/PLC/Methods/GetPeers.py +++ b/PLC/Methods/GetPeers.py @@ -5,24 +5,29 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Auth import Auth from PLC.Peers import Peer, Peers class GetPeers (Method): """ - returns information on known peers + Returns an array of structs containing details about peers. If + person_filter is specified and is an array of peer identifiers or + peer names, or a struct of peer attributes, only peers matching + the filter will be returned. """ roles = ['admin'] - accepts = [Auth(), - [Mixed(Peer.fields['peer_id'], - Peer.fields['peername'])], - ] + accepts = [ + Auth(), + Mixed([Mixed(Peer.fields['peer_id'], + Peer.fields['peername'])], + Filter(Peer.fields)) + ] returns = [Peer.fields] - def call (self, auth, peer_id_or_peername_list = None): - - return Peers (self.api, peer_id_or_peername_list).values() + def call (self, auth, peer_filter = None): + return Peers(self.api, peer_filter).values() diff --git a/PLC/Methods/GetPersons.py b/PLC/Methods/GetPersons.py index ecac429..d0f4b2d 100644 --- a/PLC/Methods/GetPersons.py +++ b/PLC/Methods/GetPersons.py @@ -1,14 +1,16 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Persons import Person, Persons from PLC.Auth import Auth class GetPersons(Method): """ - Return an array of structs containing details about accounts. If - person_id_or_email_list is specified, only the specified accounts - will be queried. + Returns an array of structs containing details about users. If + person_filter is specified and is an array of user identifiers or + usernames, or a struct of user attributes, only users matching the + filter will be returned. Users and techs may only retrieve details about themselves. PIs may retrieve details about themselves and others at their @@ -19,9 +21,9 @@ class GetPersons(Method): accepts = [ Auth(), - [Mixed(Person.fields['person_id'], - Person.fields['email'])], - Parameter([str], 'List of fields to return') + Mixed([Mixed(Person.fields['person_id'], + Person.fields['email'])], + Filter(Person.fields)) ] # Filter out password field @@ -29,7 +31,7 @@ class GetPersons(Method): return_fields = dict(filter(can_return, Person.fields.items())) returns = [return_fields] - def call(self, auth, person_id_or_email_list = None): + def call(self, auth, person_filter = None): # If we are not admin, make sure to only return viewable accounts if 'admin' not in self.caller['roles']: # Get accounts that we are able to view @@ -42,10 +44,10 @@ class GetPersons(Method): if not valid_person_ids: return [] - if not person_id_or_email_list: - person_id_or_email_list = valid_person_ids + if person_filter is None: + person_filter = valid_person_ids - persons = Persons(self.api, person_id_or_email_list).values() + persons = Persons(self.api, person_filter).values() # Filter out accounts that are not viewable if 'admin' not in self.caller['roles']: diff --git a/PLC/Methods/GetSites.py b/PLC/Methods/GetSites.py index 0f0257a..885e280 100644 --- a/PLC/Methods/GetSites.py +++ b/PLC/Methods/GetSites.py @@ -1,5 +1,6 @@ from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Auth import Auth from PLC.Sites import Site, Sites @@ -14,8 +15,9 @@ class GetSites(Method): accepts = [ Auth(), - [Mixed(Site.fields['site_id'], - Site.fields['login_base'])] + Mixed([Mixed(Site.fields['site_id'], + Site.fields['login_base'])], + Filter(Site.fields)) ] returns = [Site.fields] @@ -24,5 +26,5 @@ class GetSites(Method): object_type = 'Site' object_ids = [] - def call(self, auth, site_id_or_login_base_list = None): - return Sites(self.api, site_id_or_login_base_list).values() + def call(self, auth, site_filter = None): + return Sites(self.api, site_filter).values() diff --git a/PLC/Methods/GetSliceAttributeTypes.py b/PLC/Methods/GetSliceAttributeTypes.py index a0db661..0495523 100644 --- a/PLC/Methods/GetSliceAttributeTypes.py +++ b/PLC/Methods/GetSliceAttributeTypes.py @@ -1,5 +1,6 @@ from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Auth import Auth from PLC.SliceAttributeTypes import SliceAttributeType, SliceAttributeTypes @@ -14,11 +15,12 @@ class GetSliceAttributeTypes(Method): accepts = [ Auth(), - [Mixed(SliceAttributeType.fields['attribute_type_id'], - SliceAttributeType.fields['name'])], + Mixed([Mixed(SliceAttributeType.fields['attribute_type_id'], + SliceAttributeType.fields['name'])], + Filter(SliceAttributeType.fields)) ] returns = [SliceAttributeType.fields] - def call(self, auth, attribute_type_id_or_name_list = None): - return SliceAttributeTypes(self.api, attribute_type_id_or_name_list).values() + def call(self, auth, attribute_type_filter = None): + return SliceAttributeTypes(self.api, attribute_type_filter).values() diff --git a/PLC/Methods/GetSliceAttributes.py b/PLC/Methods/GetSliceAttributes.py index 9a37c93..af5deec 100644 --- a/PLC/Methods/GetSliceAttributes.py +++ b/PLC/Methods/GetSliceAttributes.py @@ -1,10 +1,10 @@ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.SliceAttributes import SliceAttribute, SliceAttributes from PLC.Sites import Site, Sites from PLC.Slices import Slice, Slices -from PLC.Nodes import Node, Nodes from PLC.Auth import Auth class GetSliceAttributes(Method): @@ -24,12 +24,13 @@ class GetSliceAttributes(Method): accepts = [ Auth(), - [SliceAttribute.fields['slice_attribute_id']], + Mixed([SliceAttribute.fields['slice_attribute_id']], + Filter(SliceAttribute.fields)) ] returns = [SliceAttribute.fields] - def call(self, auth, slice_attribute_ids = None): + def call(self, auth, slice_attribute_filter = None): # If we are not admin, make sure to only return our own slice # and sliver attributes. if 'admin' not in self.caller['roles']: @@ -49,8 +50,18 @@ class GetSliceAttributes(Method): for slice in slices: valid_slice_attribute_ids += slice['slice_attribute_ids'] - slice_attribute_ids = set(slice_attribute_ids).intersection(valid_slice_attribute_ids) - if not slice_attribute_ids: + if not valid_slice_attribute_ids: return [] - return SliceAttributes(self.api, slice_attribute_ids).values() + if slice_attribute_filter is None: + slice_attribute_filter = valid_slice_attribute_ids + + slice_attributes = SliceAttributes(self.api, slice_attribute_filter).values() + + # Filter out slice attributes that are not viewable + if 'admin' not in self.caller['roles']: + slice_attributes = filter(lambda slice_attribute: \ + slice_attribute['slice_attribute_id'] in valid_slice_attribute_ids, + slice_attributes) + + return slice_attributes diff --git a/PLC/Methods/GetSlices.py b/PLC/Methods/GetSlices.py index 6c870f2..478ff95 100644 --- a/PLC/Methods/GetSlices.py +++ b/PLC/Methods/GetSlices.py @@ -1,13 +1,15 @@ from PLC.Method import Method from PLC.Parameter import Parameter, Mixed +from PLC.Filter import Filter from PLC.Auth import Auth from PLC.Slices import Slice, Slices class GetSlices(Method): """ - Return an array of structs containing details about slices. If - slice_id_or_name_list is specified, only the specified slices will - be queried. + Returns an array of structs containing details about slices. If + slice_filter is specified and is an array of slice identifiers or + slice names, or a struct of slice attributes, only slices matching + the filter will be returned. Users may only query slices of which they are members. PIs may query any of the slices at their sites. Admins may query any @@ -20,13 +22,14 @@ class GetSlices(Method): accepts = [ Auth(), - [Mixed(Slice.fields['slice_id'], - Slice.fields['name'])] + Mixed([Mixed(Slice.fields['slice_id'], + Slice.fields['name'])], + Filter(Slice.fields)) ] returns = [Slice.fields] - def call(self, auth, slice_id_or_name_list = None): + def call(self, auth, slice_filter = None): # If we are not admin, make sure to return only viewable # slices. if 'admin' not in self.caller['roles']: @@ -40,10 +43,10 @@ class GetSlices(Method): if not valid_slice_ids: return [] - if not slice_id_or_name_list: - slice_id_or_name_list = valid_slice_ids + if slice_filter is None: + slice_filter = valid_slice_ids - slices = Slices(self.api, slice_id_or_name_list).values() + slices = Slices(self.api, slice_filter).values() # Filter out slices that are not viewable if 'admin' not in self.caller['roles']: -- 2.43.0