2 # Thierry Parmentelat - INRIA
6 from PLC.Faults import *
7 from PLC.Method import Method
8 from PLC.Parameter import Parameter, Mixed
9 from PLC.Auth import Auth
11 from PLC.NodeTagTypes import NodeTagType, NodeTagTypes
12 from PLC.NodeTags import NodeTag, NodeTags
13 from PLC.Nodes import Node, Nodes
15 from PLC.Sites import Sites
17 class AddNodeTag(Method):
19 Sets the specified tag for the specified node
20 to the specified value.
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.
25 Returns the new node_tag_id (> 0) if successful, faults
29 roles = ['admin', 'pi', 'tech', 'user']
33 # no other way to refer to a node
34 Mixed(Node.fields['node_id'],
35 Node.fields['hostname']),
36 Mixed(NodeTagType.fields['node_tag_type_id'],
37 NodeTagType.fields['tagname']),
38 NodeTag.fields['tagvalue'],
41 returns = Parameter(int, 'New node_tag_id (> 0) if successful')
46 def call(self, auth, node_id, node_tag_type_id_or_name, value):
47 nodes = Nodes(self.api, [node_id])
49 raise PLCInvalidArgument, "No such node %r"%node_id
52 node_tag_types = NodeTagTypes(self.api, [node_tag_type_id_or_name])
53 if not node_tag_types:
54 raise PLCInvalidArgument, "No such node tag type %r"%node_tag_type_id_or_name
55 node_tag_type = node_tag_types[0]
57 # checks for existence - does not allow several different tags
58 conflicts = NodeTags(self.api,
59 {'node_id':node['node_id'],
60 'node_tag_type_id':node_tag_type['node_tag_type_id']})
63 raise PLCInvalidArgument, "Node %d already has tag %d"%(node['node_id'],
64 node_tag_type['node_tag_type_id'])
66 # check permission : it not admin, is the user affiliated with the right site
67 if 'admin' not in self.caller['roles']:
69 node = Nodes (self.api,[node['node_id']])[0]
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']
76 required_min_role = node_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
81 node_tag = NodeTag(self.api)
82 node_tag['node_id'] = node['node_id']
83 node_tag['node_tag_type_id'] = node_tag_type['node_tag_type_id']
84 node_tag['tagvalue'] = value
87 self.object_ids = [node_tag['node_tag_id']]
89 return node_tag['node_tag_id']