2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Table import Row
6 from PLC.Nodes import Node, Nodes
7 from PLC.NodeGroups import NodeGroup, NodeGroups
8 from PLC.Sites import Site, Sites
9 from PLC.Auth import Auth
11 can_update = ['hostname', 'node_type', 'boot_state', 'model', 'version']
13 class AddNode(Method):
15 Adds a new node. Any values specified in node_fields are used,
16 otherwise defaults are used.
18 PIs and techs may only add nodes to their own sites. Admins may
19 add nodes to any site.
21 Returns the new node_id (> 0) if successful, faults otherwise.
24 roles = ['admin', 'pi', 'tech']
26 accepted_fields = Row.accepted_fields(can_update, [Node.fields,Node.tags])
30 Mixed(Site.fields['site_id'],
31 Site.fields['login_base']),
35 returns = Parameter(int, 'New node_id (> 0) if successful')
37 def call(self, auth, site_id_or_login_base, node_fields):
39 [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags])
42 raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected
44 # Get site information
45 sites = Sites(self.api, [site_id_or_login_base])
47 raise PLCInvalidArgument, "No such site"
51 # Authenticated function
52 assert self.caller is not None
54 # If we are not an admin, make sure that the caller is a
56 if 'admin' not in self.caller['roles']:
57 if site['site_id'] not in self.caller['site_ids']:
58 assert self.caller['person_id'] not in site['person_ids']
59 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
61 assert self.caller['person_id'] in site['person_ids']
63 node = Node(self.api, native)
64 node['site_id'] = site['site_id']
68 print 'AddNode: warning, tags not handled yet',
69 for (k,v) in tags.iteritems(): print k
71 self.event_objects = {'Site': [site['site_id']],
72 'Node': [node['node_id']]}
73 self.message = "Node %s created" % node['node_id']
75 return node['node_id']