iteration 4 & last:
[plcapi.git] / PLC / Methods / AddSliceToNodes.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Nodes import Node, Nodes
5 from PLC.Slices import Slice, Slices
6 from PLC.Auth import Auth
7
8 class AddSliceToNodes(Method):
9     """
10     Adds the specified slice to the specified nodes.
11     Nodes can be either local or foreign nodes, as returned by GetNodes
12
13     If the slice is
14     already associated with a node, no errors are returned. 
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     roles = ['admin', 'pi', 'user']
20
21     accepts = [
22         Auth(),
23         Mixed(Slice.fields['slice_id'],
24               Slice.fields['name']),
25         [Mixed(Node.fields['node_id'],
26                Node.fields['hostname'])]
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     event_type = 'AddTo'
32     object_type = 'Node'
33
34     def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
35         # Get slice information
36         slices = Slices(self.api, [slice_id_or_name])
37         if not slices:
38             raise PLCInvalidArgument, "No such slice"
39
40         slice = slices[0]
41
42         if 'admin' not in self.caller['roles']:
43             if self.caller['person_id'] in slice['person_ids']:
44                 pass
45             # Thierry : I cannot figure out how this works
46             # how is having pi role related to being in a slice ?
47             elif 'pi' not in self.caller['roles']:
48                 raise PLCPermissionDenied, "Not a member of the specified slice"
49             elif slice['site_id'] not in self.caller['site_ids']:
50                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
51         
52         # Get specified nodes, add them to the slice
53          
54         nodes = Nodes(self.api, node_id_or_hostname_list)
55         for node in nodes:
56             if slice['slice_id'] not in node['slice_ids']:
57                 slice.add_node(node, commit = False)
58
59         slice.sync()
60
61         self.object_ids = [node['node_id'] for node in nodes]
62
63         return 1