- fix documentation. remove return_fields junk
[plcapi.git] / PLC / Methods / GetNodes.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Nodes import Node, Nodes
5 from PLC.Auth import PasswordAuth
6
7 class GetNodes(Method):
8     """
9     Return an array of structs containing details about nodes. If
10     node_id_or_hostname_list is specified, only the specified nodes
11     will be queried.
12
13     Some fields may only be viewed by admins.
14     """
15
16     roles = ['admin', 'pi', 'user', 'tech']
17
18     accepts = [
19         PasswordAuth(),
20         [Mixed(Node.fields['node_id'],
21                Node.fields['hostname'])],
22         ]
23
24     returns = [Node.fields]
25
26     def call(self, auth, node_id_or_hostname_list = None):
27         # Authenticated function
28         assert self.caller is not None
29
30         valid_fields = Node.fields.keys()
31
32         # Remove admin only fields
33         if 'admin' not in self.caller['roles']:
34             for key in ['boot_nonce', 'key', 'session', 'root_person_ids']:
35                 if key in valid_fields:
36                     valid_fields.remove(key)
37
38         # Get node information
39         nodes = Nodes(self.api, node_id_or_hostname_list).values()
40
41         # Turn each node into a real dict
42         nodes = [dict(node) for node in nodes]
43                     
44         return nodes