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 PasswordAuth
7 can_update = lambda (field, value): field in \
8 ['hostname', 'boot_state', 'model', 'version',
11 class UpdateNode(Method):
13 Updates a node. Only the fields specified in node_fields are
14 updated, all other fields are left untouched.
16 PIs and techs can update only the nodes at their sites. Only
17 admins can update the key and session fields.
19 Returns 1 if successful, faults otherwise.
22 roles = ['admin', 'pi', 'tech']
24 node_fields = dict(filter(can_update, Node.fields.items()))
25 for field in node_fields.values():
30 Mixed(Node.fields['node_id'],
31 Node.fields['hostname']),
35 returns = Parameter(int, '1 if successful')
37 def call(self, auth, node_id_or_hostname, node_fields):
38 node_fields = dict(filter(can_update, node_fields.items()))
40 # Remove admin only fields
41 if 'admin' not in self.caller['roles']:
42 for key in 'key', 'session':
45 # Get account information
46 nodes = Nodes(self.api, [node_id_or_hostname])
48 raise PLCInvalidArgument, "No such node"
50 node = nodes.values()[0]
52 # Authenticated function
53 assert self.caller is not None
55 # If we are not an admin, make sure that the caller is a
56 # member of the site at which the node is located.
57 if 'admin' not in self.caller['roles']:
58 if node['site_id'] not in self.caller['site_ids']:
59 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
61 node.update(node_fields)