56d232b5db1b636f3df7480126ab8b8310767e6e
[plcapi.git] / PLC / Methods / AddInterfaceTag.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
8
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
14
15 class AddInterfaceTag(Method):
16     """
17     Sets the specified setting for the specified interface
18     to the specified value.
19
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.
23
24     Returns the new interface_tag_id (> 0) if successful, faults
25     otherwise.
26     """
27
28     roles = ['admin', 'pi', 'tech', 'user']
29
30     accepts = [
31         Auth(),
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'],
37         ]
38
39     returns = Parameter(int, 'New interface_tag_id (> 0) if successful')
40
41     def call(self, auth, interface_id, tag_type_id_or_name, value):
42         interfaces = Interfaces(self.api, [interface_id])
43         if not interfaces:
44             raise PLCInvalidArgument, "No such interface %r"%interface_id
45         interface = interfaces[0]
46
47         tag_types = TagTypes(self.api, [tag_type_id_or_name])
48         if not tag_types:
49             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
50         tag_type = tag_types[0]
51
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']})
56
57         if len(conflicts) :
58             raise PLCInvalidArgument, "Interface %d already has setting %d"%(interface['interface_id'],
59                                                                                tag_type['tag_type_id'])
60
61         # check authorizations
62         interface.caller_may_write_tag(self.api,self.caller,tag_type)
63
64         interface_tag = InterfaceTag(self.api)
65         interface_tag['interface_id'] = interface['interface_id']
66         interface_tag['tag_type_id'] = tag_type['tag_type_id']
67         interface_tag['value'] = value
68
69         interface_tag.sync()
70         self.object_ids = [interface_tag['interface_tag_id']]
71
72         return interface_tag['interface_tag_id']