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
9 class AdmGetNodes(Method):
11 Return an array of dictionaries containing details about the
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:
20 roles = ['admin', 'pi', 'user', 'tech']
24 [Mixed(Node.fields['node_id'],
25 Node.fields['hostname'])],
26 Parameter([str], 'List of fields to return')
29 returns = [Node.fields]
31 def __init__(self, *args, **kwds):
32 Method.__init__(self, *args, **kwds)
33 # Update documentation with list of default fields returned
34 self.__doc__ += os.linesep.join(Node.fields.keys())
36 def call(self, auth, node_id_or_hostname_list = None, return_fields = None):
37 # Authenticated function
38 assert self.caller is not None
40 valid_fields = Node.fields.keys()
42 # Remove admin only fields
43 if 'admin' not in self.caller['roles']:
44 for key in ['boot_nonce', 'key', 'session', 'root_person_ids']:
45 valid_fields.remove(key)
47 # Make sure that only valid fields are specified
48 if return_fields is None:
49 return_fields = valid_fields
50 elif filter(lambda field: field not in valid_fields, return_fields):
51 raise PLCInvalidArgument, "Invalid return field specified"
53 # Get node information
54 nodes = Nodes(self.api, node_id_or_hostname_list, return_fields).values()
56 # Filter out undesired or None fields (XML-RPC cannot marshal
57 # None) and turn each node into a real dict.
58 valid_return_fields_only = lambda (key, value): \
59 key in return_fields and value is not None
60 nodes = [dict(filter(valid_return_fields_only, node.items())) \