Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmUpdateNode.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 AdmUpdateNode(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         # XML-RPC cannot marshal None, so we need special values to
42         # represent "unset".
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
48
49         # Get account information
50         nodes = Nodes(self.api, [node_id_or_hostname])
51         if not nodes:
52             raise PLCInvalidArgument, "No such node"
53
54         node = nodes.values()[0]
55
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
61
62             if node['site_id'] not in self.caller['site_ids']:
63                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
64
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"
69
70         node.update(update_fields)
71         node.flush()
72
73         return 1