- added 'message' instance variable (high level description of this event)
[plcapi.git] / PLC / Methods / AddNode.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Nodes import Node, Nodes
5 from PLC.NodeGroups import NodeGroup, NodeGroups
6 from PLC.Sites import Site, Sites
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field in \
10              ['hostname', 'boot_state', 'model', 'version']
11
12 class AddNode(Method):
13     """
14     Adds a new node. Any values specified in node_fields are used,
15     otherwise defaults are used.
16
17     PIs and techs may only add nodes to their own sites. Admins may
18     add nodes to any site.
19
20     Returns the new node_id (> 0) if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi', 'tech']
24
25     node_fields = dict(filter(can_update, Node.fields.items()))
26
27     accepts = [
28         Auth(),
29         Mixed(Site.fields['site_id'],
30               Site.fields['login_base']),
31         node_fields
32         ]
33
34     returns = Parameter(int, 'New node_id (> 0) if successful')
35
36
37     def call(self, auth, site_id_or_login_base, node_fields):
38         node_fields = dict(filter(can_update, node_fields.items()))
39
40         # Get site information
41         sites = Sites(self.api, [site_id_or_login_base])
42         if not sites:
43             raise PLCInvalidArgument, "No such site"
44
45         site = sites[0]
46
47         # Authenticated function
48         assert self.caller is not None
49
50         # If we are not an admin, make sure that the caller is a
51         # member of the site.
52         if 'admin' not in self.caller['roles']:
53             if site['site_id'] not in self.caller['site_ids']:
54                 assert self.caller['person_id'] not in site['person_ids']
55                 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
56             else:
57                 assert self.caller['person_id'] in site['person_ids']
58
59         node = Node(self.api, node_fields)
60         node['site_id'] = site['site_id']
61         node.sync()
62
63         self.object_ids = [site['site_id'], node['node_id']]    
64         self.message = "Node %s created" % node['node_id']
65
66         return node['node_id']