- removed return_fields param
[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         ]
27
28     returns = [Node.fields]
29
30     def __init__(self, *args, **kwds):
31         Method.__init__(self, *args, **kwds)
32         # Update documentation with list of default fields returned
33         self.__doc__ += os.linesep.join(Node.fields.keys())
34
35     def call(self, auth, node_id_or_hostname_list = None):
36         # Authenticated function
37         assert self.caller is not None
38
39         valid_fields = Node.fields.keys()
40
41         # Remove admin only fields
42         if 'admin' not in self.caller['roles']:
43             for key in ['boot_nonce', 'key', 'session', 'root_person_ids']:
44                 valid_fields.remove(key)
45
46         # Get node information
47         nodes = Nodes(self.api, node_id_or_hostname_list).values()
48
49         # turn each node into a real dict.
50         nodes = [dict(node.items()) for node in nodes]
51                     
52         return nodes