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 class UpdateNode(Method):
9 Updates a node. Only the fields specified in update_fields are
10 updated, all other fields are left untouched.
12 To remove a value without setting a new one in its place (for
13 example, to remove an address from the node), specify -1 for int
14 and double fields and 'null' for string fields. hostname and
15 boot_state cannot be unset.
17 PIs and techs can only update the nodes at their sites.
19 Returns 1 if successful, faults otherwise.
22 roles = ['admin', 'pi', 'tech']
24 can_update = lambda (field, value): field in \
25 ['hostname', 'boot_state', 'model', 'version']
26 update_fields = dict(filter(can_update, Node.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, update_fields):
38 if filter(lambda field: field not in self.update_fields, update_fields):
39 raise PLCInvalidArgument, "Invalid field specified"
41 # Get account information
42 nodes = Nodes(self.api, [node_id_or_hostname])
44 raise PLCInvalidArgument, "No such node"
46 node = nodes.values()[0]
48 # Authenticated function
49 assert self.caller is not None
51 # If we are not an admin, make sure that the caller is a
52 # member of the site at which the node is located.
53 if 'admin' not in self.caller['roles']:
54 if node['site_id'] not in self.caller['site_ids']:
55 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
57 node.update(update_fields)