8bad0ad9fbf5d06364db0841b566f08a0e376164
[plcapi.git] / PLC / Methods / AddSliceToNodes.py
1 # $Id$
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
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"
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, ['node_id', 'hostname', 'slice_ids', 'slice_ids_whitelist', 'site_id'])
53         
54         for node in nodes:
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)
64
65         slice.sync()
66
67         self.event_objects = {'Node': [node['node_id'] for node in nodes],
68                               'Slice': [slice['slice_id']]}
69
70         return 1