dad6420e160f57e993b0f41adc2e8888537b500d
[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.Table import Row
6 from PLC.Auth import Auth
7
8 from PLC.Nodes import Node, Nodes
9 from PLC.TagTypes import TagTypes
10 from PLC.InterfaceTags import InterfaceTags
11 from PLC.Interfaces import Interface, Interfaces
12 from PLC.Methods.AddInterfaceTag import AddInterfaceTag
13 from PLC.Methods.UpdateInterfaceTag import UpdateInterfaceTag
14
15 can_update = ['interface_id','node_id']
16
17 class UpdateInterface(Method):
18     """
19     Updates an existing interface network. Any values specified in
20     interface_fields are used, otherwise defaults are
21     used. Acceptable values for method are dhcp and static. If type is
22     static, then ip, gateway, network, broadcast, netmask, and dns1
23     must all be specified in interface_fields. If type is dhcp,
24     these parameters, even if specified, are ignored.
25     
26     PIs and techs may only update interfaces associated with their own
27     nodes. Admins may update any interface network.
28  
29     Returns 1 if successful, faults otherwise.
30     """
31
32     roles = ['admin', 'pi', 'tech']
33
34     accepted_fields = Row.accepted_fields(can_update, [Interface.fields,Interface.tags])
35
36     accepts = [
37         Auth(),
38         Interface.fields['interface_id'],
39         accepted_fields
40         ]
41
42     returns = Parameter(int, '1 if successful')
43
44     def call(self, auth, interface_id, interface_fields):
45
46         [native,tags,rejected] = Row.split_fields(interface_fields,[Interface.fields,Interface.tags])
47
48         if rejected:
49             raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected
50
51         # Get interface information
52         interfaces = Interfaces(self.api, [interface_id])
53         if not interfaces:
54             raise PLCInvalidArgument, "No such interface"
55
56         interface = interfaces[0]
57                 
58         # Authenticated function
59         assert self.caller is not None
60
61         # If we are not an admin, make sure that the caller is a
62         # member of the site where the node exists.
63         if 'admin' not in self.caller['roles']:
64             nodes = Nodes(self.api, [interface['node_id']])
65             if not nodes:
66                 raise PLCPermissionDenied, "Interface is not associated with a node"
67             node = nodes[0]
68             if node['site_id'] not in self.caller['site_ids']:
69                 raise PLCPermissionDenied, "Not allowed to update interface"
70
71         interface.update(native)
72         interface.sync()
73         
74         for (tagname,value) in tags.iteritems():
75             # the tagtype instance is assumed to exist, just check that
76             if not TagTypes(self.api,{'tagname':tagname}):
77                 raise PLCInvalidArgument,"No such TagType %s"%tagname
78             interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']})
79             if not interface_tags:
80                 AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value)
81             else:
82                 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
83
84         self.event_objects = {'Interface': [interface['interface_id']]}
85         if 'ip' in interface:
86             self.message = "Interface %s updated"%interface['ip']
87         else:
88             self.message = "Interface %d updated"%interface['interface_id']
89         self.message += "[%s]." % ", ".join(interface_fields.keys())
90
91         return 1