2 # Thierry Parmentelat - INRIA
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
9 from PLC.Sites import Sites
10 from PLC.Nodes import Nodes
11 from PLC.Interfaces import Interface, Interfaces
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.InterfaceTags import InterfaceTag, InterfaceTags
15 class AddInterfaceTag(Method):
17 Sets the specified setting for the specified interface
18 to the specified value.
20 Admins have full access. Non-admins need to
21 (1) have at least one of the roles attached to the tagtype,
22 and (2) belong in the same site as the tagged subject.
24 Returns the new interface_tag_id (> 0) if successful, faults
28 roles = ['admin', 'pi', 'tech', 'user']
32 # no other way to refer to a interface
33 InterfaceTag.fields['interface_id'],
34 Mixed(TagType.fields['tag_type_id'],
35 TagType.fields['tagname']),
36 InterfaceTag.fields['value'],
39 returns = Parameter(int, 'New interface_tag_id (> 0) if successful')
41 def call(self, auth, interface_id, tag_type_id_or_name, value):
42 interfaces = Interfaces(self.api, [interface_id])
44 raise PLCInvalidArgument, "No such interface %r"%interface_id
45 interface = interfaces[0]
47 tag_types = TagTypes(self.api, [tag_type_id_or_name])
49 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
50 tag_type = tag_types[0]
52 # checks for existence - does not allow several different settings
53 conflicts = InterfaceTags(self.api,
54 {'interface_id':interface['interface_id'],
55 'tag_type_id':tag_type['tag_type_id']})
58 raise PLCInvalidArgument, "Interface %d already has setting %d"%(interface['interface_id'],
59 tag_type['tag_type_id'])
61 # check authorizations
62 if 'admin' in self.caller['roles']:
64 elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
65 raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
66 elif AuthorizeHelpers.interface_belongs_to_person (self.api, interface, self.caller):
69 raise PLCPermissionDenied, "%s: you must belong in the same site as subject interface"%self.name
72 interface_tag = InterfaceTag(self.api)
73 interface_tag['interface_id'] = interface['interface_id']
74 interface_tag['tag_type_id'] = tag_type['tag_type_id']
75 interface_tag['value'] = value
78 self.object_ids = [interface_tag['interface_tag_id']]
80 return interface_tag['interface_tag_id']