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