(most) all functions now take SessionAuth in addition to PasswordAuth
[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     Delete an existing Node Network. Nodenetwork_id must be associated to 
11     node_id and not be associated with a different node.
12
13     ins may delete any node network. PIs and techs can only delete 
14     nodenetworks for thier nodes.
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     roles = ['admin', 'pi', 'tech']
20
21     accepts = [
22         Auth(),
23         Mixed(NodeNetwork.fields['nodenetwork_id'],
24               NodeNetwork.fields['ip'])
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29     def call(self, auth, nodenetwork_id_or_ip):
30         # Get node network information
31         nodenetworks = NodeNetworks(self.api, [nodenetwork_id_or_ip]).values()
32         if not nodenetworks:
33             raise PLCInvalidArgument, "No such node network"
34         nodenetwork = nodenetworks[0]
35         
36         # Get node information
37         nodes = Nodes(self.api, [nodenetwork['node_id']]).values()
38         if not nodes:
39                 raise PLCInvalidArgument, "No such node"
40         node = nodes[0]
41
42         # Authenticated functino
43         assert self.caller is not None
44
45         # If we are not an admin, make sure that the caller is a
46         # member of the site at which the node is located.
47         if 'admin' not in self.caller['roles']:
48             if node['site_id'] not in self.caller['site_ids']:
49                 raise PLCPermissionDenied, "Not allowed to delete this node network"
50
51         nodenetwork.delete()
52
53         return 1