3 # Thierry Parmentelat - INRIA
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
12 from PLC.TagTypes import TagType, TagTypes
13 from PLC.NodeTags import NodeTag, NodeTags
14 from PLC.Nodes import Node, Nodes
16 from PLC.Sites import Sites
18 class AddNodeTag(Method):
20 Sets the specified tag for the specified node
21 to the specified value.
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.
26 Returns the new node_tag_id (> 0) if successful, faults
30 roles = ['admin', 'pi', 'tech', 'user']
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['tagvalue'],
42 returns = Parameter(int, 'New node_tag_id (> 0) if successful')
47 def call(self, auth, node_id, tag_type_id_or_name, value):
48 nodes = Nodes(self.api, [node_id])
50 raise PLCInvalidArgument, "No such node %r"%node_id
53 tag_types = TagTypes(self.api, [tag_type_id_or_name])
55 raise PLCInvalidArgument, "No such node tag type %r"%tag_type_id_or_name
56 tag_type = tag_types[0]
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']})
64 raise PLCInvalidArgument, "Node %d already has tag %d"%(node['node_id'],
65 tag_type['tag_type_id'])
67 # check permission : it not admin, is the user affiliated with the right site
68 if 'admin' not in self.caller['roles']:
70 node = Nodes (self.api,[node['node_id']])[0]
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']
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
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['tagvalue'] = value
88 self.object_ids = [node_tag['node_tag_id']]
90 return node_tag['node_tag_id']