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'] + \
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):
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', 'boot_nonce':
43 if node_fields.has_key(key):
46 # Get account information
47 nodes = Nodes(self.api, [node_id_or_hostname])
49 raise PLCInvalidArgument, "No such node"
52 if node['peer_id'] is not None:
53 raise PLCInvalidArgument, "Not a local node"
55 # Authenticated function
56 assert self.caller is not None
58 # If we are not an admin, make sure that the caller is a
59 # member of the site at which the node is located.
60 if 'admin' not in self.caller['roles']:
61 if node['site_id'] not in self.caller['site_ids']:
62 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
64 # Make requested associations
65 for field in related_fields:
66 if field in node_fields:
67 node.associate(auth, field, node_fields[field])
68 node_fields.pop(field)
70 node.update(node_fields)
71 node.update_last_updated(False)
75 self.event_objects = {'Node': [node['node_id']]}
76 self.message = 'Node %d updated: %s.' % \
77 (node['node_id'], ", ".join(node_fields.keys()))
78 if 'boot_state' in node_fields.keys():
79 self.message += ' boot_state updated to %s' % node_fields['boot_state']