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 AdmUpdateNode(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 # XML-RPC cannot marshal None, so we need special values to
43 for key, value in update_fields.iteritems():
44 if value == -1 or value == "null":
45 if key in ['hostname', 'boot_state']:
46 raise PLCInvalidArgument, "hostname and boot_state cannot be unset"
47 update_fields[key] = None
49 # Get account information
50 nodes = Nodes(self.api, [node_id_or_hostname])
52 raise PLCInvalidArgument, "No such node"
54 node = nodes.values()[0]
56 # If we are not an admin, make sure that the caller is a
57 # member of the site at which the node is located.
58 if 'admin' not in self.caller['roles']:
59 # Authenticated function
60 assert self.caller is not None
62 if node['site_id'] not in self.caller['site_ids']:
63 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
65 # Check if we can update this account
66 node = nodes.values()[0]
67 if not self.caller.can_update(node):
68 raise PLCPermissionDenied, "Not allowed to update specified account"
70 node.update(update_fields)