step2 : basic functions for handling nodetags and nodegroups - still highly volatile
[plcapi.git] / PLC / Methods / DeleteNodeTag.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 # $Revision: 9423 $
5 #
6
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
11
12 from PLC.NodeTags import NodeTag, NodeTags
13 from PLC.Nodes import Node, Nodes
14
15 from PLC.Nodes import Node, Nodes
16 from PLC.Sites import Site, Sites
17
18 class DeleteNodeTag(Method):
19     """
20     Deletes the specified node tag
21
22     Attributes may require the caller to have a particular role in order
23     to be deleted, depending on the related node 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         NodeTag.fields['node_tag_id']
34         ]
35
36     returns = Parameter(int, '1 if successful')
37
38     object_type = 'Node'
39
40
41     def call(self, auth, node_tag_id):
42         node_tags = NodeTags(self.api, [node_tag_id])
43         if not node_tags:
44             raise PLCInvalidArgument, "No such node tag %r"%node_tag_id
45         node_tag = node_tags[0]
46
47         ### reproducing a check from UpdateSliceAttribute, looks dumb though
48         nodes = Nodes(self.api, [node_tag['node_id']])
49         if not nodes:
50             raise PLCInvalidArgument, "No such node %r"%node_tag['node_id']
51         node = nodes[0]
52
53         assert node_tag['node_tag_id'] in node['tag_ids']
54
55         # check permission : it not admin, is the user affiliated with the right site
56         if 'admin' not in self.caller['roles']:
57             # locate node
58             node = Nodes (self.api,[node['node_id']])[0]
59             # locate site
60             site = Sites (self.api, [node['site_id']])[0]
61             # check caller is affiliated with this site
62             if self.caller['person_id'] not in site['person_ids']:
63                 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
64             
65             required_min_role = node_tag_type ['min_role_id']
66             if required_min_role is not None and \
67                     min(self.caller['role_ids']) > required_min_role:
68                 raise PLCPermissionDenied, "Not allowed to modify the specified node tag, requires role %d",required_min_role
69
70         node_tag.delete()
71         self.object_ids = [node_tag['node_tag_id']]
72
73         return 1