get rid of svn keywords once and for good
[plcapi.git] / PLC / Methods / GetWhitelist.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 class GetWhitelist(Method):
10     """
11     Returns an array of structs containing details about the specified nodes
12     whitelists. If node_filter is specified and is an array of node identifiers or
13     hostnames, or a struct of node attributes, only nodes matching the
14     filter will be returned. If return_fields is specified, only the
15     specified details will be returned.
16
17     Some fields may only be viewed by admins.
18     """
19
20     roles = ['admin', 'pi', 'user', 'tech', 'node', 'anonymous']
21
22     accepts = [
23         Auth(),
24         Mixed([Mixed(Node.fields['node_id'],
25                      Node.fields['hostname'])],
26               Filter(Node.fields)),
27         Parameter([str], "List of fields to return", nullok = True),
28         ]
29
30     returns = [Node.fields]
31
32
33     def call(self, auth, node_filter = None, return_fields = None):
34
35         # Must query at least slice_ids_whitelist
36         if return_fields is not None:
37             added_fields = set(['slice_ids_whitelist']).difference(return_fields)
38             return_fields += added_fields
39         else:
40             added_fields =[]
41
42         # Get node information
43         nodes = Nodes(self.api, node_filter, return_fields)
44
45         # Remove all nodes without a whitelist
46         for node in nodes[:]:
47             if not node['slice_ids_whitelist']:
48                 nodes.remove(node)
49
50         # Remove admin only fields
51         if not isinstance(self.caller, Person) or \
52            'admin' not in self.caller['roles']:
53             slice_ids = set()
54             if self.caller:
55                 slice_ids.update(self.caller['slice_ids'])
56             #if node has whitelist, make sure the user has a slice on the whitelist
57             for node in nodes[:]:
58                 if 'slice_ids_whitelist' in node and \
59                    node['slice_ids_whitelist'] and \
60                    not slice_ids.intersection(node['slice_ids_whitelist']):
61                     nodes.remove(node)
62             for node in nodes:
63                 for field in ['boot_nonce', 'key', 'session', 'root_person_ids']:
64                     if field in node:
65                         del node[field]
66
67         # remove added fields if not specified
68         if added_fields:
69             for node in nodes:
70                 for field in added_fields:
71                     del node[field]
72
73         return nodes