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