- fix parameter documentation
[plcapi.git] / PLC / Methods / AdmDeleteNodeNetwork.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import PasswordAuth
5 from PLC.Nodes import Node, Nodes
6 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
7
8 class AdmDeleteNodeNetwork(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     Admins 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         PasswordAuth(),
23         Mixed(Node.fields['node_id'],
24               Node.fields['hostname']),
25         Mixed(NodeNetwork.fields['nodenetwork_id'],
26               NodeNetwork.fields['hostname'])
27         ]
28
29     returns = Parameter(int, '1 if successful')
30
31     def call(self, auth, node_id_or_hostname, nodenetwork_id_or_hostname):
32         # Get node network information
33         nodenetworks = NodeNetworks(self.api, [nodenetwork_id_or_hostname]).values()
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, [node_id_or_hostname]).values()
40         if not nodes:
41                 raise PLCInvalidArgument, "No such node"
42         node = nodes[0]
43
44         # Check if node network is associated with specified node
45         if node['node_id'] != nodenetwork['node_id'] or \
46            nodenetwork['nodenetwork_id'] not in node['nodenetwork_ids']:
47             raise PLCInvalidArgument, "Node network not associated with node"
48
49         # Authenticated functino
50         assert self.caller is not None
51
52         # If we are not an admin, make sure that the caller is a
53         # member of the site at which the node is located.
54         if 'admin' not in self.caller['roles']:
55             if node['site_id'] not in self.caller['site_ids']:
56                 raise PLCPermissionDenied, "Not allowed to delete this node network"
57
58         nodenetwork.delete()
59
60         return 1