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