- fix documentation
[plcapi.git] / PLC / Methods / AdmGetNodes.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.Nodes import Node, Nodes
7 from PLC.Auth import PasswordAuth
8
9 class AdmGetNodes(Method):
10     """
11     Return an array of dictionaries containing details about the
12     specified nodes.
13
14     If return_fields is specified, only the specified fields will be
15     returned. Only admins may retrieve certain fields. Otherwise, the
16     default set of fields returned is:
17
18     """
19
20     roles = ['admin', 'pi', 'user', 'tech']
21
22     accepts = [
23         PasswordAuth(),
24         [Mixed(Node.fields['node_id'],
25                Node.fields['hostname'])],
26         Parameter([str], 'List of fields to return')
27         ]
28
29     # Filter out hidden fields
30     can_return = lambda (field, value): field not in ['deleted']
31     return_fields = dict(filter(can_return, Node.fields.items()))
32     returns = [return_fields]
33
34     def __init__(self, *args, **kwds):
35         Method.__init__(self, *args, **kwds)
36         # Update documentation with list of default fields returned
37         self.__doc__ += os.linesep.join(self.return_fields.keys())
38
39     def call(self, auth, node_id_or_hostname_list = None, return_fields = None):
40         # Authenticated function
41         assert self.caller is not None
42
43         valid_fields = dict(self.return_fields)
44
45         # Remove admin only fields
46         if 'admin' not in self.caller['roles']:
47             for key in ['boot_nonce', 'key', 'session', 'root_person_ids']:
48                 del valid_fields[key]
49
50         # Make sure that only valid fields are specified
51         if return_fields is None:
52             return_fields = valid_fields
53         elif filter(lambda field: field not in valid_fields, return_fields):
54             raise PLCInvalidArgument, "Invalid return field specified"
55
56         # Get node information
57         nodes = Nodes(self.api, node_id_or_hostname_list, return_fields).values()
58
59         # Filter out undesired or None fields (XML-RPC cannot marshal
60         # None) and turn each node into a real dict.
61         valid_return_fields_only = lambda (key, value): \
62                                    key in return_fields and value is not None
63         nodes = [dict(filter(valid_return_fields_only, node.items())) \
64                  for node in nodes]
65                     
66         return nodes