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
7 related_fields = Node.related_fields.keys()
8 can_update = lambda (field, value): field in \
9 ['hostname', 'boot_state', 'model', 'version',
10 'key', 'session', 'boot_nonce', 'site_id'] + \
13 class UpdateNode(Method):
15 Updates a node. Only the fields specified in node_fields are
16 updated, all other fields are left untouched.
18 PIs and techs can update only the nodes at their sites. Only
19 admins can update the key, session, and boot_nonce fields.
21 Returns 1 if successful, faults otherwise.
24 roles = ['admin', 'pi', 'tech']
26 node_fields = dict(filter(can_update, Node.fields.items() + Node.related_fields.items()))
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):
39 node_fields = dict(filter(can_update, node_fields.items()))
41 # Remove admin only fields
42 if 'admin' not in self.caller['roles']:
43 for key in 'key', 'session', 'boot_nonce', 'site_id':
44 if node_fields.has_key(key):
47 # Get account information
48 nodes = Nodes(self.api, [node_id_or_hostname])
50 raise PLCInvalidArgument, "No such node"
53 if node['peer_id'] is not None:
54 raise PLCInvalidArgument, "Not a local node"
56 # Authenticated function
57 assert self.caller is not None
59 # If we are not an admin, make sure that the caller is a
60 # member of the site at which the node is located.
61 if 'admin' not in self.caller['roles']:
62 if node['site_id'] not in self.caller['site_ids']:
63 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
65 # Make requested associations
66 for field in related_fields:
67 if field in node_fields:
68 node.associate(auth, field, node_fields[field])
69 node_fields.pop(field)
71 node.update(node_fields)
72 node.update_last_updated(False)
76 self.event_objects = {'Node': [node['node_id']]}
77 self.message = 'Node %d updated: %s.' % \
78 (node['node_id'], ", ".join(node_fields.keys()))
79 if 'boot_state' in node_fields.keys():
80 self.message += ' boot_state updated to %s' % node_fields['boot_state']