bc7dc7e33f9ce2b9c36efdd386689c5e314d26ab
[plcapi.git] / PLC / Methods / GetNodes.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 v43GetNodes(Method):
11     """
12     Returns an array of structs containing details about nodes. If
13     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               Parameter(str,"hostname"),
28               Parameter(int,"node_id"),
29               Filter(Node.fields)),
30         Parameter([str], "List of fields to return", nullok = True),
31         ]
32
33     returns = [Node.fields]
34
35
36     def call(self, auth, node_filter = None, return_fields = None):
37         
38         # Must query at least slice_ids_whitelist
39         if return_fields is not None:
40             added_fields = set(['slice_ids_whitelist', 'site_id']).difference(return_fields)
41             return_fields += added_fields
42         else:
43             added_fields =[]    
44
45         # Get node information
46         nodes = Nodes(self.api, node_filter, return_fields)
47
48         # Remove admin only fields
49         if not isinstance(self.caller, Person) or \
50            'admin' not in self.caller['roles']:
51             slice_ids = set()
52             site_ids = set()
53             
54             if self.caller:
55                 slice_ids.update(self.caller['slice_ids'])
56                 if isinstance(self.caller, Node):
57                     site_ids.update([self.caller['site_id']])
58                 else:  
59                     site_ids.update(self.caller['site_ids'])
60
61             # if node has whitelist, only return it if users is at
62             # the same site or user has a slice on the whitelist 
63             for node in nodes[:]:
64                 if 'site_id' in node and \
65                    site_ids.intersection([node['site_id']]):
66                     continue    
67                 if 'slice_ids_whitelist' in node and \
68                    node['slice_ids_whitelist'] and \
69                    not slice_ids.intersection(node['slice_ids_whitelist']):
70                     nodes.remove(node)
71
72             # remove remaining admin only fields
73             for node in nodes:    
74                 for field in ['boot_nonce', 'key', 'session', 'root_person_ids']:
75                     if field in node:
76                         del node[field]
77         
78         # remove added fields if not specified
79         if added_fields:
80             for node in nodes:
81                 for field in added_fields:
82                     del node[field]     
83
84         return nodes
85
86 node_fields = Node.fields.copy()
87 node_fields['nodenetwork_ids']=Parameter([int], "Legacy version of interface_ids")
88
89 class v42GetNodes(v43GetNodes):
90     """
91     Legacy wrapper for v43GetNodes.
92     """
93
94     accepts = [
95         Auth(),
96         Mixed([Mixed(Node.fields['node_id'],
97                      Node.fields['hostname'])],
98               Parameter(str,"hostname"),
99               Parameter(int,"node_id"),
100               Filter(node_fields)),
101         Parameter([str], "List of fields to return", nullok = True),
102         ]
103     returns = [node_fields]
104
105     def call(self, auth, node_filter = None, return_fields = None):
106         # convert nodenetwork_ids -> interface_ids
107         if isinstance(node_filter, dict):
108             if node_filter.has_key('nodenetwork_ids'):
109                 interface_ids = node_filter.pop('nodenetwork_ids')
110                 if not node_filter.has_key('interface_ids'):
111                     node_filter['interface_ids']=interface_ids
112
113         if isinstance(return_fields, list):
114             if 'nodenetwork_ids' in return_fields:
115                 return_fields.remove('nodenetwork_ids')
116                 if 'interface_ids' not in return_fields:
117                     return_fields.append('interface_ids')
118         nodes = v43GetNodes.call(self,auth,node_filter,return_fields)
119         # if interface_ids are present, then create a nodenetwork_ids mapping
120         for node in nodes:
121             if node.has_key('interface_ids'):
122                 node['nodenetwork_ids']=node['interface_ids']
123         return nodes
124
125 class GetNodes(v42GetNodes):
126     """
127     Returns an array of structs containing details about nodes. If
128     node_filter is specified and is an array of node identifiers or
129     hostnames, or a struct of node attributes, only nodes matching the
130     filter will be returned. If return_fields is specified, only the
131     specified details will be returned.
132
133     Some fields may only be viewed by admins.
134     """
135
136     pass