99669e4a2e83acc00a1d98f17ffe33c983cfb720
[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.Persons import Person, Persons
7 from PLC.Auth import Auth
8
9 admin_only = ['key', 'session', 'boot_nonce' ]
10
11 class GetNodes(Method):
12     """
13     Returns an array of structs containing details about nodes. If
14     node_filter is specified and is an array of node identifiers or
15     hostnames, or a struct of node attributes, only nodes matching the
16     filter will be returned.
17
18     If return_fields is specified, only the specified details will be
19     returned. NOTE that if return_fields is unspecified, the complete
20     set of native fields are returned, which DOES NOT include tags at
21     this time.
22
23     Some fields may only be viewed by admins.
24     """
25
26     roles = ['admin', 'pi', 'user', 'tech', 'node', 'anonymous']
27
28     accepts = [
29         Auth(),
30         Mixed([Mixed(Node.fields['node_id'],
31                      Node.fields['hostname'])],
32               Parameter(str,"hostname"),
33               Parameter(int,"node_id"),
34               Filter(Node.fields)),
35         Parameter([str], "List of fields to return", nullok = True),
36         ]
37
38     returns = [Node.fields]
39
40
41     def call(self, auth, node_filter = None, return_fields = None):
42
43         # Must query at least slice_ids_whitelist
44         if return_fields is not None:
45             added_fields = set(['slice_ids_whitelist', 'site_id']).difference(return_fields)
46             return_fields += added_fields
47         else:
48             added_fields =[]
49
50         # Get node information
51         nodes = Nodes(self.api, node_filter, return_fields)
52
53         # Remove admin only fields
54         if not isinstance(self.caller, Person) or \
55            'admin' not in self.caller['roles']:
56             slice_ids = set()
57             site_ids = set()
58
59             if self.caller:
60                 slice_ids.update(self.caller['slice_ids'])
61                 if isinstance(self.caller, Node):
62                     site_ids.update([self.caller['site_id']])
63                 else:
64                     site_ids.update(self.caller['site_ids'])
65
66             # if node has whitelist, only return it if users is at
67             # the same site or user has a slice on the whitelist
68             for node in nodes[:]:
69                 if 'site_id' in node and \
70                    site_ids.intersection([node['site_id']]):
71                     continue
72                 if 'slice_ids_whitelist' in node and \
73                    node['slice_ids_whitelist'] and \
74                    not slice_ids.intersection(node['slice_ids_whitelist']):
75                     nodes.remove(node)
76
77             # remove remaining admin only fields
78             for node in nodes:
79                 for field in admin_only:
80                     if field in node:
81                         del node[field]
82
83         # remove added fields if not specified
84         if added_fields:
85             for node in nodes:
86                 for field in added_fields:
87                     del node[field]
88
89         return nodes