97140e9c05b86b9ffceb4a3d2e85c960d83d13da
[plcapi.git] / PLC / Methods / DeleteNodeTag.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.NodeTags import NodeTag, NodeTags
14 from PLC.Nodes import Node, Nodes
15
16 from PLC.Nodes import Node, Nodes
17 from PLC.Sites import Site, Sites
18
19 class DeleteNodeTag(Method):
20     """
21     Deletes the specified node tag
22
23     Attributes may require the caller to have a particular role in order
24     to be deleted, depending on the related node 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         NodeTag.fields['node_tag_id']
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     object_type = 'Node'
40
41
42     def call(self, auth, node_tag_id):
43         node_tags = NodeTags(self.api, [node_tag_id])
44         if not node_tags:
45             raise PLCInvalidArgument, "No such node tag %r"%node_tag_id
46         node_tag = node_tags[0]
47
48         ### reproducing a check from UpdateSliceTag, looks dumb though
49         nodes = Nodes(self.api, [node_tag['node_id']])
50         if not nodes:
51             raise PLCInvalidArgument, "No such node %r"%node_tag['node_id']
52         node = nodes[0]
53
54         assert node_tag['node_tag_id'] in node['node_tag_ids']
55
56         # check permission : it not admin, is the user affiliated with the right site
57         if 'admin' not in self.caller['roles']:
58             # locate node
59             node = Nodes (self.api,[node['node_id']])[0]
60             # locate site
61             site = Sites (self.api, [node['site_id']])[0]
62             # check caller is affiliated with this site
63             if self.caller['person_id'] not in site['person_ids']:
64                 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
65             
66             required_min_role = tag_type ['min_role_id']
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 node tag, requires role %d",required_min_role
70
71         node_tag.delete()
72         self.object_ids = [node_tag['node_tag_id']]
73
74         return 1