1ecff52ccf8a4e23d631823f48ab2fd4b653aa5b
[plcapi.git] / PLC / Methods / AddInterfaceTag.py
1 # $Id$
2 #
3 # Thierry Parmentelat - INRIA
4 #
5 # $Revision$
6 #
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
11
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.InterfaceTags import InterfaceTag, InterfaceTags
14 from PLC.Interfaces import Interface, Interfaces
15
16 from PLC.Nodes import Nodes
17 from PLC.Sites import Sites
18
19 class AddInterfaceTag(Method):
20     """
21     Sets the specified setting for the specified interface
22     to the specified value.
23
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.
26
27     Returns the new interface_tag_id (> 0) if successful, faults
28     otherwise.
29     """
30
31     roles = ['admin', 'pi', 'tech', 'user']
32
33     accepts = [
34         Auth(),
35         # no other way to refer to a interface
36         InterfaceTag.fields['interface_id'],
37         Mixed(TagType.fields['tag_type_id'],
38               TagType.fields['tagname']),
39         InterfaceTag.fields['value'],
40         ]
41
42     returns = Parameter(int, 'New interface_tag_id (> 0) if successful')
43
44     object_type = 'Interface'
45
46
47     def call(self, auth, interface_id, tag_type_id_or_name, value):
48         interfaces = Interfaces(self.api, [interface_id])
49         if not interfaces:
50             raise PLCInvalidArgument, "No such interface %r"%interface_id
51         interface = interfaces[0]
52
53         tag_types = TagTypes(self.api, [tag_type_id_or_name])
54         if not tag_types:
55             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
56         tag_type = tag_types[0]
57
58         # checks for existence - does not allow several different settings
59         conflicts = InterfaceTags(self.api,
60                                         {'interface_id':interface['interface_id'],
61                                          'tag_type_id':tag_type['tag_type_id']})
62
63         if len(conflicts) :
64             raise PLCInvalidArgument, "Interface %d already has setting %d"%(interface['interface_id'],
65                                                                                tag_type['tag_type_id'])
66
67         # check permission : it not admin, is the user affiliated with the right site
68         if 'admin' not in self.caller['roles']:
69             # locate node
70             node = Nodes (self.api,[interface['node_id']])[0]
71             # locate site
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']
76             
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
81
82         interface_tag = InterfaceTag(self.api)
83         interface_tag['interface_id'] = interface['interface_id']
84         interface_tag['tag_type_id'] = tag_type['tag_type_id']
85         interface_tag['value'] = value
86
87         interface_tag.sync()
88         self.object_ids = [interface_tag['interface_tag_id']]
89
90         return interface_tag['interface_tag_id']