- removed tech role check
[plcapi.git] / PLC / Methods / AdmUpdateNodeNetwork.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 AdmUpdateNodeNetwork(Method):
10     """
11     Updates an existing node network. Any values specified in optional_vals 
12     are used, otherwise defaults are used. Acceptable values for method are
13     dhcp and static. If type is static, the parameter optional_vals 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 add networks to their own nodes. Admins may
19     add networks to any node.
20  
21     Returns 1 if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'tech']
25
26     cant_update = lambda (field, value): field not in \
27                  ['nodenetwork_id']
28     update_fields = dict(filter(cant_update, NodeNetwork.all_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, optional_vals=None):
40         if filter(lambda field: field not in self.update_fields, optional_vals):
41                 raise PLCInvalidArgument, "Invalid fields specified"
42
43         # Authenticated function
44         assert self.caller is not None
45
46         # Get nodenetwork information
47         nodenetworks = NodeNetworks(self.api, [nodenetwork_id_or_hostname]).values()
48         if not nodenetworks:
49                 raise PLCInvalidArgument, "No such node network"
50         nodenetwork = nodenetworks[0]
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']]).values()
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(optional_vals)
64         nodenetwork.flush()
65         
66         return 1