iteration 4 & last:
[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.Filter import Filter
5 from PLC.Nodes import Node, Nodes
6 from PLC.Auth import Auth
7
8 class GetNodes(Method):
9     """
10     Returns an array of structs containing details about nodes. If
11     node_filter is specified and is an array of node identifiers or
12     hostnames, or a struct of node attributes, only nodes matching the
13     filter will be returned. If return_fields is specified, only the
14     specified details will be returned.
15
16     Some fields may only be viewed by admins.
17     """
18
19     roles = ['admin', 'pi', 'user', 'tech']
20
21     accepts = [
22         Auth(),
23         Mixed([Mixed(Node.fields['node_id'],
24                      Node.fields['hostname'])],
25               Filter(Node.fields)),
26         Parameter([str], "List of fields to return", nullok = True),
27         Parameter(str,"scope string, can be either 'all', 'local' or 'foreign'"),
28         ]
29
30     returns = [Node.fields]
31
32     def call(self, auth, node_filter = None, return_fields = None, scope = 'all'):
33         # Get node information
34         nodes = Nodes(self.api, node_filter, return_fields, scope)
35
36         # Remove admin only fields
37         if 'admin' not in self.caller['roles']:
38             for node in nodes:
39                 for field in ['boot_nonce', 'key', 'session', 'root_person_ids']:
40                     if field in node:
41                         del node[field]
42
43         return nodes