2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.Auth import Auth
8 related_fields = Node.related_fields.keys()
9 can_update = lambda (field, value): field in \
10 ['hostname', 'boot_state', 'model', 'version',
11 'key', 'session', 'boot_nonce', 'site_id'] + \
14 class UpdateNode(Method):
16 Updates a node. Only the fields specified in node_fields are
17 updated, all other fields are left untouched.
19 PIs and techs can update only the nodes at their sites. Only
20 admins can update the key, session, and boot_nonce fields.
22 Returns 1 if successful, faults otherwise.
25 roles = ['admin', 'pi', 'tech']
27 node_fields = dict(filter(can_update, Node.fields.items() + Node.related_fields.items()))
31 Mixed(Node.fields['node_id'],
32 Node.fields['hostname']),
36 returns = Parameter(int, '1 if successful')
38 def call(self, auth, node_id_or_hostname, node_fields):
40 node_fields = dict(filter(can_update, node_fields.items()))
42 # Remove admin only fields
43 if 'admin' not in self.caller['roles']:
44 for key in 'key', 'session', 'boot_nonce', 'site_id':
45 if node_fields.has_key(key):
48 # Get account information
49 nodes = Nodes(self.api, [node_id_or_hostname])
51 raise PLCInvalidArgument, "No such node"
54 if node['peer_id'] is not None:
55 raise PLCInvalidArgument, "Not a local node"
57 # Authenticated function
58 assert self.caller is not None
60 # If we are not an admin, make sure that the caller is a
61 # member of the site at which the node is located.
62 if 'admin' not in self.caller['roles']:
63 if node['site_id'] not in self.caller['site_ids']:
64 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
66 # Make requested associations
67 for field in related_fields:
68 if field in node_fields:
69 node.associate(auth, field, node_fields[field])
70 node_fields.pop(field)
72 node.update(node_fields)
73 node.update_last_updated(False)
77 self.event_objects = {'Node': [node['node_id']]}
78 self.message = 'Node %d updated: %s.' % \
79 (node['node_id'], ", ".join(node_fields.keys()))
80 if 'boot_state' in node_fields.keys():
81 self.message += ' boot_state updated to %s' % node_fields['boot_state']