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