3 # Thierry Parmentelat - INRIA
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.Ilinks import Ilink, Ilinks
14 from PLC.Interfaces import Interface, Interfaces
16 from PLC.Sites import Sites
18 class AddIlink(Method):
20 Create a link between two interfaces
21 The link has a tag type, that needs be created beforehand
22 and an optional value.
24 Returns the new ilink_id (> 0) if successful, faults
28 roles = ['admin', 'pi', 'tech', 'user']
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'],
40 returns = Parameter(int, 'New ilink_id (> 0) if successful')
42 def call(self, auth, src_if_id, dst_if_id, tag_type_id_or_name, value):
44 src_if = Interfaces (self.api, [src_if_id],['interface_id'])
46 raise PLCInvalidArgument, "No such source interface %r"%src_if_id
47 dst_if = Interfaces (self.api, [dst_if_id],['interface_id'])
49 raise PLCInvalidArgument, "No such destination interface %r"%dst_if_id
51 tag_types = TagTypes(self.api, [tag_type_id_or_name])
53 raise PLCInvalidArgument, "AddIlink: No such tag type %r"%tag_type_id_or_name
54 tag_type = tag_types[0]
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,})
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'])
67 if 'admin' not in self.caller['roles']:
68 # # check permission : it not admin, is the user affiliated with the right site(s) ????
70 # node = Nodes (self.api,[node['node_id']])[0]
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']
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
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
90 self.object_type = 'Interface'
91 self.object_ids = [src_if_id,dst_if_id]
93 return ilink['ilink_id']