allow filters to be specified in most Get() calls
[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.
14
15     Some fields may only be viewed by admins.
16     """
17
18     roles = ['admin', 'pi', 'user', 'tech']
19
20     accepts = [
21         Auth(),
22         Mixed([Mixed(Node.fields['node_id'],
23                      Node.fields['hostname'])],
24               Filter(Node.fields))
25         ]
26
27     returns = [Node.fields]
28
29     def call(self, auth, node_filter = None):
30         # Get node information
31         nodes = Nodes(self.api, node_filter).values()
32
33         # Remove admin only fields
34         if 'admin' not in self.caller['roles']:
35             for node in nodes:
36                 for field in ['boot_nonce', 'key', 'session', 'root_person_ids']:
37                     if field in node:
38                         del node[field]
39
40         return nodes