2406945e2e68f04b3be0a3ae7f185c35c8c090f6
[plcapi.git] / PLC / Methods / UpdateInterface.py
1 # $Id$
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.Interfaces import Interface, Interfaces
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field not in \
10              ['interface_id','node_id']
11
12 class UpdateInterface(Method):
13     """
14     Updates an existing node network. Any values specified in
15     interface_fields are used, otherwise defaults are
16     used. Acceptable values for method are dhcp and static. If type is
17     static, then ip, gateway, network, broadcast, netmask, and dns1
18     must all be specified in interface_fields. If type is dhcp,
19     these parameters, even if specified, are ignored.
20     
21     PIs and techs may only update networks associated with their own
22     nodes. Admins may update any node network.
23  
24     Returns 1 if successful, faults otherwise.
25     """
26
27     roles = ['admin', 'pi', 'tech']
28
29     interface_fields = dict(filter(can_update, Interface.fields.items()))
30
31     accepts = [
32         Auth(),
33         Interface.fields['interface_id'],
34         interface_fields
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     def call(self, auth, interface_id, interface_fields):
40         interface_fields = dict(filter(can_update, interface_fields.items()))
41
42         # Get node network information
43         interfaces = Interfaces(self.api, [interface_id])
44         if not interfaces:
45             raise PLCInvalidArgument, "No such node network"
46
47         interface = interfaces[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, [interface['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         interface.update(interface_fields)
64         interface.sync()
65         
66         self.event_objects = {'Interface': [interface['interface_id']]}
67         self.message = "Node network %d updated: %s " % \
68             (interface['interface_id'], ", ".join(interface_fields.keys()))
69
70         return 1