DeleteSliceFromNodes: check that slice is local (like AddSliceToNodes does)
[plcapi.git] / PLC / Methods / DeleteSliceFromNodes.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.Auth import Auth
7
8 class DeleteSliceFromNodes(Method):
9     """
10     Deletes the specified slice from the specified nodes. If the slice is
11     not associated with a node, no errors are returned.
12
13     Returns 1 if successful, faults otherwise.
14     """
15
16     roles = ['admin', 'pi', 'user']
17
18     accepts = [
19         Auth(),
20        Mixed(Slice.fields['slice_id'],
21               Slice.fields['name']),
22         [Mixed(Node.fields['node_id'],
23                Node.fields['hostname'])]
24         ]
25
26     returns = Parameter(int, '1 if successful')
27
28     def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
29         # Get slice information
30         slices = Slices(self.api, [slice_id_or_name])
31         if not slices:
32             raise PLCInvalidArgument, "No such slice"
33         slice = slices[0]
34
35         if slice['peer_id'] is not None:
36             raise PLCInvalidArgument, "Not a local slice"
37
38         if 'admin' not in self.caller['roles']:
39             if self.caller['person_id'] in slice['person_ids']:
40                 pass
41             elif 'pi' not in self.caller['roles']:
42                 raise PLCPermissionDenied, "Not a member of the specified slice"
43             elif slice['site_id'] not in self.caller['site_ids']:
44                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
45
46         # Remove slice from all nodes found
47
48         # Get specified nodes
49         nodes = Nodes(self.api, node_id_or_hostname_list)
50         for node in nodes:
51             if slice['peer_id'] is not None and node['peer_id'] is not None:
52                 raise PLCPermissionDenied, "Not allowed to remove peer slice from peer node"
53             if slice['slice_id'] in node['slice_ids']:
54                 slice.remove_node(node, commit = False)
55
56         slice.sync()
57
58         self.event_objects = {'Node': [node['node_id'] for node in nodes],
59                               'Slice': [slice['slice_id']]}
60
61         return 1