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