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