Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / trunk / 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 Auth
6
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'] + \
11              related_fields
12
13 class UpdateNode(Method):
14     """
15     Updates a node. Only the fields specified in node_fields are
16     updated, all other fields are left untouched.
17     
18     PIs and techs can update only the nodes at their sites. Only
19     admins can update the key, session, and boot_nonce fields.
20
21     Returns 1 if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'tech']
25
26     node_fields = dict(filter(can_update, Node.fields.items() + Node.related_fields.items()))
27
28     accepts = [
29         Auth(),
30         Mixed(Node.fields['node_id'],
31               Node.fields['hostname']),
32         node_fields
33         ]
34
35     returns = Parameter(int, '1 if successful')
36
37     def call(self, auth, node_id_or_hostname, node_fields):
38         node_fields = dict(filter(can_update, node_fields.items()))
39
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):
44                     del node_fields[key]
45
46         # Get account information
47         nodes = Nodes(self.api, [node_id_or_hostname])
48         if not nodes:
49             raise PLCInvalidArgument, "No such node"
50         node = nodes[0]
51
52         if node['peer_id'] is not None:
53             raise PLCInvalidArgument, "Not a local node"
54
55         # Authenticated function
56         assert self.caller is not None
57
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"
63
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)
69
70         node.update(node_fields)
71         node.update_last_updated(False)
72         node.sync()
73         
74         # Logging variables
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']
80
81         return 1