nodenetworks cannot be referred to by ip (may be NULL)
[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     def call(self, auth, nodenetwork_id):
28         # Get node network information
29         nodenetworks = NodeNetworks(self.api, [nodenetwork_id])
30         if not nodenetworks:
31             raise PLCInvalidArgument, "No such node network"
32         nodenetwork = nodenetworks[0]
33         
34         # Get node information
35         nodes = Nodes(self.api, [nodenetwork['node_id']])
36         if not nodes:
37                 raise PLCInvalidArgument, "No such node"
38         node = nodes[0]
39
40         # Authenticated functino
41         assert self.caller is not None
42
43         # If we are not an admin, make sure that the caller is a
44         # member of the site at which the node is located.
45         if 'admin' not in self.caller['roles']:
46             if node['site_id'] not in self.caller['site_ids']:
47                 raise PLCPermissionDenied, "Not allowed to delete this node network"
48
49         nodenetwork.delete()
50
51         return 1