svn keywords
[plcapi.git] / PLC / Methods / AddSliceToNodes.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Nodes import Node, Nodes
7 from PLC.Slices import Slice, Slices
8 from PLC.Persons import Person, Persons
9 from PLC.Auth import Auth
10
11 class AddSliceToNodes(Method):
12     """
13     Adds the specified slice to the specified nodes. Nodes may be
14     either local or foreign nodes.
15
16     If the slice is already associated with a node, no errors are
17     returned.
18
19     Returns 1 if successful, faults otherwise.
20     """
21
22     roles = ['admin', 'pi', 'user']
23
24     accepts = [
25         Auth(),
26         Mixed(Slice.fields['slice_id'],
27               Slice.fields['name']),
28         [Mixed(Node.fields['node_id'],
29                Node.fields['hostname'])]
30         ]
31
32     returns = Parameter(int, '1 if successful')
33
34     def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
35         # Get slice information
36         slices = Slices(self.api, [slice_id_or_name])
37         if not slices:
38             raise PLCInvalidArgument, "No such slice"
39         slice = slices[0]
40
41         if slice['peer_id'] is not None:
42             raise PLCInvalidArgument, "Not a local slice"
43
44         if 'admin' not in self.caller['roles']:
45             if self.caller['person_id'] in slice['person_ids']:
46                 pass
47             elif 'pi' not in self.caller['roles']:
48                 raise PLCPermissionDenied, "Not a member of the specified slice"
49             elif slice['site_id'] not in self.caller['site_ids']:
50                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
51         
52         # Get specified nodes, add them to the slice         
53         nodes = Nodes(self.api, node_id_or_hostname_list, ['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         self.event_objects = {'Node': [node['node_id'] for node in nodes],
69                               'Slice': [slice['slice_id']]}
70
71         return 1