more detailed info passed when raising an exception
[plcapi.git] / PLC / Methods / DeleteNodeNetwork.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import Auth
5 from PLC.Nodes import Node, Nodes
6 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
7
8 class DeleteNodeNetwork(Method):
9     """
10     Deletes an existing node network interface.
11
12     Admins may delete any node network. PIs and techs may only delete
13     node network interfaces associated with nodes at their sites.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin', 'pi', 'tech']
19
20     accepts = [
21         Auth(),
22         NodeNetwork.fields['nodenetwork_id']
23         ]
24
25     returns = Parameter(int, '1 if successful')
26
27     event_type = 'Delete'
28     object_type = 'NodeNetwork'
29
30     def call(self, auth, nodenetwork_id):
31
32         # Get node network information
33         nodenetworks = NodeNetworks(self.api, [nodenetwork_id])
34         if not nodenetworks:
35             raise PLCInvalidArgument, "No such node network"
36         nodenetwork = nodenetworks[0]
37         
38         # Get node information
39         nodes = Nodes(self.api, [nodenetwork['node_id']])
40         if not nodes:
41                 raise PLCInvalidArgument, "No such node"
42         node = nodes[0]
43
44         # Authenticated functino
45         assert self.caller is not None
46
47         # If we are not an admin, make sure that the caller is a
48         # member of the site at which the node is located.
49         if 'admin' not in self.caller['roles']:
50             if node['site_id'] not in self.caller['site_ids']:
51                 raise PLCPermissionDenied, "Not allowed to delete this node network"
52
53         nodenetwork.delete()
54         self.object_ids = [nodenetwork['nodenetwork_id']]
55
56         return 1