2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.Slices import Slice, Slices
7 from PLC.Persons import Person, Persons
8 from PLC.Auth import Auth
10 class AddSliceToNodes(Method):
12 Adds the specified slice to the specified nodes. Nodes may be
13 either local or foreign nodes.
15 If the slice is already associated with a node, no errors are
18 Returns 1 if successful, faults otherwise.
21 roles = ['admin', 'pi', 'user']
25 Mixed(Slice.fields['slice_id'],
26 Slice.fields['name']),
27 [Mixed(Node.fields['node_id'],
28 Node.fields['hostname'])]
31 returns = Parameter(int, '1 if successful')
33 def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
34 # Get slice information
35 slices = Slices(self.api, [slice_id_or_name])
37 raise PLCInvalidArgument, "No such slice"
40 if slice['peer_id'] is not None:
41 raise PLCInvalidArgument, "Not a local slice"
43 if 'admin' not in self.caller['roles']:
44 if self.caller['person_id'] in slice['person_ids']:
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"
51 # Get specified nodes, add them to the slice
52 nodes = Nodes(self.api, node_id_or_hostname_list, ['node_id', 'hostname', 'slice_ids', 'slice_ids_whitelist', 'site_id'])
55 # check the slice whitelist on each node first
56 # allow users at site to add node to slice, ignoring whitelist
57 if node['slice_ids_whitelist'] and \
58 slice['slice_id'] not in node['slice_ids_whitelist'] and \
59 not set(self.caller['site_ids']).intersection([node['site_id']]):
60 raise PLCInvalidArgument, "%s is not allowed on %s (not on the whitelist)" % \
61 (slice['name'], node['hostname'])
62 if slice['slice_id'] not in node['slice_ids']:
63 slice.add_node(node, commit = False)
67 self.event_objects = {'Node': [node['node_id'] for node in nodes],
68 'Slice': [slice['slice_id']]}