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