2 # Thierry Parmentelat - INRIA
6 from PLC.Faults import *
7 from PLC.Method import Method
8 from PLC.Parameter import Parameter, Mixed
9 from PLC.Auth import Auth
11 from PLC.TagTypes import TagType, TagTypes
12 from PLC.Ilinks import Ilink, Ilinks
13 from PLC.Interfaces import Interface, Interfaces
15 from PLC.Sites import Sites
17 class AddIlink(Method):
19 Create a link between two interfaces
20 The link has a tag type, that needs be created beforehand
21 and an optional value.
23 Returns the new ilink_id (> 0) if successful, faults
27 roles = ['admin', 'pi', 'tech', 'user']
31 # refer to either the id or the type name
32 Ilink.fields['src_interface_id'],
33 Ilink.fields['dst_interface_id'],
34 Mixed(TagType.fields['tag_type_id'],
35 TagType.fields['tagname']),
36 Ilink.fields['value'],
39 returns = Parameter(int, 'New ilink_id (> 0) if successful')
41 def call(self, auth, src_if_id, dst_if_id, tag_type_id_or_name, value):
43 src_if = Interfaces (self.api, [src_if_id],['interface_id'])
45 raise PLCInvalidArgument, "No such source interface %r"%src_if_id
46 dst_if = Interfaces (self.api, [dst_if_id],['interface_id'])
48 raise PLCInvalidArgument, "No such destination interface %r"%dst_if_id
50 tag_types = TagTypes(self.api, [tag_type_id_or_name])
52 raise PLCInvalidArgument, "AddIlink: No such tag type %r"%tag_type_id_or_name
53 tag_type = tag_types[0]
55 # checks for existence - with the same type
56 conflicts = Ilinks(self.api,
57 {'tag_type_id':tag_type['tag_type_id'],
58 'src_interface_id':src_if_id,
59 'dst_interface_id':dst_if_id,})
63 raise PLCInvalidArgument, "Ilink (%s,%d,%d) already exists and has value %r"\
64 %(tag_type['name'],src_if_id,dst_if_id,ilink['value'])
66 if 'admin' not in self.caller['roles']:
67 # # check permission : it not admin, is the user affiliated with the right site(s) ????
69 # node = Nodes (self.api,[node['node_id']])[0]
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']
76 required_min_role = tag_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
81 ilink = Ilink(self.api)
82 ilink['tag_type_id'] = tag_type['tag_type_id']
83 ilink['src_interface_id'] = src_if_id
84 ilink['dst_interface_id'] = dst_if_id
85 ilink['value'] = value
89 self.object_type = 'Interface'
90 self.object_ids = [src_if_id,dst_if_id]
92 return ilink['ilink_id']