api now allows marshalling of None (although type checking does not allow None yet...
[plcapi.git] / PLC / Methods / UpdateNode.py
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
6
7 class UpdateNode(Method):
8     """
9     Updates a node. Only the fields specified in update_fields are
10     updated, all other fields are left untouched.
11
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.
16     
17     PIs and techs can only update the nodes at their sites.
18
19     Returns 1 if successful, faults otherwise.
20     """
21
22     roles = ['admin', 'pi', 'tech']
23
24     can_update = lambda (field, value): field in \
25                  ['hostname', 'boot_state', 'model', 'version']
26     update_fields = dict(filter(can_update, Node.fields.items()))
27
28     accepts = [
29         PasswordAuth(),
30         Mixed(Node.fields['node_id'],
31               Node.fields['hostname']),
32         update_fields
33         ]
34
35     returns = Parameter(int, '1 if successful')
36
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"
40
41         # Get account information
42         nodes = Nodes(self.api, [node_id_or_hostname])
43         if not nodes:
44             raise PLCInvalidArgument, "No such node"
45
46         node = nodes.values()[0]
47
48         # Authenticated function
49         assert self.caller is not None
50
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"
56
57         node.update(update_fields)
58         node.sync()
59
60         return 1