Add new capability to API for a node to report its current runlevel.
[plcapi.git] / PLC / Methods / ReportRunlevel.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Auth import Auth, BootAuth, SessionAuth
6 from PLC.Nodes import Node, Nodes
7
8 can_update = ['run_level', 'extrainfo']
9
10 class ReportRunlevel(Method):
11     """
12         report runlevel
13     """
14     roles = ['node', 'admin']
15
16     accepts = [
17         Mixed(BootAuth(), SessionAuth(), Auth()),
18         {'run_level': Node.fields['run_level'],
19          'extrainfo': Node.fields['extrainfo'],
20          },
21         Mixed(Node.fields['node_id'],
22               Node.fields['hostname'])
23         ]
24
25     returns = Parameter(int, '1 if successful')
26
27     def call(self, auth, report_fields, node_id_or_hostname=None):
28
29         if not isinstance(self.caller, Node):
30             # check admin
31             if 'admin' not in self.caller['roles']:
32                 raise PLCPermissionDenied, "Not allowed to update node run_level"
33
34             nodes = Nodes(self.api, [node_id_or_hostname])
35             if not nodes:
36                 raise PLCInvalidArgument, "No such node"
37         else:
38             nodes  = [self.caller]
39
40         node = nodes[0]
41
42         node.update_last_contact()
43         for field in can_update:
44             if field in report_fields:
45                 node.update({field : report_fields[field]})
46
47         node.sync(commit=True)
48
49         self.message = "Node Runlevel Report : %s" % ", ".join(report_fields.keys())
50
51         return 1