more detailed info passed when raising an exception
[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     def call(self, auth, node_id_or_hostname, node_fields):
36         node_fields = dict(filter(can_update, node_fields.items()))
37
38         # Remove admin only fields
39         if 'admin' not in self.caller['roles']:
40             for key in 'key', 'session', 'boot_nonce':
41                 del node_fields[key]
42
43         # Get account information
44         nodes = Nodes(self.api, [node_id_or_hostname])
45         if not nodes:
46             raise PLCInvalidArgument, "No such node"
47
48         node = nodes[0]
49
50         # Authenticated function
51         assert self.caller is not None
52
53         # If we are not an admin, make sure that the caller is a
54         # member of the site at which the node is located.
55         if 'admin' not in self.caller['roles']:
56             if node['site_id'] not in self.caller['site_ids']:
57                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
58
59         node.update(node_fields)
60         node.sync()
61
62         return 1