0d07dfe3153d1c966f1f622498e45626fc691132
[plcapi.git] / PLC / Methods / DeleteIlink.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4
5 from PLC.Faults import *
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Auth import Auth
9
10 from PLC.Ilinks import Ilink, Ilinks
11 from PLC.Interfaces import Interface, Interfaces
12 from PLC.Nodes import Node, Nodes
13 from PLC.Sites import Site, Sites
14 from PLC.TagTypes import TagType, TagTypes
15
16 from PLC.AuthorizeHelpers import AuthorizeHelpers
17
18 class DeleteIlink(Method):
19     """
20     Deletes the specified ilink
21
22     Attributes may require the caller to have a particular
23     role in order to be deleted, depending on the related tag type.
24     Admins may delete attributes of any slice or sliver.
25
26     Returns 1 if successful, faults otherwise.
27     """
28
29     roles = ['admin', 'pi', 'user']
30
31     accepts = [
32         Auth(),
33         Ilink.fields['ilink_id']
34         ]
35
36     returns = Parameter(int, '1 if successful')
37
38     object_type = 'Interface'
39
40
41     def call(self, auth, ilink_id):
42         ilinks = Ilinks(self.api, [ilink_id])
43         if not ilinks:
44             raise PLCInvalidArgument, "No such ilink %r"%ilink_id
45         ilink = ilinks[0]
46
47         src_if=Interfaces(self.api,ilink['src_interface_id'])[0]
48         dst_if=Interfaces(self.api,ilink['dst_interface_id'])[0]
49         
50         tag_type_id = ilink['tag_type_id']
51         tag_type = TagTypes (self.api,[tag_type_id])[0]
52
53         # check authorizations
54         if 'admin' in self.caller['roles']:
55             pass
56         elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
57             raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
58         elif AuthorizeHelpers.interface_belongs_to_person (self.api, src_if, self.caller):
59             pass
60         elif src_if_id != dst_if_id and AuthorizeHelpers.interface_belongs_to_person (self.api, dst_if, self.caller):
61             pass
62         else:
63             raise PLCPermissionDenied, "%s: you must own either the src or dst interface"%self.name
64             
65         ilink.delete()
66         self.object_ids = [ilink['src_interface_id'],ilink['dst_interface_id']]
67
68         return 1