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