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