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