first draft for node tags & new node groups:
[plcapi.git] / PLC / Methods / UpdateInterface.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.Interfaces import Interface, Interfaces
6 from PLC.Auth import Auth
7
8 can_update = lambda (field, value): field not in \
9              ['interface_id','node_id']
10
11 class UpdateInterface(Method):
12     """
13     Updates an existing node network. Any values specified in
14     interface_fields are used, otherwise defaults are
15     used. Acceptable values for method are dhcp and static. If type is
16     static, then ip, gateway, network, broadcast, netmask, and dns1
17     must all be specified in interface_fields. If type is dhcp,
18     these parameters, even if specified, are ignored.
19     
20     PIs and techs may only update networks associated with their own
21     nodes. Admins may update any node network.
22  
23     Returns 1 if successful, faults otherwise.
24     """
25
26     roles = ['admin', 'pi', 'tech']
27
28     interface_fields = dict(filter(can_update, Interface.fields.items()))
29
30     accepts = [
31         Auth(),
32         Interface.fields['interface_id'],
33         interface_fields
34         ]
35
36     returns = Parameter(int, '1 if successful')
37
38     def call(self, auth, interface_id, interface_fields):
39         interface_fields = dict(filter(can_update, interface_fields.items()))
40
41         # Get node network information
42         interfaces = Interfaces(self.api, [interface_id])
43         if not interfaces:
44             raise PLCInvalidArgument, "No such node network"
45
46         interface = interfaces[0]
47                 
48         # Authenticated function
49         assert self.caller is not None
50
51         # If we are not an admin, make sure that the caller is a
52         # member of the site where the node exists.
53         if 'admin' not in self.caller['roles']:
54             nodes = Nodes(self.api, [interface['node_id']])
55             if not nodes:
56                 raise PLCPermissionDenied, "Node network is not associated with a node"
57             node = nodes[0]
58             if node['site_id'] not in self.caller['site_ids']:
59                 raise PLCPermissionDenied, "Not allowed to update node network"
60
61         # Update node network
62         interface.update(interface_fields)
63         interface.sync()
64         
65         self.event_objects = {'Interface': [interface['interface_id']]}
66         self.message = "Node network %d updated: %s " % \
67             (interface['interface_id'], ", ".join(interface_fields.keys()))
68
69         return 1