Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / trunk / 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 class GetNodes(Method):
10     """
11     Returns an array of structs containing details about nodes. If
12     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               Parameter(str,"hostname"),
27               Parameter(int,"node_id"),
28               Filter(Node.fields)),
29         Parameter([str], "List of fields to return", nullok = True),
30         ]
31
32     returns = [Node.fields]
33
34
35     def call(self, auth, node_filter = None, return_fields = None):
36         
37         # Must query at least slice_ids_whitelist
38         if return_fields is not None:
39             added_fields = set(['slice_ids_whitelist', 'site_id']).difference(return_fields)
40             return_fields += added_fields
41         else:
42             added_fields =[]    
43
44         # Get node information
45         nodes = Nodes(self.api, node_filter, return_fields)
46
47         # Remove admin only fields
48         if not isinstance(self.caller, Person) or \
49            'admin' not in self.caller['roles']:
50             slice_ids = set()
51             site_ids = set()
52             if self.caller:
53                 slice_ids.update(self.caller['slice_ids'])
54                 site_ids.update(self.caller['site_ids'])
55
56             # if node has whitelist, only return it if users is at
57             # the same site or user has a slice on the whitelist 
58             for node in nodes[:]:
59                 if 'site_id' in node and \
60                    site_ids.intersection([node['site_id']]):
61                     continue    
62                 if 'slice_ids_whitelist' in node and \
63                    node['slice_ids_whitelist'] and \
64                    not slice_ids.intersection(node['slice_ids_whitelist']):
65                     nodes.remove(node)
66
67             # remove remaining admin only fields
68             for node in nodes:    
69                 for field in ['boot_nonce', 'key', 'session', 'root_person_ids']:
70                     if field in node:
71                         del node[field]
72         
73         # remove added fields if not specified
74         if added_fields:
75             for node in nodes:
76                 for field in added_fields:
77                     del node[field]     
78
79         return nodes