dont print tagvalue - breaks stress test
[plcapi.git] / PLC / Methods / AddNode.py
1 # $Id$
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
10
11 can_update = ['hostname', 'node_type', 'boot_state', 'model', 'version']
12
13 class AddNode(Method):
14     """
15     Adds a new node. Any values specified in node_fields are used,
16     otherwise defaults are used.
17
18     PIs and techs may only add nodes to their own sites. Admins may
19     add nodes to any site.
20
21     Returns the new node_id (> 0) if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'tech']
25
26     accepted_fields = Row.accepted_fields(can_update, [Node.fields,Node.tags])
27
28     accepts = [
29         Auth(),
30         Mixed(Site.fields['site_id'],
31               Site.fields['login_base']),
32         accepted_fields
33         ]
34
35     returns = Parameter(int, 'New node_id (> 0) if successful')
36
37     def call(self, auth, site_id_or_login_base, node_fields):
38
39         [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags])
40
41         if rejected:
42             raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected
43
44         # Get site information
45         sites = Sites(self.api, [site_id_or_login_base])
46         if not sites:
47             raise PLCInvalidArgument, "No such site"
48
49         site = sites[0]
50
51         # Authenticated function
52         assert self.caller is not None
53
54         # If we are not an admin, make sure that the caller is a
55         # member of the site.
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"
60             else:
61                 assert self.caller['person_id'] in site['person_ids']
62
63         node = Node(self.api, native)
64         node['site_id'] = site['site_id']
65         node.sync()
66
67         if tags:
68             print 'AddNode: warning, tags not handled yet',
69             for (k,v) in tags.iteritems(): print k
70
71         self.event_objects = {'Site': [site['site_id']],
72                              'Node': [node['node_id']]} 
73         self.message = "Node %s created" % node['node_id']
74
75         return node['node_id']