svn keywords
[plcapi.git] / PLC / Methods / DeleteInterfaceTag.py
1 # $Id$
2 # $URL$
3 #
4 # Thierry Parmentelat - INRIA
5 #
6 # $Revision$
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.InterfaceTags import InterfaceTag, InterfaceTags
15 from PLC.Interfaces import Interface, Interfaces
16
17 from PLC.Nodes import Node, Nodes
18 from PLC.Sites import Site, Sites
19
20 class DeleteInterfaceTag(Method):
21     """
22     Deletes the specified interface setting
23
24     Attributes may require the caller to have a particular role in order
25     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         InterfaceTag.fields['interface_tag_id']
36         ]
37
38     returns = Parameter(int, '1 if successful')
39
40     object_type = 'Interface'
41
42
43     def call(self, auth, interface_tag_id):
44         interface_tags = InterfaceTags(self.api, [interface_tag_id])
45         if not interface_tags:
46             raise PLCInvalidArgument, "No such interface tag %r"%interface_tag_id
47         interface_tag = interface_tags[0]
48
49         ### reproducing a check from UpdateSliceTag, looks dumb though
50         interfaces = Interfaces(self.api, [interface_tag['interface_id']])
51         if not interfaces:
52             raise PLCInvalidArgument, "No such interface %r"%interface_tag['interface_id']
53         interface = interfaces[0]
54
55         assert interface_tag['interface_tag_id'] in interface['interface_tag_ids']
56
57         # check permission : it not admin, is the user affiliated with the right site
58         if 'admin' not in self.caller['roles']:
59             # locate node
60             node = Nodes (self.api,[interface['node_id']])[0]
61             # locate site
62             site = Sites (self.api, [node['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             required_min_role = tag_type ['min_role_id']
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 interface setting, requires role %d",required_min_role
71
72         interface_tag.delete()
73         self.object_ids = [interface_tag['interface_tag_id']]
74
75         return 1