3 # Thierry Parmentelat - INRIA
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.InterfaceSettings import InterfaceSetting, InterfaceSettings
14 from PLC.Interfaces import Interface, Interfaces
16 from PLC.Nodes import Nodes
17 from PLC.Sites import Sites
19 class AddInterfaceSetting(Method):
21 Sets the specified setting for the specified interface
22 to the specified value.
24 In general only tech(s), PI(s) and of course admin(s) are allowed to
25 do the change, but this is defined in the tag type object.
27 Returns the new interface_setting_id (> 0) if successful, faults
31 roles = ['admin', 'pi', 'tech', 'user']
35 # no other way to refer to a interface
36 InterfaceSetting.fields['interface_id'],
37 Mixed(TagType.fields['tag_type_id'],
38 TagType.fields['tagname']),
39 InterfaceSetting.fields['value'],
42 returns = Parameter(int, 'New interface_setting_id (> 0) if successful')
44 object_type = 'Interface'
47 def call(self, auth, interface_id, tag_type_id_or_name, value):
48 interfaces = Interfaces(self.api, [interface_id])
50 raise PLCInvalidArgument, "No such interface %r"%interface_id
51 interface = interfaces[0]
53 tag_types = TagTypes(self.api, [tag_type_id_or_name])
55 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
56 tag_type = tag_types[0]
58 # checks for existence - does not allow several different settings
59 conflicts = InterfaceSettings(self.api,
60 {'interface_id':interface['interface_id'],
61 'tag_type_id':tag_type['tag_type_id']})
64 raise PLCInvalidArgument, "Interface %d already has setting %d"%(interface['interface_id'],
65 tag_type['tag_type_id'])
67 # check permission : it not admin, is the user affiliated with the right site
68 if 'admin' not in self.caller['roles']:
70 node = Nodes (self.api,[interface['node_id']])[0]
72 site = Sites (self.api, [node['site_id']])[0]
73 # check caller is affiliated with this site
74 if self.caller['person_id'] not in site['person_ids']:
75 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
77 required_min_role = tag_type ['min_role_id']
78 if required_min_role is not None and \
79 min(self.caller['role_ids']) > required_min_role:
80 raise PLCPermissionDenied, "Not allowed to modify the specified interface setting, requires role %d",required_min_role
82 interface_setting = InterfaceSetting(self.api)
83 interface_setting['interface_id'] = interface['interface_id']
84 interface_setting['tag_type_id'] = tag_type['tag_type_id']
85 interface_setting['value'] = value
87 interface_setting.sync()
88 self.object_ids = [interface_setting['interface_setting_id']]
90 return interface_setting['interface_setting_id']