ac3274860a3d28f9a136cae33d52f8868e48b4e0
[plcapi.git] / PLC / Methods / AddIlink.py
1 # $Id$
2 #
3 # Thierry Parmentelat - INRIA
4 #
5 # $Revision: 9423 $
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.Ilinks import Ilink, Ilinks
14 from PLC.Interfaces import Interface, Interfaces
15
16 from PLC.Sites import Sites
17
18 class AddIlink(Method):
19     """
20     Create a link between two interfaces
21     The link has a tag type, that needs be created beforehand
22     and an optional value. 
23
24     Returns the new ilink_id (> 0) if successful, faults
25     otherwise.
26     """
27
28     roles = ['admin', 'pi', 'tech', 'user']
29
30     accepts = [
31         Auth(),
32         # refer to either the id or the type name
33         Ilink.fields['src_interface_id'],
34         Ilink.fields['dst_interface_id'],
35         Mixed(TagType.fields['tag_type_id'],
36               TagType.fields['tagname']),
37         Ilink.fields['value'],
38         ]
39
40     returns = Parameter(int, 'New ilink_id (> 0) if successful')
41
42     def call(self, auth,  src_if_id, dst_if_id, tag_type_id_or_name, value):
43
44         src_if = Interfaces (self.api, [src_if_id],['interface_id'])
45         if not src_if:
46             raise PLCInvalidArgument, "No such source interface %r"%src_if_id
47         dst_if = Interfaces (self.api, [dst_if_id],['interface_id'])
48         if not dst_if:
49             raise PLCInvalidArgument, "No such destination interface %r"%dst_if_id
50
51         tag_types = TagTypes(self.api, [tag_type_id_or_name])
52         if not tag_types:
53             raise PLCInvalidArgument, "AddIlink: No such tag type %r"%tag_type_id_or_name
54         tag_type = tag_types[0]
55
56         # checks for existence - with the same type
57         conflicts = Ilinks(self.api,
58                            {'tag_type_id':tag_type['tag_type_id'],
59                             'src_interface_id':src_if_id,
60                             'dst_interface_id':dst_if_id,})
61
62         if len(conflicts) :
63             ilink=conflicts[0]
64             raise PLCInvalidArgument, "Ilink (%s,%d,%d) already exists and has value %r"\
65                 %(tag_type['name'],src_if_id,dst_if_id,ilink['value'])
66
67         if 'admin' not in self.caller['roles']:
68 #       # check permission : it not admin, is the user affiliated with the right site(s) ????
69 #           # locate node
70 #           node = Nodes (self.api,[node['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 ilink, requires role %d",required_min_role
81
82         ilink = Ilink(self.api)
83         ilink['tag_type_id'] = tag_type['tag_type_id']
84         ilink['src_interface_id'] = src_if_id
85         ilink['dst_interface_id'] = dst_if_id
86         ilink['value'] = value
87
88         ilink.sync()
89
90         self.object_type = 'Interface'
91         self.object_ids = [src_if_id,dst_if_id]
92
93         return ilink['ilink_id']