- re-enable return_fields specification
[plcapi.git] / PLC / Methods / UpdateNodeNetwork.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.NodeNetworks import NodeNetwork, NodeNetworks
6 from PLC.Auth import Auth
7
8 can_update = lambda (field, value): field not in \
9              ['nodenetwork_id']
10
11 class UpdateNodeNetwork(Method):
12     """
13     Updates an existing node network. Any values specified in update_fields 
14     are used, otherwise defaults are used. Acceptable values for method are
15     dhcp and static. If type is static, the parameter update_fields must
16     be present and ip, gateway, network, broadcast, netmask, and dns1 must
17     all be specified. If type is dhcp, these parameters, even if
18     specified, are ignored.
19     
20     PIs and techs may only update networks associated with their own
21     nodes. ins may update any node network.
22  
23     Returns 1 if successful, faults otherwise.
24     """
25
26     roles = ['admin', 'pi', 'tech']
27
28     nodenetwork_fields = dict(filter(can_update, NodeNetwork.fields.items()))
29
30     accepts = [
31         Auth(),
32         Mixed(NodeNetwork.fields['nodenetwork_id'],
33               NodeNetwork.fields['ip']),
34         nodenetwork_fields
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     def call(self, auth, nodenetwork_id_or_ip, nodenetwork_fields):
40         nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
41
42         # Get node network information
43         nodenetworks = NodeNetworks(self.api, [nodenetwork_id_or_ip])
44         if not nodenetworks:
45             raise PLCInvalidArgument, "No such node network"
46
47         nodenetwork = nodenetworks[0]
48                 
49         # Authenticated function
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 where the node exists.
54         if 'admin' not in self.caller['roles']:
55             nodes = Nodes(self.api, [nodenetwork['node_id']])
56             if not nodes:
57                 raise PLCPermissionDenied, "Node network is not associated with a node"
58             node = nodes[0]
59             if node['site_id'] not in self.caller['site_ids']:
60                 raise PLCPermissionDenied, "Not allowed to update node network"
61
62         # Update node network
63         nodenetwork.update(nodenetwork_fields)
64         nodenetwork.sync()
65         
66         return 1