a7124d881798937bde40dac07eeedb99e63bd706
[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.Persons import Person, Persons
7 from PLC.Auth import Auth
8 from PLC.Plugins import PluginManager
9
10 class AddSliceToNodes(Method):
11     """
12     Adds the specified slice to the specified nodes. Nodes may be
13     either local or foreign nodes.
14
15     If the slice is already associated with a node, no errors are
16     returned.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     roles = ['admin', 'pi', 'user']
22
23     accepts = [
24         Auth(),
25         Mixed(Slice.fields['slice_id'],
26               Slice.fields['name']),
27         [Mixed(Node.fields['node_id'],
28                Node.fields['hostname'])]
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
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])
36         if not slices:
37             raise PLCInvalidArgument, "No such slice %r"%slice_id_or_name
38         slice = slices[0]
39
40         if slice['peer_id'] is not None:
41             raise PLCInvalidArgument, "Not a local slice"
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         # Get specified nodes, add them to the slice
52         nodes = Nodes(self.api, node_id_or_hostname_list, 
53                       ['node_id', 'hostname', 'slice_ids', 'slice_ids_whitelist', 'site_id'])
54
55         for node in nodes:
56             # check the slice whitelist on each node first
57             # allow users at site to add node to slice, ignoring whitelist
58             if node['slice_ids_whitelist'] and \
59                slice['slice_id'] not in node['slice_ids_whitelist'] and \
60                not set(self.caller['site_ids']).intersection([node['site_id']]):
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         nodeids = [node['node_id'] for node in nodes]
69         self.event_objects = {'Node': nodeids,
70                               'Slice': [slice['slice_id']]}
71         self.message = 'Slice %d added to nodes %s' % (slice['slice_id'], nodeids)
72
73         PluginManager(self.api, auth).notify("slice.AddToNodes", self.event_objects)
74
75         return 1