1282df2a648a7addceca3572ffe336202c505201
[plcapi.git] / PLC / Methods / DeleteIlink.py
1 # $Id#
2 #
3 # Thierry Parmentelat - INRIA
4 #
5 # $Revision: 9423 $
6 #
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.Ilinks import Ilink, Ilinks
14 from PLC.Interfaces import Interface, Interfaces
15 from PLC.Nodes import Node, Nodes
16 from PLC.Sites import Site, Sites
17 from PLC.TagTypes import TagType, TagTypes
18
19 class DeleteIlink(Method):
20     """
21     Deletes the specified ilink
22
23     Attributes may require the caller to have a particular 
24     role in order to be deleted, depending on the related tag type.
25     Admins may delete attributes of any slice or sliver.
26
27     Returns 1 if successful, faults otherwise.
28     """
29
30     roles = ['admin', 'pi', 'user']
31
32     accepts = [
33         Auth(),
34         Ilink.fields['ilink_id']
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     object_type = 'Interface'
40
41
42     def call(self, auth, ilink_id):
43         ilinks = Ilinks(self.api, [ilink_id])
44         if not ilinks:
45             raise PLCInvalidArgument, "No such ilink %r"%ilink_id
46         ilink = ilinks[0]
47
48         tag_type_id = ilink['tag_type_id']
49         tag_type = TagTypes (self.api,[tag_type_id])[0]
50         required_min_role = tag_type ['min_role_id']
51
52         # check permission : it not admin, is the user affiliated with the right site<S>
53         if 'admin' not in self.caller['roles']:
54             for key in ['src_interface_id','dst_interface_id']:
55                 # locate interface
56                 interface_id=ilink[key]
57                 interface = Interfaces (self.api,interface_id)[0]
58                 node_id=interface['node_id']
59                 node = Nodes (self.api,node_id) [0]
60                 # locate site
61                 site_id = node['site_id']
62                 site = Sites (self.api, [site_id]) [0]
63                 # check caller is affiliated with this site
64                 if self.caller['person_id'] not in site['person_ids']:
65                     raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
66             
67                 if required_min_role is not None and \
68                         min(self.caller['role_ids']) > required_min_role:
69                     raise PLCPermissionDenied, "Not allowed to modify the specified ilink, requires role %d",required_min_role
70
71         ilink.delete()
72         self.object_ids = [ilink['src_interface_id'],ilink['dst_interface_id']]
73
74         return 1