new global PLC_FLAVOUR category to globally chose sliver vref image
[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     # xxx this method is a bit spamming the events log
36     #    todo : log only when a change occurs
37     # also this seems to expect the user-provided node_fields to optionally 
38     #    contain the 'primary_network' key that should be renamed into 'primary_interface'
39     def call(self, auth, node_fields):
40         # Update node state
41         if node_fields.has_key('boot_state'):
42             self.caller['boot_state'] = node_fields['boot_state']
43         if node_fields.has_key('ssh_host_key'):
44             self.caller['ssh_rsa_key'] = node_fields['ssh_host_key']
45
46         # Update primary interface state
47         if node_fields.has_key('primary_network'):
48             primary_network = node_fields['primary_network'] 
49
50             if 'interface_id' not in primary_network:
51                 raise PLCInvalidArgument, "Interface not specified"
52             if primary_network['interface_id'] not in self.caller['interface_ids']:
53                 raise PLCInvalidArgument, "Interface not associated with calling node"
54
55             interfaces = Interfaces(self.api, [primary_network['interface_id']])
56             if not interfaces:
57                 raise PLCInvalidArgument, "No such interface %r"%interface_id
58             interface = interfaces[0]
59
60             if not interface['is_primary']:
61                 raise PLCInvalidArgument, "Not the primary interface on record"
62
63             interface_fields = dict(filter(can_update, primary_network.items()))
64             interface.update(interface_fields)
65             interface.sync(commit = False)
66
67         # indicate that node has booted & contacted PLC.
68         if isinstance(self.caller, Node):
69             node = self.caller
70             node.update_last_contact()
71
72         self.caller.sync(commit = True)
73         self.message = "Node updated: %s" % ", ".join(node_fields.keys())
74
75         return 1