- added class variables: event_type, object_type, object_ids (used by event logger)
[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 PasswordAuth
7
8 class AddSliceToNodes(Method):
9     """
10     Adds the specified slice to the specified nodes. If the slice is
11     already associated with a node, no errors are returned. 
12
13     Returns 1 if successful, faults otherwise.
14     """
15
16     roles = ['admin', 'pi', 'user']
17
18     accepts = [
19         PasswordAuth(),
20         Mixed(Slice.fields['slice_id'],
21               Slice.fields['name']),
22         [Mixed(Node.fields['node_id'],
23                Node.fields['hostname'])]
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28     event_type = 'AddTo'
29     object_type = 'Node'
30     object_ids = []
31
32     def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
33         # Get slice information
34         slices = Slices(self.api, [slice_id_or_name])
35         if not slices:
36             raise PLCInvalidArgument, "No such slice"
37
38         slice = slices.values()[0]
39
40          # Get specified nodes
41         nodes = Nodes(self.api, node_id_or_hostname_list).values()
42
43         if 'admin' not in self.caller['roles']:
44             if self.caller['person_id'] in slice['person_ids']:
45                 pass
46             elif 'pi' not in self.caller['roles']:
47                 raise PLCPermissionDenied, "Not a member of the specified slice"
48             elif slice['site_id'] not in self.caller['site_ids']:
49                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
50         
51         # Add slice to all nodes found
52         for node in nodes:
53             if slice['slice_id'] not in node['slice_ids']:
54                 slice.add_node(node)
55                  
56         self.object_ids = [node['node_id'] for node in nodes]
57
58         return 1