This commit was manufactured by cvs2svn to create branch
[plcapi.git] / PLC / Methods / UpdateNode.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.Auth import Auth
6
7 can_update = lambda (field, value): field in \
8              ['hostname', 'boot_state', 'model', 'version',
9               'key', 'session', 'boot_nonce']
10
11 class UpdateNode(Method):
12     """
13     Updates a node. Only the fields specified in node_fields are
14     updated, all other fields are left untouched.
15     
16     PIs and techs can update only the nodes at their sites. Only
17     admins can update the key, session, and boot_nonce fields.
18
19     Returns 1 if successful, faults otherwise.
20     """
21
22     roles = ['admin', 'pi', 'tech']
23
24     node_fields = dict(filter(can_update, Node.fields.items()))
25
26     accepts = [
27         Auth(),
28         Mixed(Node.fields['node_id'],
29               Node.fields['hostname']),
30         node_fields
31         ]
32
33     returns = Parameter(int, '1 if successful')
34
35     object_type = 'Node'
36
37     def call(self, auth, node_id_or_hostname, node_fields):
38         node_fields = dict(filter(can_update, node_fields.items()))
39
40         # Remove admin only fields
41         if 'admin' not in self.caller['roles']:
42             for key in 'key', 'session', 'boot_nonce':
43                 del node_fields[key]
44
45         # Get account information
46         nodes = Nodes(self.api, [node_id_or_hostname])
47         if not nodes:
48             raise PLCInvalidArgument, "No such node"
49         node = nodes[0]
50
51         if node['peer_id'] is not None:
52             raise PLCInvalidArgument, "Not a local node"
53
54         # Authenticated function
55         assert self.caller is not None
56
57         # If we are not an admin, make sure that the caller is a
58         # member of the site at which the node is located.
59         if 'admin' not in self.caller['roles']:
60             if node['site_id'] not in self.caller['site_ids']:
61                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
62
63         node.update(node_fields)
64         node.sync()
65         
66         # Logging variables
67         self.object_ids = [node['node_id']]
68         self.message = 'Node %d updated: %s.' % \
69                 (node['node_id'], ", ".join(node_fields.keys()))
70         if 'boot_state' in node_fields.keys():
71                 self.message += ' boot_state updated to %s' %  node_fields['boot_state']
72
73         return 1