- fixed return documentation
[plcapi.git] / PLC / Methods / AdmGetSiteNodes.py
1 import os
2
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Sites import Site, Sites
7 from PLC.Nodes import Node, Nodes
8 from PLC.Auth import PasswordAuth
9
10 class AdmGetSiteNodes(Method):
11     """
12     Return a dictionary containing a list of node_ids for the  sites specified.
13
14     Admins may retrieve details about all nodes on a site by not specifying
15     site_id_or_name or by specifying an empty list. Users and
16     techs may only retrieve details about themselves. PIs may retrieve
17     details about themselves and others at their sites.
18
19     """
20
21     roles = ['admin', 'pi', 'user', 'tech']
22
23     accepts = [
24         PasswordAuth(),
25         [Mixed(Site.fields['site_id'],
26                Site.fields['name'])],
27         Parameter([str], 'List of fields to return')
28         ]
29        
30     returns={Site.fields['site_id']: [Site.all_fields['node_ids']]}
31     
32     def __init__(self, *args, **kwds):
33         Method.__init__(self, *args, **kwds)
34         # Update documentation with list of default fields returned
35         self.__doc__ += os.linesep.join(Site.default_fields.keys())
36
37     def call(self, auth, site_id_or_name_list = None):
38         # Authenticated function
39         assert self.caller is not None
40
41         #convert site_id_or_name_list to 'None' if is [] (empty list)
42         if site_id_or_name_list is not None and site_id_or_name_list == []:
43                 site_id_or_name_list = None
44
45         # Get site information
46         sites = Sites(self.api, site_id_or_name_list, ['node_ids']).values()    
47
48         # make sure sites are found
49         if not sites:
50                 raise PLCInvalidArgument, "No such site"
51         elif site_id_or_name_list is None:
52                 pass
53         elif not len(sites) == len(site_id_or_name_list):
54                 raise PLCInvalidArgument, "at least one site_id is invalid"
55         
56         #Convert list of sites dictionaries into a dictionary of sites -> sites:[node_ids]
57         site_nodes = {}
58         for site in sites:
59                 #filter out deleted nodes
60                 nodes = Nodes(self.api, site['node_ids'])
61                 #creat valid node list dictionary
62                 site_nodes[str(site['site_id'])] = nodes.keys()
63                 
64         return site_nodes