afc7b0c78fc1ca88bca0da8639716abfd21455c4
[plcapi.git] / PLC / Methods / BootGetNodeDetails.py
1 from PLC.Method import Method
2 from PLC.Parameter import Parameter, Mixed
3 from PLC.Auth import BootAuth
4 from PLC.Nodes import Node, Nodes
5 from PLC.Interfaces import Interface, Interfaces
6 from PLC.Sessions import Session, Sessions
7
8 class BootGetNodeDetails(Method):
9     """
10     Returns a set of details about the calling node, including a new
11     node session value.
12     """
13
14     roles = ['node']
15
16     accepts = [BootAuth()]
17
18     returns = {
19         'hostname': Node.fields['hostname'],
20         'boot_state': Node.fields['boot_state'],
21         'model': Node.fields['model'],
22         'networks': [Interface.fields],
23         'session': Session.fields['session_id'],
24         }
25
26     def call(self, auth):
27         details = {
28             'hostname': self.caller['hostname'],
29             'boot_state': self.caller['boot_state'],
30             # XXX Boot Manager cannot unmarshal None
31             'model': self.caller['model'] or "",
32             }
33
34         # Generate a new session value
35         session = Session(self.api)
36         session.sync(commit = False)
37         session.add_node(self.caller, commit = True)
38
39         details['session'] = session['session_id']
40
41         if self.caller['interface_ids']:
42             details['networks'] = Interfaces(self.api, self.caller['interface_ids'])
43             # XXX Boot Manager cannot unmarshal None
44             for network in details['networks']:
45                 for field in network:
46                     if network[field] is None:
47                         if isinstance(network[field], (int, long)):
48                             network[field] = -1
49                         else:
50                             network[field] = ""
51
52         self.message = "Node request boot_state (%s) and networks" % \
53                 (details['boot_state'])
54         return details