4 # Thierry Parmentelat - INRIA
8 from PLC.Faults import *
9 from PLC.Method import Method
10 from PLC.Parameter import Parameter, Mixed
11 from PLC.Auth import Auth
13 from PLC.TagTypes import TagType, TagTypes
14 from PLC.InterfaceTags import InterfaceTag, InterfaceTags
15 from PLC.Interfaces import Interface, Interfaces
17 from PLC.Nodes import Nodes
18 from PLC.Sites import Sites
20 class AddInterfaceTag(Method):
22 Sets the specified setting for the specified interface
23 to the specified value.
25 In general only tech(s), PI(s) and of course admin(s) are allowed to
26 do the change, but this is defined in the tag type object.
28 Returns the new interface_tag_id (> 0) if successful, faults
32 roles = ['admin', 'pi', 'tech', 'user']
36 # no other way to refer to a interface
37 InterfaceTag.fields['interface_id'],
38 Mixed(TagType.fields['tag_type_id'],
39 TagType.fields['tagname']),
40 InterfaceTag.fields['value'],
43 returns = Parameter(int, 'New interface_tag_id (> 0) if successful')
45 object_type = 'Interface'
48 def call(self, auth, interface_id, tag_type_id_or_name, value):
49 interfaces = Interfaces(self.api, [interface_id])
51 raise PLCInvalidArgument, "No such interface %r"%interface_id
52 interface = interfaces[0]
54 tag_types = TagTypes(self.api, [tag_type_id_or_name])
56 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
57 tag_type = tag_types[0]
59 # checks for existence - does not allow several different settings
60 conflicts = InterfaceTags(self.api,
61 {'interface_id':interface['interface_id'],
62 'tag_type_id':tag_type['tag_type_id']})
65 raise PLCInvalidArgument, "Interface %d already has setting %d"%(interface['interface_id'],
66 tag_type['tag_type_id'])
68 # check permission : it not admin, is the user affiliated with the right site
69 if 'admin' not in self.caller['roles']:
71 node = Nodes (self.api,[interface['node_id']])[0]
73 site = Sites (self.api, [node['site_id']])[0]
74 # check caller is affiliated with this site
75 if self.caller['person_id'] not in site['person_ids']:
76 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
78 required_min_role = tag_type ['min_role_id']
79 if required_min_role is not None and \
80 min(self.caller['role_ids']) > required_min_role:
81 raise PLCPermissionDenied, "Not allowed to modify the specified interface setting, requires role %d",required_min_role
83 interface_tag = InterfaceTag(self.api)
84 interface_tag['interface_id'] = interface['interface_id']
85 interface_tag['tag_type_id'] = tag_type['tag_type_id']
86 interface_tag['value'] = value
89 self.object_ids = [interface_tag['interface_tag_id']]
91 return interface_tag['interface_tag_id']