- removed 'Adm' prefix
[plcapi.git] / PLC / Methods / GetNodes.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 GetNodes(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     returns = [Node.fields]
30
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())
35
36     def call(self, auth, node_id_or_hostname_list = None, return_fields = None):
37         # Authenticated function
38         assert self.caller is not None
39
40         valid_fields = Node.fields.keys()
41
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)
46
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"
52
53         # Get node information
54         nodes = Nodes(self.api, node_id_or_hostname_list).values()
55
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())) \
61                  for node in nodes]
62                     
63         return nodes