get rid of svn keywords once and for good
[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
9 class AddSliceToNodes(Method):
10     """
11     Adds the specified slice to the specified nodes. Nodes may be
12     either local or foreign nodes.
13
14     If the slice is already associated with a node, no errors are
15     returned.
16
17     Returns 1 if successful, faults otherwise.
18     """
19
20     roles = ['admin', 'pi', 'user']
21
22     accepts = [
23         Auth(),
24         Mixed(Slice.fields['slice_id'],
25               Slice.fields['name']),
26         [Mixed(Node.fields['node_id'],
27                Node.fields['hostname'])]
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
33         # Get slice information
34         slices = Slices(self.api, [slice_id_or_name])
35         if not slices:
36             raise PLCInvalidArgument, "No such slice %r"%slice_id_or_name
37         slice = slices[0]
38
39         if slice['peer_id'] is not None:
40             raise PLCInvalidArgument, "Not a local slice"
41
42         if 'admin' not in self.caller['roles']:
43             if self.caller['person_id'] in slice['person_ids']:
44                 pass
45             elif 'pi' not in self.caller['roles']:
46                 raise PLCPermissionDenied, "Not a member of the specified slice"
47             elif slice['site_id'] not in self.caller['site_ids']:
48                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
49
50         # Get specified nodes, add them to the slice
51         nodes = Nodes(self.api, node_id_or_hostname_list, ['node_id', 'hostname', 'slice_ids', 'slice_ids_whitelist', 'site_id'])
52
53         for node in nodes:
54             # check the slice whitelist on each node first
55             # allow  users at site to add node to slice, ignoring whitelist
56             if node['slice_ids_whitelist'] and \
57                slice['slice_id'] not in node['slice_ids_whitelist'] and \
58                not set(self.caller['site_ids']).intersection([node['site_id']]):
59                 raise PLCInvalidArgument, "%s is not allowed on %s (not on the whitelist)" % \
60                   (slice['name'], node['hostname'])
61             if slice['slice_id'] not in node['slice_ids']:
62                 slice.add_node(node, commit = False)
63
64         slice.sync()
65
66         nodeids = [node['node_id'] for node in nodes]
67         self.event_objects = {'Node': nodeids,
68                               'Slice': [slice['slice_id']]}
69         self.message = 'Slice %d added to nodes %s' % (slice['slice_id'], nodeids)
70
71         return 1