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
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
16 cannot_update = ['interface_id','node_id']
18 class UpdateInterface(Method):
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.
27 PIs and techs may only update interfaces associated with their own
28 nodes. Admins may update any interface network.
30 Returns 1 if successful, faults otherwise.
33 roles = ['admin', 'pi', 'tech']
35 accepted_fields = Row.accepted_fields(cannot_update, Interface.fields,exclude=True)
36 accepted_fields.update(Interface.tags)
40 Interface.fields['interface_id'],
44 returns = Parameter(int, '1 if successful')
46 def call(self, auth, interface_id, interface_fields):
48 [native,tags,rejected] = Row.split_fields(interface_fields,[Interface.fields,Interface.tags])
51 native= Row.check_fields (native, self.accepted_fields)
53 raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected
55 # Get interface information
56 interfaces = Interfaces(self.api, [interface_id])
58 raise PLCInvalidArgument, "No such interface"
60 interface = interfaces[0]
62 # Authenticated function
63 assert self.caller is not None
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']])
70 raise PLCPermissionDenied, "Interface is not associated with a node"
72 if node['site_id'] not in self.caller['site_ids']:
73 raise PLCPermissionDenied, "Not allowed to update interface"
75 interface.update(native)
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)
86 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
88 self.event_objects = {'Interface': [interface['interface_id']]}
90 self.message = "Interface %s updated"%interface['ip']
92 self.message = "Interface %d updated"%interface['interface_id']
93 self.message += "[%s]." % ", ".join(interface_fields.keys())