2 from PLC.Faults import *
3 from PLC.Auth import Auth
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Table import Row
8 from PLC.Sites import Site, Sites
9 from PLC.Nodes import Node, Nodes
10 from PLC.TagTypes import TagTypes
11 from PLC.NodeTags import NodeTags
12 from PLC.Methods.AddNodeTag import AddNodeTag
13 from PLC.Methods.UpdateNodeTag import UpdateNodeTag
15 can_update = ['hostname', 'node_type', 'boot_state', 'model', 'version']
17 class AddNode(Method):
19 Adds a new node. Any values specified in node_fields are used,
20 otherwise defaults are used.
22 PIs and techs may only add nodes to their own sites. Admins may
23 add nodes to any site.
25 Returns the new node_id (> 0) if successful, faults otherwise.
28 roles = ['admin', 'pi', 'tech']
30 accepted_fields = Row.accepted_fields(can_update, [Node.fields,Node.tags])
34 Mixed(Site.fields['site_id'],
35 Site.fields['login_base']),
39 returns = Parameter(int, 'New node_id (> 0) if successful')
41 def call(self, auth, site_id_or_login_base, node_fields):
43 [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags])
46 raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected
48 # Get site information
49 sites = Sites(self.api, [site_id_or_login_base])
51 raise PLCInvalidArgument, "No such site"
55 # Authenticated function
56 assert self.caller is not None
58 # If we are not an admin, make sure that the caller is a
60 if 'admin' not in self.caller['roles']:
61 if site['site_id'] not in self.caller['site_ids']:
62 assert self.caller['person_id'] not in site['person_ids']
63 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
65 assert self.caller['person_id'] in site['person_ids']
67 node = Node(self.api, native)
68 node['site_id'] = site['site_id']
71 for (tagname,value) in tags.iteritems():
72 # the tagtype instance is assumed to exist, just check that
73 if not TagTypes(self.api,{'tagname':tagname}):
74 raise PLCInvalidArgument,"No such TagType %s"%tagname
75 node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
77 AddNodeTag(self.api).__call__(auth,node['node_id'],tagname,value)
79 UpdateNodeTag(self.api).__call__(auth,node_tags[0]['node_tag_id'],value)
81 self.event_objects = {'Site': [site['site_id']],
82 'Node': [node['node_id']]}
83 self.message = "Node %s created" % node['node_id']
85 return node['node_id']