api now allows marshalling of None (although type checking does not allow None yet...
[plcapi.git] / PLC / Methods / UpdateNodeNetwork.py
1
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
7 from PLC.Auth import PasswordAuth
8
9 class UpdateNodeNetwork(Method):
10     """
11     Updates an existing node network. Any values specified in update_fields 
12     are used, otherwise defaults are used. Acceptable values for method are
13     dhcp and static. If type is static, the parameter update_fields must
14     be present and ip, gateway, network, broadcast, netmask, and dns1 must
15     all be specified. If type is dhcp, these parameters, even if
16     specified, are ignored.
17     
18     PIs and techs may only update networks associated with their own
19     nodes. ins may update any node network.
20  
21     Returns 1 if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'tech']
25
26     can_update = lambda (field, value): field not in \
27                  ['nodenetwork_id']
28     update_fields = dict(filter(can_update, NodeNetwork.fields.items()))
29
30     accepts = [
31         PasswordAuth(),
32         Mixed(NodeNetwork.fields['nodenetwork_id'],
33               NodeNetwork.fields['hostname']),
34         update_fields
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     def call(self, auth, nodenetwork_id_or_hostname, update_fields):
40         # Check for invalid fields
41         if filter(lambda field: field not in self.update_fields, update_fields):
42             raise PLCInvalidArgument, "Invalid fields specified"
43
44         # Get node network information
45         nodenetworks = NodeNetworks(self.api, [nodenetwork_id_or_hostname]).values()
46         if not nodenetworks:
47             raise PLCInvalidArgument, "No such node network"
48
49         nodenetwork = nodenetworks[0]
50                 
51         # Authenticated function
52         assert self.caller is not None
53
54         # If we are not an admin, make sure that the caller is a
55         # member of the site where the node exists.
56         if 'admin' not in self.caller['roles']:
57             nodes = Nodes(self.api, [nodenetwork['node_id']]).values()
58             if not nodes:
59                 raise PLCPermissionDenied, "Node network is not associated with a node"
60             node = nodes[0]
61             if node['site_id'] not in self.caller['site_ids']:
62                 raise PLCPermissionDenied, "Not allowed to update node network"
63
64         # Update node network
65         nodenetwork.update(update_fields)
66         nodenetwork.sync()
67         
68         return 1