397dee0e282087038df0c7941032d7c9562ce51e
[plcapi.git] / PLC / Methods / AddNodeTag.py
1 # $Id$
2 #
3 # Thierry Parmentelat - INRIA
4 #
5 # $Revision: 9423 $
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.Sites import Sites
13 from PLC.Nodes import Node, Nodes
14 from PLC.TagTypes import TagType, TagTypes
15 from PLC.NodeTags import NodeTag, NodeTags
16
17
18 class AddNodeTag(Method):
19     """
20     Sets the specified tag for the specified node
21     to the specified value.
22
23     In general only tech(s), PI(s) and of course admin(s) are allowed to
24     do the change, but this is defined in the node tag type object.
25
26     Returns the new node_tag_id (> 0) if successful, faults
27     otherwise.
28     """
29
30     roles = ['admin', 'pi', 'tech', 'user']
31
32     accepts = [
33         Auth(),
34         # no other way to refer to a node
35         Mixed(Node.fields['node_id'],
36               Node.fields['hostname']),
37         Mixed(TagType.fields['tag_type_id'],
38               TagType.fields['tagname']),
39         NodeTag.fields['value'],
40         ]
41
42     returns = Parameter(int, 'New node_tag_id (> 0) if successful')
43
44     object_type = 'Node'
45
46
47     def call(self, auth, node_id, tag_type_id_or_name, value):
48         nodes = Nodes(self.api, [node_id])
49         if not nodes:
50             raise PLCInvalidArgument, "No such node %r"%node_id
51         node = nodes[0]
52
53         tag_types = TagTypes(self.api, [tag_type_id_or_name])
54         if not tag_types:
55             raise PLCInvalidArgument, "No such node tag type %r"%tag_type_id_or_name
56         tag_type = tag_types[0]
57
58         # checks for existence - does not allow several different tags
59         conflicts = NodeTags(self.api,
60                                         {'node_id':node['node_id'],
61                                          'tag_type_id':tag_type['tag_type_id']})
62
63         if len(conflicts) :
64             raise PLCInvalidArgument, "Node %d already has tag %d"%(node['node_id'],
65                                                                                tag_type['tag_type_id'])
66
67         # check permission : it not admin, is the user affiliated with the right site
68         if 'admin' not in self.caller['roles']:
69             # locate node
70             node = Nodes (self.api,[node['node_id']])[0]
71             # locate site
72             site = Sites (self.api, [node['site_id']])[0]
73             # check caller is affiliated with this site
74             if self.caller['person_id'] not in site['person_ids']:
75                 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
76             
77             required_min_role = tag_type ['min_role_id']
78             if required_min_role is not None and \
79                     min(self.caller['role_ids']) > required_min_role:
80                 raise PLCPermissionDenied, "Not allowed to modify the specified node tag, requires role %d",required_min_role
81
82         node_tag = NodeTag(self.api)
83         node_tag['node_id'] = node['node_id']
84         node_tag['tag_type_id'] = tag_type['tag_type_id']
85         node_tag['value'] = value
86
87         node_tag.sync()
88         self.object_ids = [node_tag['node_tag_id']]
89
90         return node_tag['node_tag_id']