svn keywords
[plcapi.git] / PLC / Methods / BootUpdateNode.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Auth import Auth, BootAuth, SessionAuth
7 from PLC.Nodes import Node, Nodes
8 from PLC.Interfaces import Interface, Interfaces
9
10 can_update = lambda (field, value): field in \
11              ['method', 'mac', 'gateway', 'network',
12               'broadcast', 'netmask', 'dns1', 'dns2']
13
14 class BootUpdateNode(Method):
15     """
16     Allows the calling node to update its own record. Only the primary
17     network can be updated, and the node IP cannot be changed.
18
19     Returns 1 if updated successfully.
20     """
21
22     roles = ['node']
23
24     interface_fields = dict(filter(can_update, Interface.fields.items()))
25
26     accepts = [
27         Mixed(BootAuth(), SessionAuth()),
28         {'boot_state': Node.fields['boot_state'],
29          'primary_network': interface_fields,
30          'ssh_host_key': Node.fields['ssh_rsa_key']}
31         ]
32
33     returns = Parameter(int, '1 if successful')
34
35     def call(self, auth, node_fields):
36         # Update node state
37         if node_fields.has_key('boot_state'):
38             self.caller['boot_state'] = node_fields['boot_state']
39         if node_fields.has_key('ssh_host_key'):
40             self.caller['ssh_rsa_key'] = node_fields['ssh_host_key']
41
42         # Update primary interface state
43         if node_fields.has_key('primary_network'):
44             primary_network = node_fields['primary_network'] 
45
46             if 'interface_id' not in primary_network:
47                 raise PLCInvalidArgument, "Interface not specified"
48             if primary_network['interface_id'] not in self.caller['interface_ids']:
49                 raise PLCInvalidArgument, "Interface not associated with calling node"
50
51             interfaces = Interfaces(self.api, [primary_network['interface_id']])
52             if not interfaces:
53                 raise PLCInvalidArgument, "No such interface %r"%interface_id
54             interface = interfaces[0]
55
56             if not interface['is_primary']:
57                 raise PLCInvalidArgument, "Not the primary interface on record"
58
59             interface_fields = dict(filter(can_update, primary_network.items()))
60             interface.update(interface_fields)
61             interface.sync(commit = False)
62
63         # indicate that node has booted & contacted PLC.
64         if isinstance(self.caller, Node):
65             node = self.caller
66             node.update_last_contact()
67
68         self.caller.sync(commit = True)
69         self.message = "Node updated: %s" % ", ".join(node_fields.keys())
70
71         return 1