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
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
14 cannot_update = ['interface_id','node_id']
16 class UpdateInterface(Method):
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.
25 PIs and techs may only update interfaces associated with their own
26 nodes. Admins may update any interface network.
28 Returns 1 if successful, faults otherwise.
31 roles = ['admin', 'pi', 'tech']
33 accepted_fields = Row.accepted_fields(cannot_update, Interface.fields,exclude=True)
34 accepted_fields.update(Interface.tags)
38 Interface.fields['interface_id'],
42 returns = Parameter(int, '1 if successful')
44 def call(self, auth, interface_id, interface_fields):
46 [native,tags,rejected] = Row.split_fields(interface_fields,[Interface.fields,Interface.tags])
49 native= Row.check_fields (native, self.accepted_fields)
51 raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected
53 # Get interface information
54 interfaces = Interfaces(self.api, [interface_id])
56 raise PLCInvalidArgument, "No such interface"
58 interface = interfaces[0]
60 # Authenticated function
61 assert self.caller is not None
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']])
68 raise PLCPermissionDenied, "Interface is not associated with a node"
70 if node['site_id'] not in self.caller['site_ids']:
71 raise PLCPermissionDenied, "Not allowed to update interface"
73 interface.update(native)
74 interface.update_last_updated(commit=False)
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)
85 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
87 self.event_objects = {'Interface': [interface['interface_id']]}
89 self.message = "Interface %s updated"%interface['ip']
91 self.message = "Interface %d updated"%interface['interface_id']
92 self.message += "[%s]." % ", ".join(interface_fields.keys())