- allow any site member to add their slice to any node at their site, even if their...
[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. Nodes may be
11     either local or foreign nodes.
12
13     If the slice is already associated with a node, no errors are
14     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     def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
32         # Get slice information
33         slices = Slices(self.api, [slice_id_or_name])
34         if not slices:
35             raise PLCInvalidArgument, "No such slice"
36         slice = slices[0]
37
38         if slice['peer_id'] is not None:
39             raise PLCInvalidArgument, "Not a local slice"
40
41         if 'admin' not in self.caller['roles']:
42             if self.caller['person_id'] in slice['person_ids']:
43                 pass
44             elif 'pi' not in self.caller['roles']:
45                 raise PLCPermissionDenied, "Not a member of the specified slice"
46             elif slice['site_id'] not in self.caller['site_ids']:
47                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
48         
49         # Get specified nodes, add them to the slice         
50         nodes = Nodes(self.api, node_id_or_hostname_list, ['node_id', 'hostname', 'slice_ids', 'slice_ids_whitelist', 'site_id'])
51         
52         for node in nodes:
53             # allow  users at site to add node to slice, ignoring whitelist
54             if isinstance(self.caller, Person) and \
55                set(self.caller['site_ids']).intersection(node['site_id']):
56                 slice.add_node(node, commit = False)
57                 continue
58             # check the slice whitelist on each node first
59             if node['slice_ids_whitelist'] and \
60                slice['slice_id'] not in node['slice_ids_whitelist']:
61                 raise PLCInvalidArgument, "%s is not allowed on %s (not on the whitelist)" % \
62                   (slice['name'], node['hostname'])
63             if slice['slice_id'] not in node['slice_ids']:
64                 slice.add_node(node, commit = False)
65
66         slice.sync()
67
68         self.event_objects = {'Node': [node['node_id'] for node in nodes],
69                               'Slice': [slice['slice_id']]}
70
71         return 1