remove ugly hack - just assert node isinstance of Node OR ForeignNode
[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.ForeignNodes import ForeignNode, ForeignNodes
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     event_type = 'DeleteFrom'
30     object_type = 'Node'
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"
37
38         slice = slices[0]
39
40         if 'admin' not in self.caller['roles']:
41             if self.caller['person_id'] in slice['person_ids']:
42                 pass
43             elif 'pi' not in self.caller['roles']:
44                 raise PLCPermissionDenied, "Not a member of the specified slice"
45             elif slice['site_id'] not in self.caller['site_ids']:
46                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
47         
48         # Remove slice from all nodes found
49
50         # Get specified nodes
51         nodes = Nodes(self.api, node_id_or_hostname_list)
52         foreign_nodes = ForeignNodes(self.api, node_id_or_hostname_list)
53         all_nodes = nodes+foreign_nodes;
54         for node in all_nodes:
55             if slice['slice_id'] in node['slice_ids']:
56                 slice.remove_node(node, commit = False)
57
58         slice.sync()
59         
60         self.object_ids = [node['node_id'] for node in all_nodes]
61
62         return 1