2 # Thierry Parmentelat - INRIA
6 from PLC.Faults import *
7 from PLC.Method import Method
8 from PLC.Parameter import Parameter, Mixed
9 from PLC.Auth import Auth
11 from PLC.TagTypes import TagType, TagTypes
12 from PLC.InterfaceSettings import InterfaceSetting, InterfaceSettings
13 from PLC.Interfaces import Interface, Interfaces
15 from PLC.Nodes import Nodes
16 from PLC.Sites import Sites
18 class AddInterfaceSetting(Method):
20 Sets the specified setting for the specified interface
21 to the specified value.
23 In general only tech(s), PI(s) and of course admin(s) are allowed to
24 do the change, but this is defined in the tag type object.
26 Returns the new interface_setting_id (> 0) if successful, faults
30 roles = ['admin', 'pi', 'tech', 'user']
34 # no other way to refer to a interface
35 InterfaceSetting.fields['interface_id'],
36 Mixed(TagType.fields['tag_type_id'],
37 TagType.fields['tagname']),
38 InterfaceSetting.fields['value'],
41 returns = Parameter(int, 'New interface_setting_id (> 0) if successful')
43 object_type = 'Interface'
46 def call(self, auth, interface_id, tag_type_id_or_name, value):
47 interfaces = Interfaces(self.api, [interface_id])
49 raise PLCInvalidArgument, "No such interface %r"%interface_id
50 interface = interfaces[0]
52 tag_types = TagTypes(self.api, [tag_type_id_or_name])
54 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
55 tag_type = tag_types[0]
57 # checks for existence - does not allow several different settings
58 conflicts = InterfaceSettings(self.api,
59 {'interface_id':interface['interface_id'],
60 'tag_type_id':tag_type['tag_type_id']})
63 raise PLCInvalidArgument, "Interface %d already has setting %d"%(interface['interface_id'],
64 tag_type['tag_type_id'])
66 # check permission : it not admin, is the user affiliated with the right site
67 if 'admin' not in self.caller['roles']:
69 node = Nodes (self.api,[interface['node_id']])[0]
71 site = Sites (self.api, [node['site_id']])[0]
72 # check caller is affiliated with this site
73 if self.caller['person_id'] not in site['person_ids']:
74 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
76 required_min_role = tag_type ['min_role_id']
77 if required_min_role is not None and \
78 min(self.caller['role_ids']) > required_min_role:
79 raise PLCPermissionDenied, "Not allowed to modify the specified interface setting, requires role %d",required_min_role
81 interface_setting = InterfaceSetting(self.api)
82 interface_setting['interface_id'] = interface['interface_id']
83 interface_setting['tag_type_id'] = tag_type['tag_type_id']
84 interface_setting['value'] = value
86 interface_setting.sync()
87 self.object_ids = [interface_setting['interface_setting_id']]
89 return interface_setting['interface_setting_id']